OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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_where
78     'artists.author_id is not null'
79   end
80   
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)
83   end
84   
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)
87   end
88   
89   def self.list_opt
90     {:author => {} }
91   end
92   
93   def self.list_json_opt
94     {:include => {:author => {}} }
95   end
96   
97   def self.show aid, au
98     opt = {}
99     opt.merge!(Artist.show_opt)
100     res = Artist.find(aid, opt)
101     raise ActiveRecord::Forbidden unless res.visible?(au)
102     res
103   end
104   
105   def self.edit sid, au
106     opt = {}
107     opt.merge!(Artist.show_opt)
108     res = Artist.find sid, opt
109     raise ActiveRecord::Forbidden unless res.own?(au)
110     res
111   end
112   
113   def self.show_opt
114     {:include => {:author => {}} }
115   end
116   
117   def self.show_json_opt
118     {:include => {:author => {}} }
119   end
120   
121   def self.visible_count
122     Artist.count :conditions => ['artists.author_id is not null']
123   end
124   
125   def self.export(dt = nil)
126     opt = {}
127     cond = if dt
128       ['artists.author_id is not null and artists.updated_at >= ?', dt]
129     else
130       'artists.author_id is not null'
131     end
132     opt.merge!({:conditions => cond}) 
133     opt.merge!({:order => 'id'})
134     Artist.find(:all, opt)
135   end
136   
137 end