OSDN Git Service

t#31896:
[pettanr/pettanr.git] / app / models / balloon.rb
1 class Balloon < ActiveRecord::Base
2   include ElementPart
3   belongs_to :speech_balloon
4   belongs_to :system_picture
5   
6   validates :speech_balloon_id, :numericality => {:allow_blank => true}
7   validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :x, :presence => true, :numericality => true
9   validates :y, :presence => true, :numericality => true
10   validates :width, :presence => true, :numericality => true, :natural_number => true
11   validates :height, :presence => true, :numericality => true, :natural_number => true
12   validates :r, :presence => true, :numericality => true
13 #  validates :caption, :presence => true
14   validates :settings, :extend_balloon => true
15
16   before_validation :valid_encode
17   
18   def valid_encode
19     ['settings'].each do |a|
20       next if attributes[a] == nil
21       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
22     end
23   end
24   
25   def self.colum_structures
26     @@colum_structures ||= {
27       :r => {
28         :helper => 'panels/tail_angle_helper'
29       }
30     }
31   end
32   
33   def url
34     '/system_pictures/' + self.system_picture.filename
35   end
36   
37   def visible? roles
38     if MagicNumber['run_mode'] == 0
39       return false unless guest_role_check(roles)
40     else
41       return false unless reader_role_check(roles)
42     end
43     return true if self.speech_balloon.panel.own?(roles)
44     self.speech_balloon.panel.visible? roles
45   end
46   
47   def supply_default
48     self.x = 0
49     self.y = 0
50     self.width = 100
51     self.height = 100
52     self.r = 0
53 self.system_picture_id = 1
54   end
55   
56   def get_parent
57     self.speech_balloon || @new_parent
58   end
59   
60   def tag_element_part_type
61     'balloon'
62   end
63   
64   def self.default_page_size
65     25
66   end
67   
68   def self.max_page_size
69     100
70   end
71   
72   def self.page prm = nil
73     page = prm.to_i
74     page = 1 if page < 1
75     page
76   end
77   
78   def self.page_size prm = self.default_page_size
79     page_size = prm.to_i
80     page_size = self.max_page_size if page_size > self.max_page_size
81     page_size = self.default_page_size if page_size < 1
82     page_size
83   end
84   
85   def self.list page = 1, page_size = self.default_page_size
86     opt = {}
87     opt.merge!(Balloon.list_opt)
88     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
89     opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
90     Balloon.find(:all, opt)
91   end
92   
93   def self.list_opt
94     {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
95   end
96   
97   def self.list_json_opt
98     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
99   end
100   
101   def self.show cid, au
102     opt = {}
103     opt.merge!(Balloon.show_opt)
104     res = Balloon.find(cid, opt)
105     raise ActiveRecord::Forbidden unless res.visible?(au)
106     res
107   end
108   
109   def self.show_opt
110     {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
111   end
112   
113   def self.show_json_opt
114     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
115   end
116   
117   def copy_attributes
118     r = self.attributes
119     r.delete 'id'
120     r.delete 'speech_balloon_id'
121     r.delete 'created_at'
122     r.delete 'updated_at'
123     r
124   end
125   
126   def self.panelize balloon_attributes
127     {'balloon_attributes' => balloon_attributes}
128   end
129   
130   def scenario
131   end
132   
133   def plain_scenario
134   end
135   
136 end