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}
17 if MagicNumber['run_mode'] == 0
18 return false unless guest_role_check(roles)
20 return false unless reader_role_check(roles)
22 return true if self.panel.own?(roles)
23 self.panel.visible? roles
26 def self.default_page_size
30 def self.max_page_size
34 def self.page prm = nil
40 def self.page_size prm = self.default_page_size
42 page_size = self.max_page_size if page_size > self.max_page_size
43 page_size = self.default_page_size if page_size < 1
51 def self.mylist_where au
52 ['panels.author_id = ?', au.id]
55 def self.himlist_where au
56 ['panels.author_id = ? and panels.publish > 0', au.id]
59 def self.list page = 1, page_size = self.default_page_size
60 GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
63 def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
64 GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
67 def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
68 GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
71 def self.list_paginate page = 1, page_size = self.default_page_size
72 Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
75 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
76 Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
79 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
80 Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
84 {:panel => {:author => {}} }
87 def self.list_json_opt
88 {:include => {:panel => {:include => {:author => {}}} }}
91 def self.show cid, roles
93 opt.merge!(GroundColor.show_opt)
94 res = GroundColor.find(cid, opt)
95 raise ActiveRecord::Forbidden unless res.visible?(roles)
100 {:include => {:panel => {:author => {}} }}
103 def self.show_json_opt
104 {:include => {:panel => {:include => {:author => {}}} }}
109 self.panel.ground_colors.build(self.attributes)
111 self.panel.ground_colors.each do |ground_color|
112 next unless ground_color == self
113 attr = self.attributes
115 ground_color.attributes = attr
119 self.panel.store({}, au)
124 ground_colors_attributes = {}
125 self.panel.ground_colors.each do |ground_color|
126 attr = ground_color.attributes
127 if ground_color == self
128 attr['_destroy'] = true
135 ground_colors_attributes[ground_color.id] = attr
137 self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
138 self.panel.store({}, au)