1 class GroundColor < Pettanr::Item
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 :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
10 validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
11 validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
12 validates :z, :presence => true, :numericality => {:greater_than => 0}
13 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
27 def self.valid_encode_columns
28 super.merge ['caption']
31 def self.colum_structures
32 @@colum_structures ||= {
34 :helper => 'panels/color_helper'
39 def self.list_opt_for_panel
46 def self.show_opt_for_panel
53 def self.json_opt_for_panel
65 self.t = self.panel.new_t
66 self.z = self.panel.new_z
75 if MagicNumber['run_mode'] == 0
76 return false unless guest_role_check(roles)
78 return false unless reader_role_check(roles)
80 return true if self.panel.own?(roles)
81 self.panel.visible? roles
89 wh ? wh : 100 - self.div_offset
93 if self.orientation == 0
101 if self.orientation == 0
109 if self.orientation == 0
117 if self.orientation == 0
124 def opt_div_style spot = nil, opacity = 20
125 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
126 "position: absolute; left:#{self.div_x}%; top:#{self.div_y}%; width:#{self.div_width}%; height:#{self.div_height}%; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
136 def self.default_page_size
140 def self.max_page_size
144 def self.page prm = nil
150 def self.page_size prm = self.default_page_size
152 page_size = self.max_page_size if page_size > self.max_page_size
153 page_size = self.default_page_size if page_size < 1
161 def self.mylist_where au
162 ['panels.author_id = ?', au.id]
165 def self.himlist_where au
166 ['panels.author_id = ? and panels.publish > 0', au.id]
169 def self.list page = 1, page_size = self.default_page_size
170 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
173 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
174 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
177 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
178 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
181 def self.list_paginate page = 1, page_size = self.default_page_size
182 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
185 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
186 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
189 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
190 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
193 def self.list_by_panel_where panel_id
194 ['ground_colors.panel_id = ?', panel_id]
197 def self.list_by_panel panel_id, roles, page = 1, page_size = self.default_page_size
198 self.where(self.list_by_panel_where(panel_id)).includes(self.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
202 {:panel => {:author => {}} }
205 def self.list_json_opt
206 {:include => {:panel => {:include => {:author => {}}} }}
209 def self.show cid, roles
211 opt.merge!(GroundColor.show_opt)
212 res = GroundColor.find(cid, opt)
213 raise ActiveRecord::Forbidden unless res.visible?(roles)
218 {:include => {:panel => {:author => {}} }}
221 def self.show_json_opt
222 {:include => {:panel => {:include => {:author => {}}} }}
227 self.panel.ground_colors.build(self.attributes)
229 self.panel.ground_colors.each do |ground_color|
230 next unless ground_color == self
231 attr = self.attributes
233 ground_color.attributes = attr
237 self.panel.store({}, au)
241 self.panel.remove_element(self, au)
248 '<p>' + ERB::Util.html_escape(self.caption) + '</p>'