OSDN Git Service

fix:new elm
[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 self.pickup_item_name
17     Picture.item_name
18   end
19   
20   def self.pickup_column_name
21     self.pickup_item_name + '_id'
22   end
23   
24   def pickup_id
25     # get :picture_id if head revision
26     self.attributes[self.pickup_column_name]
27   end
28   
29   def y
30     self.attributes['y']
31   end
32   
33   def self.by_author_list_includes
34     {
35       :panel => {
36         :author => {}
37       }
38     }
39   end
40   
41   def self.has_picture?
42     true
43   end
44   
45   def supply_default
46     self.x = 0
47     self.y = 0
48     self.repeat = 0
49     if self.panel
50       self.t = self.panel.new_t 
51       self.z = self.panel.new_z 
52     end
53   end
54   
55   def overwrite pid
56     self.panel_id = pid
57   end
58   
59   def style spot = nil, opacity = 20
60     r = {
61       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
62       'background-image' => "url(#{self.picture.url})", 
63       'background-repeat' => self.repeat_text, 
64       'background-position' => "#{self.x}px #{self.y}px"
65     }
66     self.merge_opacity(r, opacity) if spot and spot != self
67     r
68   end
69   
70   def tmb_opt_img_tag
71     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
72     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
73   end
74   
75   def symbol_option
76     self.tmb_opt_img_tag
77   end
78   
79   def repeat_text
80     @@repeat_texts[self.repeat]
81   end
82   
83   def self.public_list_where list
84     'panels.publish > 0'
85   end
86   
87   def self.show_opt
88     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
89   end
90   
91   def scenario
92     if caption.blank?
93       ''
94     else
95       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
96     end
97   end
98   
99   def plain_scenario
100     if caption.blank?
101       ''
102     else
103       self.caption + "\n"
104     end
105   end
106   
107 end