class Artist < ActiveRecord::Base
belongs_to :author
has_many :original_pictures
+ has_many :pictures
+ has_many :resource_pictures
validates :name, :presence => true, :length => {:maximum => 30}
-# validates :default_license_id, :presence => true, :numericality => true, :existence => true
- \r
- before_save do |r|
- r.supply_default
+ validates :author_id, :numericality => {:allow_blank => true}
+
+ before_validation :valid_encode
+
+ def valid_encode
+ ['name'].each do |a|
+ next if attributes[a] == nil
+ raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+ end
end
def supply_default
+ self.name = 'no name' if self.name.blank?
+ end
+
+ def overwrite au
+ return false unless au
+ self.author_id = au.id
+ end
+
+ def own? roles
+ roles = [roles] unless roles.respond_to?(:each)
+ au = Artist.get_author_from_roles roles
+ return false unless au
+ self.author_id == au.id
+ end
+
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
+ else
+ return false unless resource_reader_role_check(roles)
+ end
+ true
end
def self.find_by_author author
100
end
- def self.default_panel_size
- 30
- end
-
- def self.max_panel_size
- 200
- end
-
def self.page prm = nil
page = prm.to_i
page = 1 if page < 1
offset
end
- def self.list opt = {}, page = 1, page_size = self.default_page_size
- opt.merge!(self.list_opt) unless opt[:include]
- opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
- Artist.find(:all, opt)
+ def self.list_where
+ 'artists.author_id is not null'
+ end
+
+ def self.list page = 1, page_size = self.default_page_size
+ Artist.where(self.list_where()).includes(Artist.list_opt).order('artists.created_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_paginate page = 1, page_size = self.default_page_size
+ Kaminari.paginate_array(Array.new(Artist.where(self.list_where()).count, nil)).page(page).per(page_size)
end
def self.list_opt
- {:include => :author}
+ {:author => {} }
end
def self.list_json_opt
- {:include => :author}
+ {:include => {:author => {}} }
end
- def self.show aid, opt = {}
- a = Artist.find(aid, :include => self.show_include_opt(opt))
-# raise ActiveRecord::Forbidden unless c.visible?(au)
- a
+ def self.show aid, au
+ opt = {}
+ opt.merge!(Artist.show_opt)
+ res = Artist.find(aid, opt)
+ raise ActiveRecord::Forbidden unless res.visible?(au)
+ res
end
- def self.show_include_opt opt = {}
- res = [:author]
- res.push(opt[:include]) if opt[:include]
+ def self.edit sid, au
+ opt = {}
+ opt.merge!(Artist.show_opt)
+ res = Artist.find sid, opt
+ raise ActiveRecord::Forbidden unless res.own?(au)
res
end
- def self.show_json_include_opt
- {:include => :author}
+ def self.show_opt
+ {:include => {:author => {}} }
+ end
+
+ def self.show_json_opt
+ {:include => {:author => {}} }
end
def self.visible_count
- Artist.count
+ Artist.count :conditions => ['artists.author_id is not null']
+ end
+
+ def self.export(dt = nil)
+ opt = {}
+ cond = if dt
+ ['artists.author_id is not null and artists.updated_at >= ?', dt]
+ else
+ 'artists.author_id is not null'
+ end
+ opt.merge!({:conditions => cond})
+ opt.merge!({:order => 'id'})
+ Artist.find(:all, opt)
end
end