1 class GroundPicture < ActiveRecord::Base
5 validates :panel_id, :numericality => {:allow_blank => true}
6 validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
7 validates :x, :numericality => true
8 validates :y, :numericality => true
9 validates :picture_id, :numericality => true, :existence => {:both => false}
10 validates :z, :presence => true, :numericality => {:greater_than => 0}
11 validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13 @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
15 before_validation :valid_encode
18 ['caption'].each do |a|
19 next if attributes[a] == nil
20 raise Pettanr::BadRequest unless attributes[a].valid_encoding?
29 self.t = self.panel.new_t
30 self.z = self.panel.new_z
38 if MagicNumber['run_mode'] == 0
39 return false unless guest_role_check(roles)
41 return false unless reader_role_check(roles)
43 return true if self.panel.own?(roles)
44 self.panel.visible? roles
47 def opt_div_style full_url, spot = nil, opacity = 20
48 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
49 "position: absolute; width:#{self.panel.width}px; height:#{self.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}"
53 tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
54 {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
58 @@repeat_texts[self.repeat]
61 def self.default_page_size
65 def self.max_page_size
69 def self.page prm = nil
75 def self.page_size prm = self.default_page_size
77 page_size = self.max_page_size if page_size > self.max_page_size
78 page_size = self.default_page_size if page_size < 1
86 def self.mylist_where au
87 ['panels.author_id = ?', au.id]
90 def self.himlist_where au
91 ['panels.author_id = ? and panels.publish > 0', au.id]
94 def self.list page = 1, page_size = self.default_page_size
95 GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
98 def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
99 GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
102 def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
103 GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
106 def self.list_paginate page = 1, page_size = self.default_page_size
107 Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
110 def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
111 Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
114 def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
115 Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
119 {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
122 def self.list_json_opt
123 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
126 def self.show cid, au
128 opt.merge!(GroundPicture.show_opt)
129 res = GroundPicture.find(cid, opt)
130 raise ActiveRecord::Forbidden unless res.visible?(au)
135 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
138 def self.show_json_opt
139 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
144 self.panel.ground_pictures.build(self.attributes)
146 self.panel.ground_pictures.each do |ground_picture|
147 next unless ground_picture == self
148 attr = self.attributes
150 ground_picture.attributes = attr
154 self.panel.store({}, au)
159 ground_pictures_attributes = {}
160 self.panel.ground_pictures.each do |ground_picture|
161 attr = ground_picture.attributes
162 if ground_picture == self
163 attr['_destroy'] = true
170 ground_pictures_attributes[ground_picture.id] = attr
172 self.panel.attributes = {:ground_pictures_attributes => ground_pictures_attributes}
173 self.panel.store({}, au)