OSDN Git Service

810ffb55ee8c0b3f54f97816be8d147f26f85216
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < ActiveRecord::Base
2   include ElementInspire
3   belongs_to :panel
4   belongs_to :color
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
8   validates :z, :presence => true, :numericality => {:greater_than => 0}
9   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
10   
11   before_validation :valid_encode
12   
13   def valid_encode
14     ['caption'].each do |a|
15       next if attributes[a] == nil
16       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
17     end
18   end
19   
20   def supply_default
21     self.code ||= 0
22     if self.panel
23       self.t = self.panel.new_t 
24       self.z = self.panel.new_z 
25     end
26   end
27   
28   def overwrite pid
29     self.panel_id = pid
30   end
31   
32   def visible? roles
33     if MagicNumber['run_mode'] == 0
34       return false unless guest_role_check(roles)
35     else
36       return false unless reader_role_check(roles)
37     end
38     return true if self.panel.own?(roles)
39     self.panel.visible? roles
40   end
41   
42   def opt_div_style spot = nil, opacity = 20
43     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
44     "position: absolute; width:#{self.panel.width}px; height:#{self.panel.height}px; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
45   end
46   
47   def tag_id c = nil
48     'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
49   end
50   
51   def field_tag_id f
52     self.tag_id + f.to_s
53   end
54   
55   def tag_panel_id
56     self.panel.new_record? ? '0' : self.panel.id.to_s
57   end
58   
59   def tag_element_id
60     self.new_record? ? '0' : self.id.to_s
61   end
62   
63   def tag_element_type
64     'ground_color'
65   end
66   
67   def field_tree f
68     'panels-' + self.tag_panel_id + '-ground_colors_attributes-' + self.tag_element_id + '-' + f.to_s
69   end
70   
71   def self.default_page_size
72     25
73   end
74   
75   def self.max_page_size
76     100
77   end
78   
79   def self.page prm = nil
80     page = prm.to_i
81     page = 1 if page < 1
82     page
83   end
84   
85   def self.page_size prm = self.default_page_size
86     page_size = prm.to_i
87     page_size = self.max_page_size if page_size > self.max_page_size
88     page_size = self.default_page_size if page_size < 1
89     page_size
90   end
91   
92   def self.list_where
93     'panels.publish > 0'
94   end
95   
96   def self.mylist_where au
97     ['panels.author_id = ?', au.id]
98   end
99   
100   def self.himlist_where au
101     ['panels.author_id = ? and panels.publish > 0', au.id]
102   end
103   
104   def self.list page = 1, page_size = self.default_page_size
105     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
106   end
107   
108   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
109     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
110   end
111   
112   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
113     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
114   end
115   
116   def self.list_paginate page = 1, page_size = self.default_page_size
117     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
118   end
119   
120   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
121     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
122   end
123   
124   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
125     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
126   end
127   
128   def self.list_opt
129     {:panel => {:author => {}} }
130   end
131   
132   def self.list_json_opt
133     {:include => {:panel => {:include => {:author => {}}} }}
134   end
135   
136   def self.show cid, roles
137     opt = {}
138     opt.merge!(GroundColor.show_opt)
139     res = GroundColor.find(cid, opt)
140     raise ActiveRecord::Forbidden unless res.visible?(roles)
141     res
142   end
143   
144   def self.show_opt
145     {:include => {:panel => {:author => {}} }}
146   end
147   
148   def self.show_json_opt
149     {:include => {:panel => {:include => {:author => {}}} }}
150   end
151   
152   def store au
153     if self.new_record?
154       self.panel.ground_colors.build(self.attributes)
155     else
156       self.panel.ground_colors.each do |ground_color|
157         next unless ground_color == self
158         attr = self.attributes
159         attr.delete 'id'
160         ground_color.attributes = attr
161         break
162       end
163     end
164     self.panel.store({}, au)
165   end
166   
167   def remove au
168     self.panel.remove_element(self, au)
169   end
170   
171   def scenario
172     if caption.blank?
173       ''
174     else
175       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
176     end
177   end
178   
179   def plain_scenario
180     if caption.blank?
181       ''
182     else
183       self.caption + "\n"
184     end
185   end
186   
187 end