OSDN Git Service

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