OSDN Git Service

t#31945:separate js
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < ActiveRecord::Base
2   include Element
3   include ElementInspire
4   belongs_to :panel
5   belongs_to :color
6   
7   validates :panel_id, :numericality => {:allow_blank => true}
8   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
9   validates :z, :presence => true, :numericality => {:greater_than => 0}
10   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
11   
12   before_validation :valid_encode
13   
14   def valid_encode
15     ['caption'].each do |a|
16       next if attributes[a] == nil
17       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
18     end
19   end
20   
21   def self.colum_structures
22     @@colum_structures ||= {
23       :code => {
24         :helper => 'panels/color_helper'
25       }
26     }
27   end
28   
29   def self.list_opt_for_panel
30     {
31       :ground_colors => {
32       }
33     }
34   end
35   
36   def self.show_opt_for_panel
37     {
38       :ground_colors => {
39       }
40     }
41   end
42   
43   def self.json_opt_for_panel
44     {
45     }
46   end
47   
48   def self.has_picture?
49     false
50   end
51   
52   def supply_default
53     self.code ||= 0
54     if self.panel
55       self.t = self.panel.new_t 
56       self.z = self.panel.new_z 
57     end
58   end
59   
60   def overwrite pid
61     self.panel_id = pid
62   end
63   
64   def visible? roles
65     if MagicNumber['run_mode'] == 0
66       return false unless guest_role_check(roles)
67     else
68       return false unless reader_role_check(roles)
69     end
70     return true if self.panel.own?(roles)
71     self.panel.visible? roles
72   end
73   
74   def opt_div_style spot = nil, opacity = 20
75     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
76     "position: absolute; width:#{self.get_panel.width}px; height:#{self.get_panel.height}px; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
77   end
78   
79   def boost
80   end
81   
82   def tag_element_type
83     'ground_color'
84   end
85   
86   def self.default_page_size
87     25
88   end
89   
90   def self.max_page_size
91     100
92   end
93   
94   def self.page prm = nil
95     page = prm.to_i
96     page = 1 if page < 1
97     page
98   end
99   
100   def self.page_size prm = self.default_page_size
101     page_size = prm.to_i
102     page_size = self.max_page_size if page_size > self.max_page_size
103     page_size = self.default_page_size if page_size < 1
104     page_size
105   end
106   
107   def self.list_where
108     'panels.publish > 0'
109   end
110   
111   def self.mylist_where au
112     ['panels.author_id = ?', au.id]
113   end
114   
115   def self.himlist_where au
116     ['panels.author_id = ? and panels.publish > 0', au.id]
117   end
118   
119   def self.list page = 1, page_size = self.default_page_size
120     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
121   end
122   
123   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
124     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
125   end
126   
127   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
128     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
129   end
130   
131   def self.list_paginate page = 1, page_size = self.default_page_size
132     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
133   end
134   
135   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
136     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
137   end
138   
139   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
140     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
141   end
142   
143   def self.list_opt
144     {:panel => {:author => {}} }
145   end
146   
147   def self.list_json_opt
148     {:include => {:panel => {:include => {:author => {}}} }}
149   end
150   
151   def self.show cid, roles
152     opt = {}
153     opt.merge!(GroundColor.show_opt)
154     res = GroundColor.find(cid, opt)
155     raise ActiveRecord::Forbidden unless res.visible?(roles)
156     res
157   end
158   
159   def self.show_opt
160     {:include => {:panel => {:author => {}} }}
161   end
162   
163   def self.show_json_opt
164     {:include => {:panel => {:include => {:author => {}}} }}
165   end
166   
167   def store au
168     if self.new_record?
169       self.panel.ground_colors.build(self.attributes)
170     else
171       self.panel.ground_colors.each do |ground_color|
172         next unless ground_color == self
173         attr = self.attributes
174         attr.delete 'id'
175         ground_color.attributes = attr
176         break
177       end
178     end
179     self.panel.store({}, au)
180   end
181   
182   def remove au
183     self.panel.remove_element(self, au)
184   end
185   
186   def scenario
187     if caption.blank?
188       ''
189     else
190       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
191     end
192   end
193   
194   def plain_scenario
195     if caption.blank?
196       ''
197     else
198       self.caption + "\n"
199     end
200   end
201   
202 end