OSDN Git Service

e8062177709115fcf6a992cc649de3caf6c17749
[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 visible? operators
51     return false unless super
52     self.owner_model.visible? operators
53   end
54   
55   def supply_default
56     self.x = 0
57     self.y = 0
58     if self.picture
59       self.width = self.picture.width 
60       self.height = self.picture.height 
61     end
62     if self.panel
63       self.t = self.panel.new_t 
64       self.z = self.panel.new_z 
65     end
66   end
67   
68   def overwrite  pid
69     self.panel_id = pid
70   end
71   
72   def flip
73     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
74     res += '/' unless res.empty?
75     res
76   end
77   
78   def filename
79     self.flip + self.picture.filename
80   end
81   
82   def url
83     '/pictures/' + self.filename
84   end
85   
86   def opt_div_style
87     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
88   end
89   
90   def opt_img_tag spot = nil, opacity = 20
91     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
92     self.tag_attributes(:img, {
93       :class => "panel-picture", :vPicture => self.id, 
94       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
95       :style => "#{o}"
96     })
97   end
98   
99   def tmb_opt_img_tag
100     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
101     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
102   end
103   
104   def symbol_option
105     self.picture.symbol_option
106   end
107   
108   def self.list_where
109     'panels.publish > 0'
110   end
111   
112   def self.list_order
113     'panel_pictures.updated_at desc'
114   end
115   
116   def self.list_opt
117     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
118   end
119   
120   def self.list_json_opt
121     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
122   end
123   
124   def self.show_opt
125     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
126   end
127   
128   def self.show_json_opt
129     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
130   end
131   
132   def store operators
133     if self.new_record?
134       self.panel.panel_pictures.build(self.attributes)
135     else
136       self.panel.panel_pictures.each do |panel_picture|
137         next unless panel_picture == self
138         attr = self.attributes
139         attr.delete 'id'
140         panel_picture.attributes = attr
141         break
142       end
143     end
144     self.panel.store({}, operators)
145   end
146   
147   def remove operators
148     self.panel.remove_element(self, operators)
149   end
150   
151   def scenario
152     if caption.blank?
153       ''
154     else
155       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
156     end
157   end
158   
159   def plain_scenario
160     if caption.blank?
161       ''
162     else
163       self.caption + "\n"
164     end
165   end
166   
167 end