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?
27 if MagicNumber['run_mode'] == 0
28 return false unless guest_role_check(roles)
30 return false unless reader_role_check(roles)
32 return true if self.panel.own?(roles)
33 self.panel.visible? roles
36 def self.default_page_size
40 def self.max_page_size
44 def self.page prm = nil
50 def self.page_size prm = self.default_page_size
52 page_size = self.max_page_size if page_size > self.max_page_size
53 page_size = self.default_page_size if page_size < 1
61 def self.mylist_where au
62 ['panels.author_id = ?', au.id]
65 def self.himlist_where au
66 ['panels.author_id = ? and panels.publish > 0', au.id]
69 def self.list page = 1, page_size = self.default_page_size
70 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
73 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
74 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
77 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
78 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
81 def self.list_paginate page = 1, page_size = self.default_page_size
82 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
85 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
86 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
89 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
90 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
94 {:panel => {:author => {}} }
97 def self.list_json_opt
98 {:include => {:panel => {:include => {:author => {}}} }}
101 def self.show cid, roles
103 opt.merge!(GroundColor.show_opt)
104 res = GroundColor.find(cid, opt)
105 raise ActiveRecord::Forbidden unless res.visible?(roles)
110 {:include => {:panel => {:author => {}} }}
113 def self.show_json_opt
114 {:include => {:panel => {:include => {:author => {}}} }}
119 self.panel.ground_colors.build(self.attributes)
121 self.panel.ground_colors.each do |ground_color|
122 next unless ground_color == self
123 attr = self.attributes
125 ground_color.attributes = attr
129 self.panel.store({}, au)
134 ground_colors_attributes = {}
135 self.panel.ground_colors.each do |ground_color|
136 attr = ground_color.attributes
137 if ground_color == self
138 attr['_destroy'] = true
145 ground_colors_attributes[ground_color.id] = attr
147 self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
148 self.panel.store({}, au)