OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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 symbol_option
57     self.get_parent.speech_balloon_template.symbol_option
58   end
59   
60   def get_parent
61     self.speech_balloon || @new_parent
62   end
63   
64   def tag_element_part_type
65     'balloon'
66   end
67   
68   def self.default_page_size
69     25
70   end
71   
72   def self.max_page_size
73     100
74   end
75   
76   def self.page prm = nil
77     page = prm.to_i
78     page = 1 if page < 1
79     page
80   end
81   
82   def self.page_size prm = self.default_page_size
83     page_size = prm.to_i
84     page_size = self.max_page_size if page_size > self.max_page_size
85     page_size = self.default_page_size if page_size < 1
86     page_size
87   end
88   
89   def self.list_where
90     'panels.publish > 0'
91   end
92   
93   def self.list page = 1, page_size = self.default_page_size
94     Balloon.where(self.list_where()).includes(Balloon.list_opt).order('balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
95   end
96   
97   def self.list_paginate page = 1, page_size = self.default_page_size
98     Kaminari.paginate_array(Array.new(Balloon.where(self.list_where()).includes(Balloon.list_opt).count, nil)).page(page).per(page_size)
99   end
100   
101   def self.list_opt
102     {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}
103   end
104   
105   def self.list_json_opt
106     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
107   end
108   
109   def self.show cid, au
110     opt = {}
111     opt.merge!(Balloon.show_opt)
112     res = Balloon.find(cid, opt)
113     raise ActiveRecord::Forbidden unless res.visible?(au)
114     res
115   end
116   
117   def self.show_opt
118     {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
119   end
120   
121   def self.show_json_opt
122     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
123   end
124   
125   def copy_attributes
126     r = self.attributes
127     r.delete 'id'
128     r.delete 'speech_balloon_id'
129     r.delete 'created_at'
130     r.delete 'updated_at'
131     r
132   end
133   
134   def self.panelize balloon_attributes
135     {'balloon_attributes' => balloon_attributes}
136   end
137   
138   def scenario
139   end
140   
141   def plain_scenario
142   end
143   
144 end