OSDN Git Service

fix read permission for element
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :picture
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :picture_id, :numericality => true, :existence => {:both => false}
8   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
9   validates :x, :presence => true, :numericality => true
10   validates :y, :presence => true, :numericality => true
11   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
12   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true
13   validates :z, :presence => true, :numericality => {:greater_than => 0}
14   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
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       :panel_pictures => {
27         :picture => {:artist => {}, :license => {}}
28       }
29     }
30   end
31   
32   def self.show_opt_for_panel
33     {
34       :panel_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     if self.picture
54       self.width = self.picture.width 
55       self.height = self.picture.height 
56     end
57     if self.panel
58       self.t = self.panel.new_t 
59       self.z = self.panel.new_z 
60     end
61   end
62   
63   def overwrite  pid
64     self.panel_id = pid
65   end
66   
67   def flip
68     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
69     res += '/' unless res.empty?
70     res
71   end
72   
73   def filename
74     self.flip + self.picture.filename
75   end
76   
77   def url
78     '/pictures/' + self.filename
79   end
80   
81   def opt_div_style
82     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
83   end
84   
85   def opt_img_tag spot = nil, opacity = 20
86     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
87     self.tag_attributes(:img, {
88       :class => "panel-picture", :vPicture => self.id, 
89       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
90       :style => "#{o}"
91     })
92   end
93   
94   def tmb_opt_img_tag
95     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
96     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
97   end
98   
99   def symbol_option
100     self.picture.symbol_option
101   end
102   
103   def self.list_where
104     'panels.publish > 0'
105   end
106   
107   def self.list_order
108     'panel_pictures.updated_at desc'
109   end
110   
111   def self.list_opt
112     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
113   end
114   
115   def self.list_json_opt
116     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
117   end
118   
119   def self.show_opt
120     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
121   end
122   
123   def self.show_json_opt
124     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
125   end
126   
127   def store operators
128     if self.new_record?
129       self.panel.panel_pictures.build(self.attributes)
130     else
131       self.panel.panel_pictures.each do |panel_picture|
132         next unless panel_picture == self
133         attr = self.attributes
134         attr.delete 'id'
135         panel_picture.attributes = attr
136         break
137       end
138     end
139     self.panel.store({}, operators)
140   end
141   
142   def remove operators
143     self.panel.remove_element(self, operators)
144   end
145   
146   def scenario
147     if caption.blank?
148       ''
149     else
150       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
151     end
152   end
153   
154   def plain_scenario
155     if caption.blank?
156       ''
157     else
158       self.caption + "\n"
159     end
160   end
161   
162 end