OSDN Git Service

Merge branch 'v05dev' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / app / models / artist.rb
1 class Artist < ActiveRecord::Base
2   belongs_to :author
3   has_many :original_pictures
4   has_many :pictures
5   has_many :resource_pictures
6   
7   validates :name, :presence => true, :length => {:maximum => 30}
8   validates :author_id, :numericality => {:allow_blank => true}
9   
10   before_validation :valid_encode
11   
12   def valid_encode
13     ['name'].each do |a|
14       next if attributes[a] == nil
15       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
16     end
17   end
18   
19   def supply_default
20     self.name = 'no name' if self.name.blank?
21   end
22   
23   def overwrite au
24     return false unless au
25     self.author_id = au.id
26   end
27   
28   def own? roles
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
33   end
34   
35   def visible? roles
36     if MagicNumber['run_mode'] == 0
37       return false unless guest_role_check(roles)
38     else
39       return false unless resource_reader_role_check(roles)
40     end
41     true
42   end
43   
44   def self.find_by_author author
45     Artist.find( :first, :conditions => ['author_id = ?', author.id])
46   end
47   
48   def self.default_page_size
49     25
50   end
51   
52   def self.max_page_size
53     100
54   end
55   
56   def self.page prm = nil
57     page = prm.to_i
58     page = 1 if page < 1
59     page
60   end
61   
62   def self.page_size prm = self.default_page_size
63     page_size = prm.to_i
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
66     page_size
67   end
68   
69   def self.offset cnt, prm = nil
70     offset = prm.to_i
71     offset = cnt - 1 if offset >= cnt
72     offset = cnt - offset.abs if offset < 0
73     offset = 0 if offset < 0
74     offset
75   end
76   
77   def self.list page = 1, page_size = self.default_page_size
78     opt = {}
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)
83   end
84   
85   def self.list_opt
86     {:include => {:author => {}} }
87   end
88   
89   def self.list_json_opt
90     {:include => {:author => {}} }
91   end
92   
93   def self.show aid, au
94     opt = {}
95     opt.merge!(Artist.show_opt)
96     res = Artist.find(aid, opt)
97     raise ActiveRecord::Forbidden unless res.visible?(au)
98     res
99   end
100   
101   def self.edit sid, au
102     opt = {}
103     opt.merge!(Artist.show_opt)
104     res = Artist.find sid, opt
105     raise ActiveRecord::Forbidden unless res.own?(au)
106     res
107   end
108   
109   def self.show_opt
110     {:include => {:author => {}} }
111   end
112   
113   def self.show_json_opt
114     {:include => {:author => {}} }
115   end
116   
117   def self.visible_count
118     Artist.count :conditions => ['artists.author_id is not null']
119   end
120   
121   def self.export(dt = nil)
122     opt = {}
123     cond = if dt
124       ['artists.author_id is not null and artists.updated_at >= ?', dt]
125     else
126       'artists.author_id is not null'
127     end
128     opt.merge!({:conditions => cond}) 
129     opt.merge!({:order => 'id'})
130     Artist.find(:all, opt)
131   end
132   
133 end