X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fresource_picture.rb;h=521a071ca80a17dc0ea26b55b26d7f476d9a4d72;hb=0ead3a286b925e86845cf8211982a7985e522813;hp=935c3e2fd193ced22ad8df70139cd0b95a709e03;hpb=eb5aee94f87dd1a2c7ee7a5a6a0e88ae3e235166;p=pettanr%2Fpettanr.git diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index 935c3e2f..521a071c 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -1,9 +1,13 @@ #素材 -class ResourcePicture < ActiveRecord::Base +class ResourcePicture < Peta::Content + load_manifest belongs_to :artist + belongs_to :license_group belongs_to :license + belongs_to :system_picture belongs_to :picture belongs_to :original_picture + has_many :resource_picture_pictures validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']} validates :width, :presence => true, :numericality => true, :natural_number => true @@ -12,18 +16,49 @@ class ResourcePicture < ActiveRecord::Base validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32} validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false} validates :license_id, :presence => true, :numericality => true, :existence => {:both => false} + validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false} validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false} - validates :artist_name, :presence => true - validates :classname, :presence => true, :length => {:maximum => 50} + validates :license_group_module_name, :presence => true, :length => {:maximum => 50} validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false} + validates :license_settings, :boost => {:boost_name => :license} + validates :license_group_settings, :boost => {:boost_name => :license_group} + validates :credit_picture_settings, :boost => {:boost_name => :credit_picture} - before_validation :valid_encode + scope :find_index, -> do + self.all + end - def valid_encode - ['artist_name', 'classname', 'credit', 'settings'].each do |a| - next if attributes[a] == nil - raise Pettanr::BadRequest unless attributes[a].valid_encoding? - end + scope :find_private, -> (operators) do + where(artist_id: operators.artist.id) + end + + scope :find_by_original_picture, -> (original_picture_id) do + find_index.where(original_picture_id: original_picture_id) + end + + scope :find_by_license_group, -> (license_group_id) do + find_index.where(license_group_id: license_group_id) + end + + scope :find_by_license, -> (license_id) do + find_index.where(license_id: license_id) + end + + scope :find_by_artist, -> (artist_id) do + find_index.where(artist_id: artist_id) + end + + def self.pickup_item_name + Picture.item_name + end + + def self.pickup_column_name + self.pickup_item_name + '_id' + end + + def pickup_id + # get head picture + self.attributes[self.pickup_column_name] end def supply_default @@ -31,25 +66,16 @@ class ResourcePicture < ActiveRecord::Base def overwrite op attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, - :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5 + :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5, + :created_at => Time.now, :updated_at => Time.now } self.attributes = attr end - def own? roles - roles = [roles] unless roles.respond_to?(:each) - ar = ResourcePicture.get_artist_from_roles roles - return false unless ar - self.artist_id == ar.id - end - - def visible? roles - if MagicNumber['run_mode'] == 0 - return false unless guest_role_check(roles) - else - return false unless resource_reader_role_check(roles) - end - true + def visible? operators + # no super + # content model call to owner checker + self.user_visible? operators end def filename @@ -65,15 +91,15 @@ class ResourcePicture < ActiveRecord::Base end def url subdir = nil - '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename + '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s) end def to_gif? - self.ext == 'png' and self.flag_gif_convert >= 0 + self.ext == 'png' and self.license_extend.gif_convert >= 0 end def thumbnail(imager) - tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows) + tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows) ResourcePicture.resize(rimg.to_blob, tw, th).to_blob end @@ -86,85 +112,12 @@ class ResourcePicture < ActiveRecord::Base {:src => self.url('full'), :width => self.width, :height => self.height} end - def symbol_option - self.tmb_opt_img_tag - 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.mylist_where ar - ['resource_pictures.artist_id = ?', ar.id] - end - - def self.himlist_where ar - ['resource_pictures.artist_id = ?', ar.id] - end - - def self.list page = 1, page_size = self.default_page_size - ResourcePicture.includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size - ResourcePicture.where(self.mylist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.himlist ar, page = 1, page_size = Author.default_resource_picture_page_size - ResourcePicture.where(self.himlist_where(ar)).includes(ResourcePicture.list_opt).order('resource_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(ResourcePicture.count, nil)).page(page).per(page_size) - end - - def self.mylist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size - Kaminari.paginate_array(Array.new(ResourcePicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size) - end - - def self.himlist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size - Kaminari.paginate_array(Array.new(ResourcePicture.where(self.himlist_where(ar)).count, nil)).page(page).per(page_size) - end - - def self.list_by_original_picture_where original_picture_id - ['resource_pictures.original_picture_id = ?', original_picture_id] + def alt_name + self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']' end - def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size - self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.list_by_license_where license_id - ['resource_pictures.license_id = ?', license_id] - end - - def self.list_by_license license_id, roles, page = 1, page_size = self.default_page_size - self.where(self.list_by_license_where(license_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) - end - - def self.list_by_artist_where artist_id - ['resource_pictures.artist_id = ?', artist_id] - end - - def self.list_by_artist artist_id, roles, page = 1, page_size = self.default_page_size - self.where(self.list_by_artist_where(artist_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) + def symbol_option + self.tmb_opt_img_tag end def self.list_opt @@ -175,14 +128,6 @@ class ResourcePicture < ActiveRecord::Base {:include => {:license => {}, :artist => {}, :picture => {}} } end - def self.show rid, roles - opt = {} - opt.merge!(self.show_opt) - r = ResourcePicture.find(rid, opt) - raise ActiveRecord::Forbidden unless r.visible?(roles) - r - end - def self.show_opt {:include => {:license => {}, :artist => {}, :picture => {}} } end @@ -191,18 +136,11 @@ class ResourcePicture < ActiveRecord::Base {:include => {:license => {}, :artist => {}, :picture => {}} } end - def self.edit rid, ar - opt = {} - opt.merge!(self.show_opt) - r = ResourcePicture.find(rid, opt) - raise ActiveRecord::Forbidden unless r.own?(ar) - r - end - def new_picture imager pc = Picture.new pc.supply_default pc.overwrite self + pc.boosts 'post' r = pc.store imager return pc if r self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create') @@ -220,7 +158,17 @@ class ResourcePicture < ActiveRecord::Base pc = self.new_picture imager if pc self.picture_id = pc.id + resource_picture_picture = ResourcePicturePicture.new( + :original_picture_id => self.original_picture_id, + :resource_picture_id => self.id, + :picture_id => pc.id + ) + raise ActiveRecord::Rollback unless resource_picture_picture.save if res = self.save + self.original_picture.resource_picture_pictures.each do |resource_picture_picture| + resource_picture_picture.resource_picture_id = self.id + raise ActiveRecord::Rollback unless resource_picture_picture.save + end res = self.store_picture_with_gif(imager) end else @@ -251,7 +199,7 @@ class ResourcePicture < ActiveRecord::Base def store_picture(imager, fn) res = false - thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager + thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager return false unless thumbnail_imager begin PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn) @@ -273,6 +221,10 @@ class ResourcePicture < ActiveRecord::Base self.original_picture.published_at = nil self.original_picture.stopped_at = Time.now raise ActiveRecord::Rollback unless self.original_picture.save + self.original_picture.resource_picture_pictures.each do |resource_picture_picture| + resource_picture_picture.resource_picture_id = nil + raise ActiveRecord::Rollback unless resource_picture_picture.save + end begin PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename) PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full') @@ -294,87 +246,25 @@ class ResourcePicture < ActiveRecord::Base Base64.encode64(self.restore 'full') end - def credit_template - "#{self.classname.tableize}/attributes/credit" + def new_template + "#{self.license_group_module_name.tableize}/attributes/new" end - def full_credit_template - "#{self.classname.tableize}/attributes/full_credit" + def credit_template + "#{self.license_group_module_name.tableize}/attributes/credit" end - def credit_data - begin - @credit_data = JSON.parse(self.credit) unless @credit_data - rescue - end - @credit_data + def full_credit_template + "#{self.license_group_module_name.tableize}/attributes/full_credit" end - def flags - begin - @flags = JSON.parse(self.settings) unless @flags - rescue + def self.remake_all + ResourcePicture.find_each do |resource_picture| + resource_picture.boosts 'post' + full = resource_picture.restore 'full' + imager = PettanImager.load full + resource_picture.store_picture_with_gif(imager) end - @flags - end - - def flags=(s) - @flags = s - end - - def flag_open - @flag_open = flags["open"] unless @flag_open - @flag_open - end - - def flag_commercial - @flag_commercial = flags["commercial"] unless @flag_commercial - @flag_commercial - end - - def flag_official - @flag_official = flags["official"] unless @flag_official - @flag_official - end - - def flag_attribution - @flag_attribution = flags["attribution"] unless @flag_attribution - @flag_attribution - end - - def flag_derive - @flag_derive = flags["derive"] unless @flag_derive - @flag_derive - end - - def flag_thumbnail - @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail - @flag_thumbnail - end - - def flag_gif_convert - @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert - @flag_gif_convert - end - - def flag_reverse - @flag_reverse = flags["reverse"] unless @flag_reverse - @flag_reverse - end - - def flag_resize - @flag_resize = flags["resize"] unless @flag_resize - @flag_resize - end - - def flag_sync_vh - @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh - @flag_sync_vh - end - - def flag_overlap - @flag_overlap = flags["overlap"] unless @flag_overlap - @flag_overlap end end