OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / balloon.rb
1 class Balloon < Pettanr::Item
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   def self.singular
17     'Balloon'
18   end
19   
20   def self.plural
21     'Balloons'
22   end
23   
24   def self.owner_type
25     :author
26   end
27   
28   def self.valid_encode_columns
29     super.merge ['settings']
30   end
31   
32   def self.colum_structures
33     @@colum_structures ||= {
34       :r => {
35         :helper => 'panels/tail_angle_helper'
36       }
37     }
38   end
39   
40   def url
41     '/system_pictures/' + self.system_picture.filename
42   end
43   
44   def visible? roles
45     if MagicNumber['run_mode'] == 0
46       return false unless guest_role_check(roles)
47     else
48       return false unless reader_role_check(roles)
49     end
50     return true if self.speech_balloon.panel.own?(roles)
51     self.speech_balloon.panel.visible? roles
52   end
53   
54   def supply_default
55     self.x = 0
56     self.y = 0
57     self.width = 100
58     self.height = 100
59     self.r = 0
60 self.system_picture_id = 1
61   end
62   
63   def symbol_option
64     self.get_parent.speech_balloon_template.symbol_option
65   end
66   
67   def get_parent
68     self.speech_balloon || @new_parent
69   end
70   
71   def tag_element_part_type
72     'balloon'
73   end
74   
75   def self.default_page_size
76     25
77   end
78   
79   def self.max_page_size
80     100
81   end
82   
83   def self.page prm = nil
84     page = prm.to_i
85     page = 1 if page < 1
86     page
87   end
88   
89   def self.page_size prm = self.default_page_size
90     page_size = prm.to_i
91     page_size = self.max_page_size if page_size > self.max_page_size
92     page_size = self.default_page_size if page_size < 1
93     page_size
94   end
95   
96   def self.list_where
97     'panels.publish > 0'
98   end
99   
100   def self.list page = 1, page_size = self.default_page_size
101     Balloon.where(self.list_where()).includes(Balloon.list_opt).order('balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
102   end
103   
104   def self.list_paginate page = 1, page_size = self.default_page_size
105     Kaminari.paginate_array(Array.new(Balloon.where(self.list_where()).includes(Balloon.list_opt).count, nil)).page(page).per(page_size)
106   end
107   
108   def self.list_by_speech_balloon_where speech_balloon_id
109     ['balloons.speech_balloon_id = ?', speech_balloon_id]
110   end
111   
112   def self.list_by_speech_balloon speech_balloon_id, roles, page = 1, page_size = self.default_page_size
113     self.where(self.list_by_speech_balloon_where(speech_balloon_id)).includes(self.list_opt).order('balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
114   end
115   
116   def self.list_opt
117     {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}
118   end
119   
120   def self.list_json_opt
121     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
122   end
123   
124   def self.show cid, au
125     opt = {}
126     opt.merge!(Balloon.show_opt)
127     res = Balloon.find(cid, opt)
128     raise ActiveRecord::Forbidden unless res.visible?(au)
129     res
130   end
131   
132   def self.show_opt
133     {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
134   end
135   
136   def self.show_json_opt
137     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
138   end
139   
140   def copy_attributes
141     r = self.attributes
142     r.delete 'id'
143     r.delete 'speech_balloon_id'
144     r.delete 'created_at'
145     r.delete 'updated_at'
146     r
147   end
148   
149   def self.panelize balloon_attributes
150     {'balloon_attributes' => balloon_attributes}
151   end
152   
153   def scenario
154   end
155   
156   def plain_scenario
157   end
158   
159 end