1 class Balloon < ActiveRecord::Base
2 belongs_to :speech_balloon
3 belongs_to :system_picture
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
15 before_validation :valid_encode
18 ['settings'].each do |a|
19 next if attributes[a] == nil
20 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
25 '/system_pictures/' + self.system_picture.filename
29 if MagicNumber['run_mode'] == 0
30 return false unless guest_role_check(roles)
32 return false unless reader_role_check(roles)
34 return true if self.speech_balloon.panel.own?(roles)
35 self.speech_balloon.panel.visible? roles
44 self.system_picture_id = 1
47 def get_speech_balloon
52 if self.get_speech_balloon == nil or self.get_speech_balloon.panel == nil or self.get_speech_balloon.panel.new_record?
55 self.get_speech_balloon.panel.id
60 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
68 self.get_panel_id.to_s
72 r = if self.get_speech_balloon == nil or self.get_speech_balloon.new_record?
75 self.get_speech_balloon.panel.id
84 def tag_element_part_id
85 self.new_record? ? '0' : self.id.to_s
88 def tag_element_part_type
93 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-balloon_attributes-' + f.to_s
96 def self.default_page_size
100 def self.max_page_size
104 def self.page prm = nil
110 def self.page_size prm = self.default_page_size
112 page_size = self.max_page_size if page_size > self.max_page_size
113 page_size = self.default_page_size if page_size < 1
117 def self.list page = 1, page_size = self.default_page_size
119 opt.merge!(Balloon.list_opt)
120 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
121 opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
122 Balloon.find(:all, opt)
126 {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
129 def self.list_json_opt
130 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
133 def self.show cid, au
135 opt.merge!(Balloon.show_opt)
136 res = Balloon.find(cid, opt)
137 raise ActiveRecord::Forbidden unless res.visible?(au)
142 {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
145 def self.show_json_opt
146 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}