X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fartist.rb;h=6c97a5e60d597245f404dd4b984764b374faea0e;hb=0ead3a286b925e86845cf8211982a7985e522813;hp=56ae09ae22cbaa0483bc7206238a6d18fcfb2a69;hpb=e994abdaf74bc9d7c62d28f4e5a2caaffbc2fb48;p=pettanr%2Fpettanr.git diff --git a/app/models/artist.rb b/app/models/artist.rb index 56ae09ae..6c97a5e6 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -1,105 +1,39 @@ -class Artist < ActiveRecord::Base - belongs_to :author +class Artist < Peta::Owner + load_manifest + belongs_to :user has_many :original_pictures has_many :pictures has_many :resource_pictures validates :name, :presence => true, :length => {:maximum => 30} - validates :author_id, :numericality => {:allow_blank => true} + validates :user_id, :numericality => true, :existence => {:both => false} + validates :provider_id, :numericality => {:allow_nil => true} - def supply_default - self.name = 'no name' if self.name.blank? - end - - def overwrite au - return false unless au - self.author_id = au.id + scope :find_index, -> do + self end - def own? author - return false unless author - self.author_id == author.id - end - - def visible? author - true + def supply_default + self.name = 'no name' if self.name.blank? + self.user_id = nil end def self.find_by_author author - Artist.find( :first, :conditions => ['author_id = ?', author.id]) - 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 + Artist.where(author_id: author.id).first 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.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 - end - - def self.list page = 1, page_size = self.default_page_size - opt = {} - opt.merge!(Artist.list_opt) - opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0 - opt.merge!({:order => 'created_at desc'}) - Artist.find(:all, opt) - end - - def self.list_opt - {:include => {:author => {}} } - end - - def self.list_json_opt - {:include => {:author => {}} } - end - - def self.show aid, au - opt = {} - opt.merge!(Artist.show_opt) - res = Artist.find(aid, opt) - raise ActiveRecord::Forbidden unless res.visible?(au) - res - end - - def self.edit sid, au - opt = {} - opt.merge!(Artist.show_opt) - res = Artist.find sid, opt - raise ActiveRecord::Forbidden unless res.own?(au) - res + def self.index_list_where list + 'artists.provider_id is null' end def self.show_opt - {:include => {:author => {}} } - end - - def self.show_json_opt - {:include => {:author => {}} } + {:include => {:user => {:author => {}}} } end - def self.visible_count - Artist.count + def self.export(dt = nil) + artists = Artist.where('artists.provider = 0') + artists = artists.where(['artists.updated_at >= ?', dt]) if dt + artists.order(:id) end end