OSDN Git Service

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