OSDN Git Service

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