OSDN Git Service

element
[pettanr/pettanr.git] / app / models / ground_picture.rb
1 class GroundPicture < Peta::Element
2   load_manifest
3   include ElementInspire
4   belongs_to :panel
5   belongs_to :picture
6   
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}
14   
15   @@repeat_texts = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
16   
17   def self.list_opt_for_panel
18     {
19       :ground_pictures => {
20         :picture => {:artist => {}, :license => {}}
21       }
22     }
23   end
24   
25   def self.show_opt_for_panel
26     {
27       :ground_pictures => {
28         :picture => {:artist => {}, :license => {}}
29       }
30     }
31   end
32   
33   def self.json_opt_for_panel
34     {
35       :picture => {:artist => {}, :license => {}}
36     }
37   end
38   
39   def self.has_picture?
40     true
41   end
42   
43   def supply_default
44     self.x = 0
45     self.y = 0
46     self.repeat = 0
47     if self.panel
48       self.t = self.panel.new_t 
49       self.z = self.panel.new_z 
50     end
51   end
52   
53   def overwrite pid
54     self.panel_id = pid
55   end
56   
57   def visible? operators
58     return false unless super
59     self.owner_model.visible? operators
60   end
61   
62   def opt_div_style full_url, spot = nil, opacity = 20
63     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
64     "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}"
65   end
66   
67   def tmb_opt_img_tag
68     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
69     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
70   end
71   
72   def symbol_option
73     self.tmb_opt_img_tag
74   end
75   
76   def boost
77   end
78   
79   def tag_element_type
80     'ground_picture'
81   end
82   
83   def repeat_text
84     @@repeat_texts[self.repeat]
85   end
86   
87   def self.list_where
88     'panels.publish > 0'
89   end
90   
91   def self.list_order
92     'ground_pictures.updated_at desc'
93   end
94   
95   def self.list_opt
96     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
97   end
98   
99   def self.list_json_opt
100     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
101   end
102   
103   def self.show_opt
104     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
105   end
106   
107   def self.show_json_opt
108     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
109   end
110   
111   def store operators
112     if self.new_record?
113       self.panel.ground_pictures.build(self.attributes)
114     else
115       self.panel.ground_pictures.each do |ground_picture|
116         next unless ground_picture == self
117         attr = self.attributes
118         attr.delete 'id'
119         ground_picture.attributes = attr
120         break
121       end
122     end
123     self.panel.store({}, operators)
124   end
125   
126   def remove operators
127     self.panel.remove_element(self, operators)
128   end
129   
130   def scenario
131     if caption.blank?
132       ''
133     else
134       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
135     end
136   end
137   
138   def plain_scenario
139     if caption.blank?
140       ''
141     else
142       self.caption + "\n"
143     end
144   end
145   
146 end