OSDN Git Service

fix model
[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 repeat_text
83     @@repeat_texts[self.repeat]
84   end
85   
86   def self.list_where
87     'panels.publish > 0'
88   end
89   
90   def self.list_order
91     'ground_pictures.updated_at desc'
92   end
93   
94   def self.list_opt
95     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
96   end
97   
98   def self.list_json_opt
99     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
100   end
101   
102   def self.show_opt
103     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
104   end
105   
106   def self.show_json_opt
107     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
108   end
109   
110   def store operators
111     if self.new_record?
112       self.panel.ground_pictures.build(self.attributes)
113     else
114       self.panel.ground_pictures.each do |ground_picture|
115         next unless ground_picture == self
116         attr = self.attributes
117         attr.delete 'id'
118         ground_picture.attributes = attr
119         break
120       end
121     end
122     self.panel.store({}, operators)
123   end
124   
125   def remove operators
126     self.panel.remove_element(self, operators)
127   end
128   
129   def scenario
130     if caption.blank?
131       ''
132     else
133       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
134     end
135   end
136   
137   def plain_scenario
138     if caption.blank?
139       ''
140     else
141       self.caption + "\n"
142     end
143   end
144   
145 end