1 class GroundPicture < ActiveRecord::Base
7 validates :panel_id, :numericality => {:allow_blank => true}
8 validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
9 validates :x, :numericality => true
10 validates :y, :numericality => true
11 validates :picture_id, :numericality => true, :existence => {:both => false}
12 validates :z, :presence => true, :numericality => {:greater_than => 0}
13 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15 @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
17 before_validation :valid_encode
20 ['caption'].each do |a|
21 next if attributes[a] == nil
22 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
26 def self.list_opt_for_panel
29 :picture => {:artist => {}, :license => {}}
34 def self.show_opt_for_panel
37 :picture => {:artist => {}, :license => {}}
42 def self.json_opt_for_panel
44 :picture => {:artist => {}, :license => {}}
57 self.t = self.panel.new_t
58 self.z = self.panel.new_z
67 if MagicNumber['run_mode'] == 0
68 return false unless guest_role_check(roles)
70 return false unless reader_role_check(roles)
72 return true if self.panel.own?(roles)
73 self.panel.visible? roles
76 def opt_div_style full_url, spot = nil, opacity = 20
77 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
78 "position: absolute; width:#{self.get_panel.width}px; height:#{self.get_panel.height}px; top: 0px; left: 0px; z-index:#{self.z}; background-image: url(#{full_url}); background-repeat: #{self.repeat_text}; background-position: #{self.x}px #{self.y}px; #{o}"
82 tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
83 {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
95 self.panel || @new_panel
99 'panel' + tag_panel_id + 'ground_picture' + tag_element_id + c.to_s
107 self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
111 self.new_record? ? '0' : self.id.to_s
119 'panels-' + self.tag_panel_id + '-ground_pictures_attributes-' + self.tag_element_id + '-' + f.to_s
123 @@repeat_texts[self.repeat]
126 def self.default_page_size
130 def self.max_page_size
134 def self.page prm = nil
140 def self.page_size prm = self.default_page_size
142 page_size = self.max_page_size if page_size > self.max_page_size
143 page_size = self.default_page_size if page_size < 1
151 def self.mylist_where au
152 ['panels.author_id = ?', au.id]
155 def self.himlist_where au
156 ['panels.author_id = ? and panels.publish > 0', au.id]
159 def self.list page = 1, page_size = self.default_page_size
160 GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
163 def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
164 GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
167 def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
168 GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
171 def self.list_paginate page = 1, page_size = self.default_page_size
172 Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
175 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
176 Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
179 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
180 Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
184 {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
187 def self.list_json_opt
188 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
191 def self.show cid, au
193 opt.merge!(GroundPicture.show_opt)
194 res = GroundPicture.find(cid, opt)
195 raise ActiveRecord::Forbidden unless res.visible?(au)
200 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
203 def self.show_json_opt
204 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
209 self.panel.ground_pictures.build(self.attributes)
211 self.panel.ground_pictures.each do |ground_picture|
212 next unless ground_picture == self
213 attr = self.attributes
215 ground_picture.attributes = attr
219 self.panel.store({}, au)
223 self.panel.remove_element(self, au)
230 '<p>' + ERB::Util.html_escape(self.caption) + '</p>'