OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / app / models / balloon.rb
1 class Balloon < ActiveRecord::Base
2   belongs_to :speech_balloon
3   belongs_to :system_picture
4   
5   validates :speech_balloon_id, :numericality => {:allow_blank => true}
6   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
7   validates :x, :presence => true, :numericality => true
8   validates :y, :presence => true, :numericality => true
9   validates :width, :presence => true, :numericality => true, :natural_number => true
10   validates :height, :presence => true, :numericality => true, :natural_number => true
11 #  validates :caption, :presence => true
12 #  validates :settings, :presence => true
13
14   def url
15     '/system_pictures/' + self.system_picture.filename
16   end
17   
18   def visible? roles
19     if MagicNumber['run_mode'] == 0
20       return false unless guest_role_check(roles)
21     else
22       return false unless reader_role_check(roles)
23     end
24     return true if self.speech_balloon.panel.own?(roles)
25     self.speech_balloon.panel.visible? roles
26   end
27   
28   def self.default_page_size
29     25
30   end
31   
32   def self.max_page_size
33     100
34   end
35   
36   def self.page prm = nil
37     page = prm.to_i
38     page = 1 if page < 1
39     page
40   end
41   
42   def self.page_size prm = self.default_page_size
43     page_size = prm.to_i
44     page_size = self.max_page_size if page_size > self.max_page_size
45     page_size = self.default_page_size if page_size < 1
46     page_size
47   end
48   
49   def self.list page = 1, page_size = self.default_page_size
50     opt = {}
51     opt.merge!(Balloon.list_opt)
52     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
53     opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
54     Balloon.find(:all, opt)
55   end
56   
57   def self.list_opt
58     {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
59   end
60   
61   def self.list_json_opt
62     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
63   end
64   
65   def self.show cid, au
66     opt = {}
67     opt.merge!(Balloon.show_opt)
68     res = Balloon.find(cid, opt)
69     raise ActiveRecord::Forbidden unless res.visible?(au)
70     res
71   end
72   
73   def self.show_opt
74     {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
75   end
76   
77   def self.show_json_opt
78     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
79   end
80   
81 end