OSDN Git Service

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