OSDN Git Service

t#31485:editate ground_colors
[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 pid
13     self.panel_id = pid
14   end
15   
16   def visible? roles
17     if MagicNumber['run_mode'] == 0
18       return false unless guest_role_check(roles)
19     else
20       return false unless reader_role_check(roles)
21     end
22     return true if self.panel.own?(roles)
23     self.panel.visible? roles
24   end
25   
26   def self.default_page_size
27     25
28   end
29   
30   def self.max_page_size
31     100
32   end
33   
34   def self.page prm = nil
35     page = prm.to_i
36     page = 1 if page < 1
37     page
38   end
39   
40   def self.page_size prm = self.default_page_size
41     page_size = prm.to_i
42     page_size = self.max_page_size if page_size > self.max_page_size
43     page_size = self.default_page_size if page_size < 1
44     page_size
45   end
46   
47   def self.list page = 1, page_size = self.default_page_size
48     opt = {}
49     opt.merge!(GroundColor.list_opt)
50     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
51     opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_colors.updated_at desc'})
52     GroundColor.find(:all, opt)
53   end
54   
55   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
56     opt = {}
57     opt.merge!(self.list_opt)
58     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
59     opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_colors.updated_at desc'})
60     GroundColor.find(:all, opt)
61   end
62   
63   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
64     opt = {}
65     opt.merge!(self.list_opt)
66     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
67     opt.merge!({:conditions => ['panels.author_id = ? and panels.publish > 0', au.id], :order => 'ground_colors.updated_at desc'})
68     GroundColor.find(:all, opt)
69   end
70   
71   def self.list_opt
72     {:include => {:panel => {:author => {}} }}
73   end
74   
75   def self.list_json_opt
76     {:include => {:panel => {:include => {:author => {}}} }}
77   end
78   
79   def self.show cid, roles
80     opt = {}
81     opt.merge!(GroundColor.show_opt)
82     res = GroundColor.find(cid, opt)
83     raise ActiveRecord::Forbidden unless res.visible?(roles)
84     res
85   end
86   
87   def self.show_opt
88     {:include => {:panel => {:author => {}} }}
89   end
90   
91   def self.show_json_opt
92     {:include => {:panel => {:include => {:author => {}}} }}
93   end
94   
95   def store au
96     if self.new_record?
97       self.panel.ground_colors.build(self.attributes)
98     else
99       self.panel.ground_colors.each do |ground_color|
100         next unless ground_color == self
101         attr = self.attributes
102         attr.delete 'id'
103         ground_color.attributes = attr
104         break
105       end
106     end
107     self.panel.store({}, au)
108   end
109   
110   def remove au
111     PanelPicture.transaction do
112       d = false
113       panel_pictures_attributes = {}
114       
115       self.panel.panel_pictures.each do |panel_picture|
116         attr = panel_picture.attributes
117         if panel_picture == self
118           attr['_destroy'] = true
119           d = true
120         else
121           if d
122             attr['t']  -= 1 
123           end
124         end
125         panel_pictures_attributes[panel_picture.id] = attr
126       end
127       self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
128       self.panel.store({}, au)
129     end
130   end
131   
132   def scenario
133     ''
134   end
135   
136 end