OSDN Git Service

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