OSDN Git Service

fix root json
[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, :sync_vh => true
12   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => 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.has_picture?
25     true
26   end
27   
28   def supply_default
29     self.x = 0
30     self.y = 0
31     if self.picture
32       self.width = self.picture.width 
33       self.height = self.picture.height 
34     end
35     if self.panel
36       self.t = self.panel.new_t 
37       self.z = self.panel.new_z 
38     end
39   end
40   
41   def overwrite  pid
42     self.panel_id = pid
43   end
44   
45   def flip
46     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
47     res = res # format of /1.png?subdir=v
48     # res += '/' unless res.empty? # format of /v/1.png
49     res
50   end
51   
52   def filename
53     self.picture.filename + '?subdir=' + self.flip
54   end
55   
56   def url
57     '/pictures/' + self.filename
58   end
59   
60   def opt_div_style
61     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
62   end
63   
64   def opt_img_tag spot = nil, opacity = 20
65     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
66     self.tag_attributes(:img, {
67       :class => "panel-picture", :vPicture => self.id, 
68       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
69       :style => "#{o}"
70     })
71   end
72   
73   def tmb_opt_img_tag
74     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
75     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
76   end
77   
78   def symbol_option
79     self.picture.symbol_option
80   end
81   
82   def self.public_list_where
83     'panels.publish > 0'
84   end
85   
86   def self.show_opt
87     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
88   end
89   
90   def scenario
91     if caption.blank?
92       ''
93     else
94       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
95     end
96   end
97   
98   def plain_scenario
99     if caption.blank?
100       ''
101     else
102       self.caption + "\n"
103     end
104   end
105   
106 end