X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fpanel_picture.rb;h=2aa3affbd97a64ef0640c9f377e2916e84e464bd;hb=160cfe90adb0a44dd3cdc02962cb5326d2a11585;hp=59f04a9272f6adca5b87a138665b3745e85e81b5;hpb=bf89ae0a5c0f77ce2e9782b28023ff8ac521c501;p=pettanr%2Fpettanr.git diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index 59f04a92..2aa3affb 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -1,4 +1,5 @@ -class PanelPicture < ActiveRecord::Base +class PanelPicture < Peta::Element + load_manifest belongs_to :panel belongs_to :picture @@ -7,28 +8,38 @@ class PanelPicture < ActiveRecord::Base 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} - before_validation :valid_encode + def self.pickup_item_name + Picture.item_name + end - def valid_encode - ['link', 'caption'].each do |a| - next if attributes[a] == nil - raise Pettanr::BadRequest unless attributes[a].valid_encoding? - end + def self.pickup_column_name + self.pickup_item_name + '_id' end - def visible? roles - if MagicNumber['run_mode'] == 0 - return false unless guest_role_check(roles) - else - return false unless reader_role_check(roles) - end - return true if self.panel.own?(roles) - self.panel.visible? roles + def pickup_id + # get :picture_id if head revision + self.attributes[self.pickup_column_name] + end + + def y + self.attributes['y'] + end + + def self.by_author_list_includes + { + :panel => { + :author => {} + } + } + end + + def self.has_picture? + true end def supply_default @@ -44,26 +55,36 @@ class PanelPicture < ActiveRecord::Base end end - def overwrite + def overwrite pid + self.panel_id = pid end def flip res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h') - res += '/' unless res.empty? + res = res # format of /1.png?subdir=v + # res += '/' unless res.empty? # format of /v/1.png res end def filename - self.flip + self.picture.filename + self.picture.filename + '?subdir=' + self.flip 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});" : '' - {:src => self.url, :id => "panel-picture#{self.id}", :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}; #{o}"} + 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 @@ -71,121 +92,18 @@ class PanelPicture < ActiveRecord::Base {:src => self.url, :width => tw, :height => th, :alt => self.caption} end - def self.default_page_size - 25 + def symbol_option + self.picture.symbol_option end - def self.max_page_size - 100 - end - - def self.page prm = nil - page = prm.to_i - page = 1 if page < 1 - page - 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 - end - - def self.list_where + def self.public_list_where 'panels.publish > 0' end - def self.mylist_where au - ['panels.author_id = ?', au.id] - end - - def self.himlist_where au - ['panels.author_id = ? and panels.publish > 0', au.id] - end - - def self.list page = 1, page_size = self.default_page_size - PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size - PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size - PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.list_paginate page = 1, page_size = self.default_page_size - Kaminari.paginate_array(Array.new(PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size) - end - - def self.mylist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size - Kaminari.paginate_array(Array.new(PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size) - end - - def self.himlist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size - Kaminari.paginate_array(Array.new(PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size) - end - - def self.list_opt - {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} } - end - - def self.list_json_opt - {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }} - 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 - 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 => {}}} }} - end - - def store au - if self.new_record? - self.panel.panel_pictures.build(self.attributes) - else - self.panel.panel_pictures.each do |panel_picture| - next unless panel_picture == self - attr = self.attributes - attr.delete 'id' - panel_picture.attributes = attr - break - end - end - self.panel.store({}, au) - end - - def remove au - d = false - panel_pictures_attributes = {} - self.panel.panel_pictures.each do |panel_picture| - attr = panel_picture.attributes - if panel_picture == self - attr['_destroy'] = true - d = true - else - if d - attr['t'] -= 1 - end - end - panel_pictures_attributes[panel_picture.id] = attr - end - self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes} - self.panel.store({}, au) - end - def scenario if caption.blank? ''