X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=app%2Fmodels%2Fpanel_picture.rb;h=2406f778c1040909ecc9f84b4cf3424b299124c4;hp=0dbcdc2c23bab25bfc1646455c47d39188bb210a;hb=9edc3731389bcae0de05ef6045cceb6b7581cc8f;hpb=0cd579131bd2cc5390baed66b827a5c99d25c27e diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index 0dbcdc2c..2406f778 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -1,111 +1,153 @@ -class PanelPicture < ActiveRecord::Base +class PanelPicture < Peta::Element + load_manifest belongs_to :panel belongs_to :picture validates :panel_id, :numericality => {:allow_blank => true} - validates :picture_id, :numericality => true, :existence => true - validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true} + validates :picture_id, :numericality => true, :existence => {:both => false} + validates :link, :length => {:maximum => 200}, :allow_blank => true + #validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')} validates :x, :presence => true, :numericality => true validates :y, :presence => true, :numericality => true - validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true - validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :resize => true, :sync_vh => true + validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true + validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true validates :z, :presence => true, :numericality => {:greater_than => 0} validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} - def visible? au - if au == nil - return false if MagicNumber['run_mode'] == 1 - elsif au.is_a?(Author) - elsif au.is_a?(Admin) - return true - else - return false - end - self.panel.publish? + scope :with_panel, -> do + includes(:panel) end - def flip - res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h') - res += '/' unless res.empty? - res + scope :find_index, -> do + with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel) end - def filename - self.flip + self.picture.filename + scope :find_private, -> (operators) do + with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel) end - def url - '/pictures/' + self.filename + scope :find_by_panel, -> (panel_id) do + find_index.where(panel_id: panel_id).references(:panel) end - def opt_img_tag - {:src => self.url, :vPicture => self.id, :width => self.width.abs, :height => self.height.abs, :alt => self.caption, :style => "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; "} + scope :find_by_author, -> (author_id) do + find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel) end - def tmb_opt_img_tag - tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs) - {:src => self.url, :width => tw, :height => th, :alt => self.caption} + def self.pickup_item_name + Picture.item_name end - def self.default_page_size - 25 + def self.pickup_column_name + self.pickup_item_name + '_id' end - def self.max_page_size - 100 + def pickup_id + # get :picture_id if head revision + self.attributes[self.pickup_column_name] end - def self.page prm = nil - page = prm.to_i - page = 1 if page < 1 - page + def y + self.attributes['y'] end - def self.page_size prm = self.default_page_size - page_size = prm.to_i - page_size = self.max_page_size if page_size > self.max_page_size - page_size = self.default_page_size if page_size < 1 - page_size + def self.by_author_list_includes + { + :panel => { + :author => {} + } + } end - def self.list page = 1, page_size = self.default_page_size - opt = {} - opt.merge!(PanelPicture.list_opt) - opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0 - opt.merge!({:conditions => 'panels.publish > 0', :order => 'panel_pictures.updated_at desc'}) - PanelPicture.find(:all, opt) + def self.has_picture? + true end - def self.list_opt - {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}} + def supply_default + self.x = 0 + self.y = 0 + if self.picture + self.width = self.picture.width + self.height = self.picture.height + end + if self.panel + self.t = self.panel.new_t + self.z = self.panel.new_z + end end - def self.list_json_opt - {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }} + def overwrite pid + self.panel_id = pid end - def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size - opt = {} - opt.merge!(PanelPicture.list_opt) - opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0 - opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panel_pictures.updated_at desc'}) - PanelPicture.find(:all, opt) + def flip + res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h') + res = res # format of /1.png?subdir=v + # res += '/' unless res.empty? # format of /v/1.png + res end - def self.show cid, au - opt = {} - opt.merge!(PanelPicture.show_opt) - res = PanelPicture.find(cid, opt) - raise ActiveRecord::Forbidden unless res.visible?(au) - res + def filename + q = self.flip.empty? ? '' : '?subdir=' + self.flip + self.picture.filename + q + end + + def url + '/pictures/' + self.filename + end + + def opt_div_style + "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;" + end + + def opt_img_tag spot = nil, opacity = 20 + o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : '' + self.tag_attributes(:img, { + :class => "panel-picture", :vPicture => self.id, + :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, + :style => "#{o}" + }) + end + + def tmb_opt_img_tag + tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs) + {:src => self.url, :width => tw, :height => th, :alt => self.caption} + end + + def symbol_option + self.picture.symbol_option + end + + def self.public_list_where list + 'panels.publish > 0' end def self.show_opt {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}} end - def self.show_json_opt - {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }} + def disp_link + if self.link =~ /\:\/\// + self.link + else + '/' + self.link.to_s + end + end + + def scenario + if caption.blank? + '' + else + '

' + ERB::Util.html_escape(self.caption) + '

' + end + end + + def plain_scenario + if caption.blank? + '' + else + self.caption + "\n" + end end end