OSDN Git Service

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