OSDN Git Service

t#31538:fix default value
[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 self.default_page_size
42     25
43   end
44   
45   def self.max_page_size
46     100
47   end
48   
49   def self.page prm = nil
50     page = prm.to_i
51     page = 1 if page < 1
52     page
53   end
54   
55   def self.page_size prm = self.default_page_size
56     page_size = prm.to_i
57     page_size = self.max_page_size if page_size > self.max_page_size
58     page_size = self.default_page_size if page_size < 1
59     page_size
60   end
61   
62   def self.list_where
63     'panels.publish > 0'
64   end
65   
66   def self.mylist_where au
67     ['panels.author_id = ?', au.id]
68   end
69   
70   def self.himlist_where au
71     ['panels.author_id = ? and panels.publish > 0', au.id]
72   end
73   
74   def self.list page = 1, page_size = self.default_page_size
75     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
76   end
77   
78   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
79     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
80   end
81   
82   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
83     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
84   end
85   
86   def self.list_paginate page = 1, page_size = self.default_page_size
87     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
88   end
89   
90   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
91     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
92   end
93   
94   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
95     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
96   end
97   
98   def self.list_opt
99     {:panel => {:author => {}} }
100   end
101   
102   def self.list_json_opt
103     {:include => {:panel => {:include => {:author => {}}} }}
104   end
105   
106   def self.show cid, roles
107     opt = {}
108     opt.merge!(GroundColor.show_opt)
109     res = GroundColor.find(cid, opt)
110     raise ActiveRecord::Forbidden unless res.visible?(roles)
111     res
112   end
113   
114   def self.show_opt
115     {:include => {:panel => {:author => {}} }}
116   end
117   
118   def self.show_json_opt
119     {:include => {:panel => {:include => {:author => {}}} }}
120   end
121   
122   def store au
123     if self.new_record?
124       self.panel.ground_colors.build(self.attributes)
125     else
126       self.panel.ground_colors.each do |ground_color|
127         next unless ground_color == self
128         attr = self.attributes
129         attr.delete 'id'
130         ground_color.attributes = attr
131         break
132       end
133     end
134     self.panel.store({}, au)
135   end
136   
137   def remove au
138     d = false
139     ground_colors_attributes = {}
140     self.panel.ground_colors.each do |ground_color|
141       attr = ground_color.attributes
142       if ground_color == self
143         attr['_destroy'] = true
144         d = true
145       else
146         if d
147           attr['t']  -= 1 
148         end
149       end
150       ground_colors_attributes[ground_color.id] = attr
151     end
152     self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
153     self.panel.store({}, au)
154   end
155   
156   def scenario
157     ''
158   end
159   
160 end