OSDN Git Service

bed742ad7edff91caff341e8bbe0482fcefcfbf5
[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.by_author_list_includes
17     {
18       :panel => {
19         :author => {}
20       }
21     }
22   end
23   
24   def self.list_opt_for_panel
25     {
26       :ground_pictures => {
27         :picture => {:artist => {}, :license => {}}
28       }
29     }
30   end
31   
32   def self.show_opt_for_panel
33     {
34       :ground_pictures => {
35         :picture => {:artist => {}, :license => {}}
36       }
37     }
38   end
39   
40   def self.json_opt_for_panel
41     {
42       :picture => {:artist => {}, :license => {}}
43     }
44   end
45   
46   def self.has_picture?
47     true
48   end
49   
50   def supply_default
51     self.x = 0
52     self.y = 0
53     self.repeat = 0
54     if self.panel
55       self.t = self.panel.new_t 
56       self.z = self.panel.new_z 
57     end
58   end
59   
60   def overwrite pid
61     self.panel_id = pid
62   end
63   
64   def visible? operators
65     return false unless super
66     self.owner_model.visible? operators
67   end
68   
69   def style spot = nil, opacity = 20
70     r = {
71       'position' => 'absolute', 'top' => '0px', 'left' => '0px', 'z-index' => self.z, 
72       'background-image' => "url(#{self.picture.url})", 
73       'background-repeat' => self.repeat_text, 
74       'background-position' => "#{self.x}px #{self.y}px"
75     }
76     self.merge_opacity(r, opacity) if spot and spot != self
77     r
78   end
79   
80   def tmb_opt_img_tag
81     tw, th = PettanImager.thumbnail_size(self.picture.width, self.picture.height)
82     {:src => self.picture.url, :width => tw, :height => th, :alt => self.caption}
83   end
84   
85   def symbol_option
86     self.tmb_opt_img_tag
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