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
48 'panel' + tag_panel_id + tag_element_type + tag_element_id + tag_element_part_type + tag_element_part_id + c.to_s
56 self.speech_balloon.panel.new_record? ? '0' : self.speech_balloon.panel.id.to_s
60 self.speech_balloon.new_record? ? '0' : self.speech_balloon.id.to_s
67 def tag_element_part_id
68 self.new_record? ? '0' : self.id.to_s
71 def tag_element_part_type
76 'panels-' + self.tag_panel_id + '-speech_balloons_attributes-' + self.tag_element_id + '-balloon_attributes-' + f.to_s
79 def self.default_page_size
83 def self.max_page_size
87 def self.page prm = nil
93 def self.page_size prm = self.default_page_size
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
100 def self.list page = 1, page_size = self.default_page_size
102 opt.merge!(Balloon.list_opt)
103 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
104 opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
105 Balloon.find(:all, opt)
109 {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
112 def self.list_json_opt
113 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
116 def self.show cid, au
118 opt.merge!(Balloon.show_opt)
119 res = Balloon.find(cid, opt)
120 raise ActiveRecord::Forbidden unless res.visible?(au)
125 {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
128 def self.show_json_opt
129 {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}