OSDN Git Service

779848dcd73ddc17129310469d0bb4f1fa919a26
[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 style spot = nil, opacity = 20
63     r = {
64       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
65       'background-image' => "url(#{self.picture.url})", 
66       'background-repeat' => self.repeat_text, 
67       'background-position' => "#{self.x}px #{self.y}px"
68     }
69     self.merge_opacity(r, opacity) if spot and spot != self
70     r
71   end
72   
73   def tmb_opt_img_tag
74     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
75     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
76   end
77   
78   def symbol_option
79     self.tmb_opt_img_tag
80   end
81   
82   def boost
83   end
84   
85   def tag_element_type
86     'ground_picture'
87   end
88   
89   def repeat_text
90     @@repeat_texts[self.repeat]
91   end
92   
93   def self.list_where
94     'panels.publish > 0'
95   end
96   
97   def self.list_order
98     'ground_pictures.updated_at desc'
99   end
100   
101   def self.list_opt
102     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
103   end
104   
105   def self.list_json_opt
106     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
107   end
108   
109   def self.show_opt
110     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
111   end
112   
113   def self.show_json_opt
114     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
115   end
116   
117   def store operators
118     if self.new_record?
119       self.panel.ground_pictures.build(self.attributes)
120     else
121       self.panel.ground_pictures.each do |ground_picture|
122         next unless ground_picture == self
123         attr = self.attributes
124         attr.delete 'id'
125         ground_picture.attributes = attr
126         break
127       end
128     end
129     self.panel.store({}, operators)
130   end
131   
132   def remove operators
133     self.panel.remove_element(self, operators)
134   end
135   
136   def scenario
137     if caption.blank?
138       ''
139     else
140       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
141     end
142   end
143   
144   def plain_scenario
145     if caption.blank?
146       ''
147     else
148       self.caption + "\n"
149     end
150   end
151   
152 end