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}
12 @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
24 if MagicNumber['run_mode'] == 0
25 return false unless guest_role_check(roles)
27 return false unless reader_role_check(roles)
29 return true if self.panel.own?(roles)
30 self.panel.visible? roles
33 def opt_div_style full_url, spot = nil, opacity = 20
34 o = spot and spot != self ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
35 "width:#{self.panel.width}px; height:#{self.panel.height}px; z-index:#{self.z}; background-image: url(#{full_url}); background-repeat: #{self.repeat_text}; background-position: #{self.x}px, #{self.y}px; #{o}"
39 tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
40 {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
44 @@repeat_texts[self.repeat]
47 def self.default_page_size
51 def self.max_page_size
55 def self.page prm = nil
61 def self.page_size prm = self.default_page_size
63 page_size = self.max_page_size if page_size > self.max_page_size
64 page_size = self.default_page_size if page_size < 1
68 def self.list page = 1, page_size = self.default_page_size
70 opt.merge!(GroundPicture.list_opt)
71 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
72 opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_pictures.updated_at desc'})
73 GroundPicture.find(:all, opt)
76 def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
78 opt.merge!(self.list_opt)
79 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
80 opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_pictures.updated_at desc'})
81 GroundPicture.find(:all, opt)
84 def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
86 opt.merge!(self.list_opt)
87 opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
88 opt.merge!({:conditions => ['panels.author_id = ? and panels.publish > 0', au.id], :order => 'ground_pictures.updated_at desc'})
89 GroundPicture.find(:all, opt)
93 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
96 def self.list_json_opt
97 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
100 def self.show cid, au
102 opt.merge!(GroundPicture.show_opt)
103 res = GroundPicture.find(cid, opt)
104 raise ActiveRecord::Forbidden unless res.visible?(au)
109 {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
112 def self.show_json_opt
113 {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
118 self.panel.ground_pictures.build(self.attributes)
120 self.panel.ground_pictures.each do |ground_picture|
121 next unless ground_picture == self
122 attr = self.attributes
124 ground_picture.attributes = attr
128 self.panel.store({}, au)
133 ground_pictures_attributes = {}
134 self.panel.ground_pictures.each do |ground_picture|
135 attr = ground_picture.attributes
136 if ground_picture == self
137 attr['_destroy'] = true
144 ground_pictures_attributes[ground_picture.id] = attr
146 self.panel.attributes = {:ground_pictures_attributes => ground_pictures_attributes}
147 self.panel.store({}, au)