1 class GroundColor < ActiveRecord::Base
5 validates :panel_id, :numericality => {:allow_blank => true}
6 validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
7 validates :z, :presence => true, :numericality => {:greater_than => 0}
8 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10 before_validation :valid_encode
13 ['caption'].each do |a|
14 next if attributes[a] == nil
15 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
22 self.t = self.panel.new_t
23 self.z = self.panel.new_z
32 if MagicNumber['run_mode'] == 0
33 return false unless guest_role_check(roles)
35 return false unless reader_role_check(roles)
37 return true if self.panel.own?(roles)
38 self.panel.visible? roles
42 'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
50 self.panel.new_record? ? '0' : self.panel.id.to_s
54 self.new_record? ? '0' : self.id.to_s
62 'panels-' + self.tag_panel_id + '-ground_colors_attributes_-' + self.tag_element_id + '-' + f.to_s
65 def self.default_page_size
69 def self.max_page_size
73 def self.page prm = nil
79 def self.page_size prm = self.default_page_size
81 page_size = self.max_page_size if page_size > self.max_page_size
82 page_size = self.default_page_size if page_size < 1
90 def self.mylist_where au
91 ['panels.author_id = ?', au.id]
94 def self.himlist_where au
95 ['panels.author_id = ? and panels.publish > 0', au.id]
98 def self.list page = 1, page_size = self.default_page_size
99 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
102 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
103 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
106 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
107 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
110 def self.list_paginate page = 1, page_size = self.default_page_size
111 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
114 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
115 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
118 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
119 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
123 {:panel => {:author => {}} }
126 def self.list_json_opt
127 {:include => {:panel => {:include => {:author => {}}} }}
130 def self.show cid, roles
132 opt.merge!(GroundColor.show_opt)
133 res = GroundColor.find(cid, opt)
134 raise ActiveRecord::Forbidden unless res.visible?(roles)
139 {:include => {:panel => {:author => {}} }}
142 def self.show_json_opt
143 {:include => {:panel => {:include => {:author => {}}} }}
148 self.panel.ground_colors.build(self.attributes)
150 self.panel.ground_colors.each do |ground_color|
151 next unless ground_color == self
152 attr = self.attributes
154 ground_color.attributes = attr
158 self.panel.store({}, au)
163 ground_colors_attributes = {}
164 self.panel.ground_colors.each do |ground_color|
165 attr = ground_color.attributes
166 if ground_color == self
167 attr['_destroy'] = true
174 ground_colors_attributes[ground_color.id] = attr
176 self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
177 self.panel.store({}, au)