X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fpanel_picture.rb;h=4c463d9a952183ec8f8c389bf755d2966bbe7969;hb=a8af764be9b48ad0bf9eaca3e90f48af2c62ee3c;hp=b93c66707c5cf9abb559a62484af337ba0ddeff3;hpb=f207b70a40512fa3727ba924da27f19b928d680a;p=pettanr%2Fpettanr.git diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index b93c6670..4c463d9a 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -1,72 +1,178 @@ -class PanelPicture < ActiveRecord::Base +class PanelPicture < Peta::Element + load_manifest + include ElementInspire belongs_to :panel - belongs_to :resource_picture + belongs_to :picture validates :panel_id, :numericality => {:allow_blank => true} - validates :resource_picture_id, :presence => true, :numericality => true, :existence => true - validates :link, :length => {:maximum => 200} + validates :picture_id, :numericality => true, :existence => {:both => false} + 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 - validates :height, :presence => true, :numericality => true, :not_zero => 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 :z, :presence => true, :numericality => {:greater_than => 0} validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} + def self.colum_structures + @@colum_structures ||= { + :width => { + :helper => 'panels/size_helper' + }, + :height => { + :helper => 'panels/size_helper' + } + } + end + + def self.list_opt_for_panel + { + :panel_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.show_opt_for_panel + { + :panel_pictures => { + :picture => {:artist => {}, :license => {}} + } + } + end + + def self.json_opt_for_panel + { + :picture => {:artist => {}, :license => {}} + } + end + + def self.has_picture? + true + end + + def visible? operators + return false unless super + self.owner_model.visible? operators + end + + 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 overwrite pid + self.panel_id = pid + end + def flip - res = (self.height < 0 ? '' : 'v') + (self.width == 0 ? '' : 'h') + res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h') res += '/' unless res.empty? res end def filename - flip + self.resource_picture.filename + self.flip + self.picture.filename end def url - '/resource_pictures/' + filename + '/pictures/' + self.filename end - def self.default_page_size - 25 + def opt_div_style + "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;" end - def self.max_page_size - 100 + 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 self.page prm = nil - page = prm.to_i - page = 1 if page < 1 - page + 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 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 symbol_option + self.picture.symbol_option end - def self.offset cnt, prm = nil - offset = prm.to_i - offset = cnt - 1 if offset >= cnt - offset = cnt - offset.abs if offset < 0 - offset = 0 if offset < 0 - offset + def boost end - def self.list opt = {}, page = 1, page_size = self.default_page_size - opt.merge!(self.list_opt) unless opt[:include] - opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}) - PanelPicture.find(:all, opt) + def tag_element_type + 'panel_picture' + end + + def self.list_where + 'panels.publish > 0' + end + + def self.list_order + 'panel_pictures.updated_at desc' end def self.list_opt - {:include => [:panel, {:resource_picture => [:artist, :license]}]} + {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} } end def self.list_json_opt - {:include => {:panel => {}, :resource_picture => [:artist, :license]}} + {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }} + 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 operators + 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({}, operators) + end + + def remove operators + self.panel.remove_element(self, operators) + 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