X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=app%2Fmodels%2Fpanel_picture.rb;h=2406f778c1040909ecc9f84b4cf3424b299124c4;hp=36e7699c51e94841c24fde869ce1186e3510d54d;hb=9edc3731389bcae0de05ef6045cceb6b7581cc8f;hpb=6e65d291704afd169130b76c6345c5cd40ff9792 diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index 36e7699c..2406f778 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -1,47 +1,61 @@ -class PanelPicture < ActiveRecord::Base - include Element - include ElementInspire +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 => {:both => false} - validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')} + 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} - before_validation :valid_encode + scope :with_panel, -> do + includes(:panel) + end - def valid_encode - ['link', 'caption'].each do |a| - next if attributes[a] == nil - raise Pettanr::BadRequest unless attributes[a].valid_encoding? - end + scope :find_index, -> do + with_panel.where(Panel.arel_table[:publish].gt 0).references(:panel) end - def self.list_opt_for_panel - { - :panel_pictures => { - :picture => {:artist => {}, :license => {}} - } - } + scope :find_private, -> (operators) do + with_panel.where(Panel.arel_table[:author_id].eq operators.author.id).references(:panel) end - def self.show_opt_for_panel - { - :panel_pictures => { - :picture => {:artist => {}, :license => {}} - } - } + scope :find_by_panel, -> (panel_id) do + find_index.where(panel_id: panel_id).references(:panel) + end + + scope :find_by_author, -> (author_id) do + find_index.where(Panel.arel_table[:author_id].eq author_id).references(:panel) end - def self.json_opt_for_panel + def self.pickup_item_name + Picture.item_name + end + + def self.pickup_column_name + self.pickup_item_name + '_id' + end + + 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 { - :picture => {:artist => {}, :license => {}} + :panel => { + :author => {} + } } end @@ -49,16 +63,6 @@ class PanelPicture < ActiveRecord::Base true 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 - end - def supply_default self.x = 0 self.y = 0 @@ -78,12 +82,14 @@ class PanelPicture < ActiveRecord::Base 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 + q = self.flip.empty? ? '' : '?subdir=' + self.flip + self.picture.filename + q end def url @@ -96,10 +102,11 @@ class PanelPicture < ActiveRecord::Base def opt_img_tag spot = nil, opacity = 20 o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : '' - {:id => self.tag_id(:img), :panel_id => self.tag_panel_id, :element_id => self.tag_element_id, :element_type => self.tag_element_type, - :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}"} + 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 @@ -107,148 +114,24 @@ class PanelPicture < ActiveRecord::Base {:src => self.url, :width => tw, :height => th, :alt => self.caption} end - def new_index - @new_index - end - - def new_index= v - @new_index = v - end - - def new_panel - @new_panel - end - - def new_panel= v - @new_panel = v - end - - def get_panel - self.panel || @new_panel - end - - def tag_id c = nil - 'panel' + tag_panel_id + 'panel_picture' + tag_element_id + c.to_s - end - - def field_tag_id f - self.tag_id + f.to_s - end - - def tag_panel_id - self.get_panel.new_record? ? '0' : self.get_panel.id.to_s - end - - def tag_element_id - self.new_record? ? '0' : self.id.to_s + def symbol_option + self.picture.symbol_option end - def tag_element_type - 'panel_picture' - end - - def tag_new_index - self.new_index.to_s - end - - def self.default_page_size - 25 - 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 list '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) + def disp_link + if self.link =~ /\:\/\// + self.link 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 + '/' + self.link.to_s end - self.panel.store({}, au) - end - - def remove au - self.panel.remove_element(self, au) end def scenario