OSDN Git Service

commit test
[pettanr/pettanr.git] / app / models / panel.rb
1 class Panel < ActiveRecord::Base
2   belongs_to :comic
3   belongs_to :author
4   belongs_to :resource_picture
5 #  belongs_to :background_picture, :class_name => 'ResourcePicture'
6   has_many :panel_pictures, :dependent => :destroy
7   has_many :speech_balloons, :dependent => :destroy
8   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
9   accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
10
11 #  validates :comic_id, :presence => true, :numericality => true, :existence => true, :uniqueness => {:scope => :t} 
12   validates :resource_picture_id, :numericality => {:allow_blank => true}
13   validates :width, :presence => true, :numericality => true, :natural_number => true
14   validates :height, :presence => true, :numericality => true, :natural_number => true
15   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   validates :x, :numericality => {:allow_blank => true}
17   validates :y, :numericality => {:allow_blank => true}
18   validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
19   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
20   validates :author_id, :presence => true, :numericality => true, :existence => true
21   
22   def supply_default au
23     return false unless au
24     c = self.comic_id ? Comic.show(self.comic_id, au) : nil
25     if c
26       self.width = c.width if self.width.blank?
27       self.height = c.height if self.height.blank?
28       self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record?
29     end
30     self.border = 0 if self.border.blank?
31     self.author_id = au.id
32   end
33   
34   def self.new_t comic_id
35     r = Panel.max_t(comic_id)
36     r.blank? ? 0 : r.to_i + 1
37   end
38   
39   def self.max_t comic_id
40     Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id])
41   end
42   
43   def self.find_t comic_id, t
44     Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
45   end
46
47   #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
48   
49   def store
50     f = nil
51     f = Panel.find_t(self.comic_id, self.t) if self.t
52     if f
53       Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
54     else
55       self.t = Panel.new_t self.comic_id
56     end
57     self.save
58   end
59   
60   def move_to new_t
61     return true if self.t == new_t
62     if self.t > new_t
63       Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
64     else
65       nf = Panel.find_t(self.comic_id, new_t)
66       max_t = Panel.max_t.to_i self.comic_id
67       new_t = max_t if new_t > max_t
68       Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
69     end
70     self.t = new_t
71     self.save
72   end
73   
74   def destroy_and_shorten
75     Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
76     self.destroy
77   end
78   
79   def self.default_page_size
80     25
81   end
82   
83   def self.max_page_size
84     100
85   end
86   
87   def self.page prm = nil
88     page = prm.to_i
89     page = 1 if page < 1
90     page
91   end
92   
93   def self.page_size prm = self.default_page_size
94     page_size = prm.to_i
95     page_size = self.max_page_size if page_size > self.max_page_size
96     page_size = self.default_page_size if page_size < 1
97     page_size
98   end
99   
100   def self.offset cnt, prm = nil
101     offset = prm.to_i
102     offset = cnt - 1 if offset >= cnt
103     offset = cnt - offset.abs if offset < 0
104     offset = 0 if offset < 0
105     offset
106   end
107   
108   def self.list opt = {}, page = 1, page_size = self.default_page_size
109     opt.merge!(self.list_opt) unless opt[:include]
110     opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
111     Panel.find(:all, opt)
112   end
113   
114   def self.list_opt
115     {:include => {
116       :comic => :author, 
117       :panel_pictures => {
118         :resource_picture => {:artist => {}, :license => {}}
119       }, 
120       :speech_balloons => {:balloons => {}, :speeches => {}},
121       :author => {}
122     }}
123   end
124   
125   def self.list_json_opt
126     {:include => {
127       :comic => {:author => {}}, 
128       :panel_pictures => {
129         :resource_picture => {:artist => {}, :license => {}}
130       }, 
131       :speech_balloons => {:balloons => {}, :speeches => {}},
132       :author => {}
133     }}
134   end
135   
136   def self.show rid, au, opt = {}
137     r = Panel.find(rid, :include => self.show_include_opt(opt))
138     raise ActiveRecord::Forbidden unless r.visible?(au)
139     r
140   end
141   
142   def self.show_include_opt opt = {}
143     res = {
144       :comic => :author, 
145       :panel_pictures => {
146         :resource_picture => {:artist => {}, :license => {}}
147       }, 
148       :speech_balloons => {:balloons => {}, :speeches => {}},
149       :author => {}
150     }
151     res.merge!(opt[:include]) if opt[:include]
152     res
153   end
154   
155   def self.show_json_include_opt
156     {:include => {
157       :comic => {:author => {}}, 
158       :panel_pictures => {
159         :resource_picture => {:artist => {}, :license => {}}
160       }, 
161       :speech_balloons => {:balloons => {}, :speeches => {}},
162       :author => {}
163     }}
164   end
165   
166   def visible? au
167     return false unless au
168     return false unless self.comic
169     self.comic.visible?(au) or self.author_id == au.id
170   end
171   
172   def own? author
173     return false unless author
174     self.author_id == author.id
175   end
176   
177   def sort_by_time
178     pe = []
179     self.panel_pictures.each do |picture|
180       pe[picture.t] = picture
181     end
182     self.balloons.each do |balloon|
183       pe[balloon.t] = balloon
184     end
185     pe
186   end
187   
188   def each_element
189     self.sort_by_time.each do |e|
190       yield e
191     end
192   end
193   
194   def panel_elements
195     res = []
196     self.each_element do |elm|
197       if elm.kind_of?(PanelPicture)
198         res[elm.t] = elm.to_json({:include => :resource_picture})
199       end
200       if elm.kind_of?(Balloon)
201         res[elm.t] = elm.to_json({:include => :speeches})
202       end
203     end
204     res
205   end
206   
207   def to_json_play
208     self.to_json :methods => :panel_elements
209   end
210   
211   def self.visible_count
212     Panel.count
213   end
214   
215 end