Sha256: 3e0d205339b2568280aa67fba2c776b4c23574540559cabeb4990172a1a4ebcb

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

module Papermill
  PAPERCLIP_INTERPOLATION_STRING = ":id_partition/:style/:basename.:extension"
  
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  def self.options
    defined?(OPTIONS) ? OPTIONS : {}
  end
  

  module ClassMethods
    attr_reader :papermill_associations
    
    def papermill(*args)
      assoc_name = (!args.first.is_a?(Hash) && args.shift || Papermill::options[:base_association_name]).to_sym
      local_options = args.first || {}

      (@papermill_associations ||= {}).merge!( assoc_name => Papermill::options.deep_merge(local_options) )
      
      include Papermill::InstanceMethods
      before_destroy :destroy_assets
      after_create :rebase_assets
      has_many :papermill_assets, :as => "Assetable", :dependent => :destroy

      define_method assoc_name do |*options|
        scope = PapermillAsset.scoped(:conditions => {:assetable_id => self.id, :assetable_type => self.class.base_class.name})
        if assoc_name != Papermill::options[:base_association_name]
          scope = scope.scoped(:conditions => { :assetable_key => assoc_name.to_s })
        elsif options.first && !options.first.is_a?(Hash)
          scope = scope.scoped(:conditions => { :assetable_key => options.shift.to_s.nie })
        end
        scope = scope.scoped(options.shift) if options.first
        scope
      end
    end
    
    def inherited(subclass)
      subclass.instance_variable_set("@papermill_associations", @papermill_associations)
      super
    end
  end

  module InstanceMethods
    attr_writer :timestamp
    def timestamp
      @timestamp ||= "-#{(Time.now.to_f * 1000).to_i.to_s[4..-1]}"
    end
    
    private
    
    def destroy_assets
      papermill_assets.each &:destroy
    end
    
    def rebase_assets
      PapermillAsset.all(:conditions => { :assetable_id => self.timestamp, :assetable_type => self.class.base_class.name }).each do |asset|
        asset.created_at < 2.hours.ago ? asset.destroy : asset.update_attribute(:assetable_id, self.id)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
papermill-1.1.0 lib/papermill/papermill.rb
papermill-1.0.7 lib/papermill/papermill.rb
papermill-1.0.6 lib/papermill/papermill.rb