OSDN Git Service

t#31486:add t and caption on ground
[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 :r, :presence => true, :numericality => true
12 #  validates :caption, :presence => true
13 #  validates :settings, :presence => true
14
15   before_validation :valid_encode
16   
17   def valid_encode
18     ['settings'].each do |a|
19       next if attributes[a] == nil
20       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
21     end
22   end
23   
24   def url
25     '/system_pictures/' + self.system_picture.filename
26   end
27   
28   def visible? roles
29     if MagicNumber['run_mode'] == 0
30       return false unless guest_role_check(roles)
31     else
32       return false unless reader_role_check(roles)
33     end
34     return true if self.speech_balloon.panel.own?(roles)
35     self.speech_balloon.panel.visible? roles
36   end
37   
38   def self.default_page_size
39     25
40   end
41   
42   def self.max_page_size
43     100
44   end
45   
46   def self.page prm = nil
47     page = prm.to_i
48     page = 1 if page < 1
49     page
50   end
51   
52   def self.page_size prm = self.default_page_size
53     page_size = prm.to_i
54     page_size = self.max_page_size if page_size > self.max_page_size
55     page_size = self.default_page_size if page_size < 1
56     page_size
57   end
58   
59   def self.list page = 1, page_size = self.default_page_size
60     opt = {}
61     opt.merge!(Balloon.list_opt)
62     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
63     opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
64     Balloon.find(:all, opt)
65   end
66   
67   def self.list_opt
68     {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
69   end
70   
71   def self.list_json_opt
72     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
73   end
74   
75   def self.show cid, au
76     opt = {}
77     opt.merge!(Balloon.show_opt)
78     res = Balloon.find(cid, opt)
79     raise ActiveRecord::Forbidden unless res.visible?(au)
80     res
81   end
82   
83   def self.show_opt
84     {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
85   end
86   
87   def self.show_json_opt
88     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
89   end
90   
91   def scenario
92     if caption.blank?
93       ''
94     else
95       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
96     end
97   end
98   
99   def plain_scenario
100     if caption.blank?
101       ''
102     else
103       self.caption + "\n"
104     end
105   end
106   
107 end