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
78 'artists.author_id is not null'
81 def self.list page = 1, page_size = self.default_page_size
82 Artist.where(self.list_where()).includes(Artist.list_opt).order('artists.created_at desc').offset((page -1) * page_size).limit(page_size)
85 def self.list_paginate page = 1, page_size = self.default_page_size
86 Kaminari.paginate_array(Array.new(Artist.where(self.list_where()).count, nil)).page(page).per(page_size)
93 def self.list_json_opt
94 {:include => {:author => {}} }
99 opt.merge!(Artist.show_opt)
100 res = Artist.find(aid, opt)
101 raise ActiveRecord::Forbidden unless res.visible?(au)
105 def self.edit sid, au
107 opt.merge!(Artist.show_opt)
108 res = Artist.find sid, opt
109 raise ActiveRecord::Forbidden unless res.own?(au)
114 {:include => {:author => {}} }
117 def self.show_json_opt
118 {:include => {:author => {}} }
121 def self.visible_count
122 Artist.count :conditions => ['artists.author_id is not null']
125 def self.export(dt = nil)
128 ['artists.author_id is not null and artists.updated_at >= ?', dt]
130 'artists.author_id is not null'
132 opt.merge!({:conditions => cond})
133 opt.merge!({:order => 'id'})
134 Artist.find(:all, opt)