OSDN Git Service

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