OSDN Git Service

t#31326:drop color
[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   
9   def supply_default
10   end
11   
12   def overwrite
13   end
14   
15   def visible? roles
16     if MagicNumber['run_mode'] == 0
17       return false unless guest_role_check(roles)
18     else
19       return false unless reader_role_check(roles)
20     end
21     return true if self.panel.own?(roles)
22     self.panel.visible? roles
23   end
24   
25   def self.default_page_size
26     25
27   end
28   
29   def self.max_page_size
30     100
31   end
32   
33   def self.page prm = nil
34     page = prm.to_i
35     page = 1 if page < 1
36     page
37   end
38   
39   def self.page_size prm = self.default_page_size
40     page_size = prm.to_i
41     page_size = self.max_page_size if page_size > self.max_page_size
42     page_size = self.default_page_size if page_size < 1
43     page_size
44   end
45   
46   def self.list page = 1, page_size = self.default_page_size
47     opt = {}
48     opt.merge!(GroundColor.list_opt)
49     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
50     opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_colors.updated_at desc'})
51     GroundColor.find(:all, opt)
52   end
53   
54   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
55     opt = {}
56     opt.merge!(self.list_opt)
57     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
58     opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_colors.updated_at desc'})
59     GroundColor.find(:all, opt)
60   end
61   
62   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
63     opt = {}
64     opt.merge!(self.list_opt)
65     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
66     opt.merge!({:conditions => ['panels.author_id = ? and panels.publish > 0', au.id], :order => 'ground_colors.updated_at desc'})
67     GroundColor.find(:all, opt)
68   end
69   
70   def self.list_opt
71     {:include => {:panel => {:author => {}} }}
72   end
73   
74   def self.list_json_opt
75     {:include => {:panel => {:include => {:author => {}}} }}
76   end
77   
78   def self.show cid, roles
79     opt = {}
80     opt.merge!(GroundColor.show_opt)
81     res = GroundColor.find(cid, opt)
82     raise ActiveRecord::Forbidden unless res.visible?(roles)
83     res
84   end
85   
86   def self.show_opt
87     {:include => {:panel => {:author => {}} }}
88   end
89   
90   def self.show_json_opt
91     {:include => {:panel => {:include => {:author => {}}} }}
92   end
93   
94   def scenario
95     ''
96   end
97   
98 end