OSDN Git Service

t#31534:merge ground
[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   end
21   
22   def overwrite pid
23     self.panel_id = pid
24   end
25   
26   def visible? roles
27     if MagicNumber['run_mode'] == 0
28       return false unless guest_role_check(roles)
29     else
30       return false unless reader_role_check(roles)
31     end
32     return true if self.panel.own?(roles)
33     self.panel.visible? roles
34   end
35   
36   def self.default_page_size
37     25
38   end
39   
40   def self.max_page_size
41     100
42   end
43   
44   def self.page prm = nil
45     page = prm.to_i
46     page = 1 if page < 1
47     page
48   end
49   
50   def self.page_size prm = self.default_page_size
51     page_size = prm.to_i
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
54     page_size
55   end
56   
57   def self.list_where
58     'panels.publish > 0'
59   end
60   
61   def self.mylist_where au
62     ['panels.author_id = ?', au.id]
63   end
64   
65   def self.himlist_where au
66     ['panels.author_id = ? and panels.publish > 0', au.id]
67   end
68   
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)
71   end
72   
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)
75   end
76   
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)
79   end
80   
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)
83   end
84   
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)
87   end
88   
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)
91   end
92   
93   def self.list_opt
94     {:panel => {:author => {}} }
95   end
96   
97   def self.list_json_opt
98     {:include => {:panel => {:include => {:author => {}}} }}
99   end
100   
101   def self.show cid, roles
102     opt = {}
103     opt.merge!(GroundColor.show_opt)
104     res = GroundColor.find(cid, opt)
105     raise ActiveRecord::Forbidden unless res.visible?(roles)
106     res
107   end
108   
109   def self.show_opt
110     {:include => {:panel => {:author => {}} }}
111   end
112   
113   def self.show_json_opt
114     {:include => {:panel => {:include => {:author => {}}} }}
115   end
116   
117   def store au
118     if self.new_record?
119       self.panel.ground_colors.build(self.attributes)
120     else
121       self.panel.ground_colors.each do |ground_color|
122         next unless ground_color == self
123         attr = self.attributes
124         attr.delete 'id'
125         ground_color.attributes = attr
126         break
127       end
128     end
129     self.panel.store({}, au)
130   end
131   
132   def remove au
133     d = false
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
139         d = true
140       else
141         if d
142           attr['t']  -= 1 
143         end
144       end
145       ground_colors_attributes[ground_color.id] = attr
146     end
147     self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
148     self.panel.store({}, au)
149   end
150   
151   def scenario
152     ''
153   end
154   
155 end