1 class Artist < ActiveRecord::Base
3 has_many :original_pictures
5 has_many :resource_pictures
7 validates :name, :presence => true, :length => {:maximum => 30}
8 validates :author_id, :numericality => {:allow_blank => true}
10 before_validation :valid_encode
14 next if attributes[a] == nil
15 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
20 self.name = 'no name' if self.name.blank?
24 return false unless au
25 self.author_id = au.id
29 roles = [roles] unless roles.respond_to?(:each)
30 au = Artist.get_author_from_roles roles
31 return false unless au
32 self.author_id == au.id
36 if MagicNumber['run_mode'] == 0
37 return false unless guest_role_check(roles)
39 return false unless resource_reader_role_check(roles)
44 def self.find_by_author author
45 Artist.find( :first, :conditions => ['author_id = ?', author.id])
48 def self.default_page_size
52 def self.max_page_size
56 def self.page prm = nil
62 def self.page_size prm = self.default_page_size
64 page_size = self.max_page_size if page_size > self.max_page_size
65 page_size = self.default_page_size if page_size < 1
69 def self.offset cnt, prm = nil
71 offset = cnt - 1 if offset >= cnt
72 offset = cnt - offset.abs if offset < 0
73 offset = 0 if offset < 0
77 def self.list page = 1, page_size = self.default_page_size
79 opt.merge!(Artist.list_opt)
80 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
81 opt.merge!({:conditions => ['artists.author_id is not null'], :order => 'artists.created_at desc'})
82 Artist.find(:all, opt)
86 {:include => {:author => {}} }
89 def self.list_json_opt
90 {:include => {:author => {}} }
95 opt.merge!(Artist.show_opt)
96 res = Artist.find(aid, opt)
97 raise ActiveRecord::Forbidden unless res.visible?(au)
101 def self.edit sid, au
103 opt.merge!(Artist.show_opt)
104 res = Artist.find sid, opt
105 raise ActiveRecord::Forbidden unless res.own?(au)
110 {:include => {:author => {}} }
113 def self.show_json_opt
114 {:include => {:author => {}} }
117 def self.visible_count
118 Artist.count :conditions => ['artists.author_id is not null']
121 def self.export(dt = nil)
124 ['artists.author_id is not null and artists.updated_at >= ?', dt]
126 'artists.author_id is not null'
128 opt.merge!({:conditions => cond})
129 opt.merge!({:order => 'id'})
130 Artist.find(:all, opt)