OSDN Git Service

t#31558:
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < ActiveRecord::Base
2   belongs_to :panel
3   belongs_to :color
4   
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}
9   
10   before_validation :valid_encode
11   
12   def valid_encode
13     ['caption'].each do |a|
14       next if attributes[a] == nil
15       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
16     end
17   end
18   
19   def supply_default
20     self.code = 0
21     if self.panel
22       self.t = self.panel.new_t 
23       self.z = self.panel.new_z 
24     end
25   end
26   
27   def overwrite pid
28     self.panel_id = pid
29   end
30   
31   def visible? roles
32     if MagicNumber['run_mode'] == 0
33       return false unless guest_role_check(roles)
34     else
35       return false unless reader_role_check(roles)
36     end
37     return true if self.panel.own?(roles)
38     self.panel.visible? roles
39   end
40   
41   def tag_id c = nil
42     'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
43   end
44   
45   def field_tag_id f
46     self.tag_id + f.to_s
47   end
48   
49   def tag_panel_id
50     self.panel.new_record? ? '0' : self.panel.id.to_s
51   end
52   
53   def tag_element_id
54     self.new_record? ? '0' : self.id.to_s
55   end
56   
57   def tag_element_type
58     'ground_color'
59   end
60   
61   def field_tree f
62     'panels-' + self.tag_panel_id + '-ground_colors_attributes_-' + self.tag_element_id + '-' + f.to_s
63   end
64   
65   def self.default_page_size
66     25
67   end
68   
69   def self.max_page_size
70     100
71   end
72   
73   def self.page prm = nil
74     page = prm.to_i
75     page = 1 if page < 1
76     page
77   end
78   
79   def self.page_size prm = self.default_page_size
80     page_size = prm.to_i
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
83     page_size
84   end
85   
86   def self.list_where
87     'panels.publish > 0'
88   end
89   
90   def self.mylist_where au
91     ['panels.author_id = ?', au.id]
92   end
93   
94   def self.himlist_where au
95     ['panels.author_id = ? and panels.publish > 0', au.id]
96   end
97   
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)
100   end
101   
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)
104   end
105   
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)
108   end
109   
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)
112   end
113   
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)
116   end
117   
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)
120   end
121   
122   def self.list_opt
123     {:panel => {:author => {}} }
124   end
125   
126   def self.list_json_opt
127     {:include => {:panel => {:include => {:author => {}}} }}
128   end
129   
130   def self.show cid, roles
131     opt = {}
132     opt.merge!(GroundColor.show_opt)
133     res = GroundColor.find(cid, opt)
134     raise ActiveRecord::Forbidden unless res.visible?(roles)
135     res
136   end
137   
138   def self.show_opt
139     {:include => {:panel => {:author => {}} }}
140   end
141   
142   def self.show_json_opt
143     {:include => {:panel => {:include => {:author => {}}} }}
144   end
145   
146   def store au
147     if self.new_record?
148       self.panel.ground_colors.build(self.attributes)
149     else
150       self.panel.ground_colors.each do |ground_color|
151         next unless ground_color == self
152         attr = self.attributes
153         attr.delete 'id'
154         ground_color.attributes = attr
155         break
156       end
157     end
158     self.panel.store({}, au)
159   end
160   
161   def remove au
162     d = false
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
168         d = true
169       else
170         if d
171           attr['t']  -= 1 
172         end
173       end
174       ground_colors_attributes[ground_color.id] = attr
175     end
176     self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
177     self.panel.store({}, au)
178   end
179   
180   def scenario
181     ''
182   end
183   
184 end