OSDN Git Service

fix model
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < 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 :picture_id, :numericality => true, :existence => {:both => false}
9   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
10   validates :x, :presence => true, :numericality => true
11   validates :y, :presence => true, :numericality => true
12   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
13   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
14   validates :z, :presence => true, :numericality => {:greater_than => 0}
15   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
16   
17   def self.list_opt_for_panel
18     {
19       :panel_pictures => {
20         :picture => {:artist => {}, :license => {}}
21       }
22     }
23   end
24   
25   def self.show_opt_for_panel
26     {
27       :panel_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 visible? operators
44     return false unless super
45     self.owner_model.visible? operators
46   end
47   
48   def supply_default
49     self.x = 0
50     self.y = 0
51     if self.picture
52       self.width = self.picture.width 
53       self.height = self.picture.height 
54     end
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 flip
66     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
67     res += '/' unless res.empty?
68     res
69   end
70   
71   def filename
72     self.flip + self.picture.filename
73   end
74   
75   def url
76     '/pictures/' + self.filename
77   end
78   
79   def opt_div_style
80     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
81   end
82   
83   def opt_img_tag spot = nil, opacity = 20
84     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
85     self.tag_attributes(:img, {
86       :class => "panel-picture", :vPicture => self.id, 
87       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
88       :style => "#{o}"
89     })
90   end
91   
92   def tmb_opt_img_tag
93     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
94     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
95   end
96   
97   def symbol_option
98     self.picture.symbol_option
99   end
100   
101   def self.list_where
102     'panels.publish > 0'
103   end
104   
105   def self.list_order
106     'panel_pictures.updated_at desc'
107   end
108   
109   def self.list_opt
110     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
111   end
112   
113   def self.list_json_opt
114     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
115   end
116   
117   def self.show_opt
118     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
119   end
120   
121   def self.show_json_opt
122     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
123   end
124   
125   def store operators
126     if self.new_record?
127       self.panel.panel_pictures.build(self.attributes)
128     else
129       self.panel.panel_pictures.each do |panel_picture|
130         next unless panel_picture == self
131         attr = self.attributes
132         attr.delete 'id'
133         panel_picture.attributes = attr
134         break
135       end
136     end
137     self.panel.store({}, operators)
138   end
139   
140   def remove operators
141     self.panel.remove_element(self, operators)
142   end
143   
144   def scenario
145     if caption.blank?
146       ''
147     else
148       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
149     end
150   end
151   
152   def plain_scenario
153     if caption.blank?
154       ''
155     else
156       self.caption + "\n"
157     end
158   end
159   
160 end