OSDN Git Service

classname rename to module_name
[pettanr/pettanr.git] / app / models / ground_picture.rb
1 class GroundPicture < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :picture
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
8   validates :x, :numericality => true
9   validates :y, :numericality => true
10   validates :picture_id, :numericality => true, :existence => {:both => false}
11   validates :z, :presence => true, :numericality => {:greater_than => 0}
12   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13   
14   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
15   
16   def _y
17     self.attributes['y']
18   end
19   
20   def self.by_author_list_includes
21     {
22       :panel => {
23         :author => {}
24       }
25     }
26   end
27   
28   def self.has_picture?
29     true
30   end
31   
32   def supply_default
33     self.x = 0
34     self.y = 0
35     self.repeat = 0
36     if self.panel
37       self.t = self.panel.new_t 
38       self.z = self.panel.new_z 
39     end
40   end
41   
42   def overwrite pid
43     self.panel_id = pid
44   end
45   
46   def style spot = nil, opacity = 20
47     r = {
48       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
49       'background-image' => "url(#{self.picture.url})", 
50       'background-repeat' => self.repeat_text, 
51       'background-position' => "#{self.x}px #{self._y}px"
52     }
53     self.merge_opacity(r, opacity) if spot and spot != self
54     r
55   end
56   
57   def tmb_opt_img_tag
58     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
59     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
60   end
61   
62   def symbol_option
63     self.tmb_opt_img_tag
64   end
65   
66   def repeat_text
67     @@repeat_texts[self.repeat]
68   end
69   
70   def self.public_list_where
71     'panels.publish > 0'
72   end
73   
74   def self.show_opt
75     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
76   end
77   
78   def scenario
79     if caption.blank?
80       ''
81     else
82       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
83     end
84   end
85   
86   def plain_scenario
87     if caption.blank?
88       ''
89     else
90       self.caption + "\n"
91     end
92   end
93   
94 end