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 :z, :presence => true, :numericality => {:greater_than => 0}
10 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
12 before_validation :valid_encode
15 ['caption'].each do |a|
16 next if attributes[a] == nil
17 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
21 def self.list_opt_for_panel
28 def self.show_opt_for_panel
35 def self.json_opt_for_panel
47 self.t = self.panel.new_t
48 self.z = self.panel.new_z
57 if MagicNumber['run_mode'] == 0
58 return false unless guest_role_check(roles)
60 return false unless reader_role_check(roles)
62 return true if self.panel.own?(roles)
63 self.panel.visible? roles
66 def opt_div_style spot = nil, opacity = 20
67 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
68 "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}"
80 self.panel || @new_panel
84 'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
92 self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
96 self.new_record? ? '0' : self.id.to_s
103 def self.default_page_size
107 def self.max_page_size
111 def self.page prm = nil
117 def self.page_size prm = self.default_page_size
119 page_size = self.max_page_size if page_size > self.max_page_size
120 page_size = self.default_page_size if page_size < 1
128 def self.mylist_where au
129 ['panels.author_id = ?', au.id]
132 def self.himlist_where au
133 ['panels.author_id = ? and panels.publish > 0', au.id]
136 def self.list page = 1, page_size = self.default_page_size
137 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
140 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
141 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
144 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
145 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
148 def self.list_paginate page = 1, page_size = self.default_page_size
149 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
152 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
153 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
156 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
157 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
161 {:panel => {:author => {}} }
164 def self.list_json_opt
165 {:include => {:panel => {:include => {:author => {}}} }}
168 def self.show cid, roles
170 opt.merge!(GroundColor.show_opt)
171 res = GroundColor.find(cid, opt)
172 raise ActiveRecord::Forbidden unless res.visible?(roles)
177 {:include => {:panel => {:author => {}} }}
180 def self.show_json_opt
181 {:include => {:panel => {:include => {:author => {}}} }}
186 self.panel.ground_colors.build(self.attributes)
188 self.panel.ground_colors.each do |ground_color|
189 next unless ground_color == self
190 attr = self.attributes
192 ground_color.attributes = attr
196 self.panel.store({}, au)
200 self.panel.remove_element(self, au)
207 '<p>' + ERB::Util.html_escape(self.caption) + '</p>'