OSDN Git Service

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