1 class GroundColor < ActiveRecord::Base
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}
15 before_validation :valid_encode
18 ['caption'].each do |a|
19 next if attributes[a] == nil
20 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
24 def self.colum_structures
25 @@colum_structures ||= {
27 :helper => 'panels/color_helper'
32 def self.list_opt_for_panel
39 def self.show_opt_for_panel
46 def self.json_opt_for_panel
58 self.t = self.panel.new_t
59 self.z = self.panel.new_z
68 if MagicNumber['run_mode'] == 0
69 return false unless guest_role_check(roles)
71 return false unless reader_role_check(roles)
73 return true if self.panel.own?(roles)
74 self.panel.visible? roles
82 wh ? wh : 100 - self.div_offset
86 if self.orientation == 0
94 if self.orientation == 0
102 if self.orientation == 0
110 if self.orientation == 0
117 def opt_div_style spot = nil, opacity = 20
118 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
119 "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}"
129 def self.default_page_size
133 def self.max_page_size
137 def self.page prm = nil
143 def self.page_size prm = self.default_page_size
145 page_size = self.max_page_size if page_size > self.max_page_size
146 page_size = self.default_page_size if page_size < 1
154 def self.mylist_where au
155 ['panels.author_id = ?', au.id]
158 def self.himlist_where au
159 ['panels.author_id = ? and panels.publish > 0', au.id]
162 def self.list page = 1, page_size = self.default_page_size
163 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
166 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
167 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
170 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
171 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
174 def self.list_paginate page = 1, page_size = self.default_page_size
175 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
178 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
179 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
182 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
183 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
187 {:panel => {:author => {}} }
190 def self.list_json_opt
191 {:include => {:panel => {:include => {:author => {}}} }}
194 def self.show cid, roles
196 opt.merge!(GroundColor.show_opt)
197 res = GroundColor.find(cid, opt)
198 raise ActiveRecord::Forbidden unless res.visible?(roles)
203 {:include => {:panel => {:author => {}} }}
206 def self.show_json_opt
207 {:include => {:panel => {:include => {:author => {}}} }}
212 self.panel.ground_colors.build(self.attributes)
214 self.panel.ground_colors.each do |ground_color|
215 next unless ground_color == self
216 attr = self.attributes
218 ground_color.attributes = attr
222 self.panel.store({}, au)
226 self.panel.remove_element(self, au)
233 '<p>' + ERB::Util.html_escape(self.caption) + '</p>'