lib/papermill/papermill.rb in papermill-1.4.3 vs lib/papermill/papermill.rb in papermill-2.0.0.pre
- old
+ new
@@ -1,71 +1,83 @@
module Papermill
+ MSWIN = (Config::CONFIG['host_os'] =~ /mswin|mingw/)
- def self.included(base)
- base.extend(ClassMethods)
- end
-
def self.options
@options ||= BASE_OPTIONS.deep_merge(defined?(OPTIONS) ? OPTIONS : {})
end
- MSWIN = (Config::CONFIG['host_os'] =~ /mswin|mingw/)
-
def self.compute_paperclip_path
path = []
path << (options[:use_id_partition] ? ":id_partition" : ":id")
path << (":url_key" if options[:use_url_key])
path << ":style"
path << ":basename.:extension"
path.compact.join("/")
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) )
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.class_eval do
+ def papermill(key, through = ((po = self.class.papermill_options) && (po[key.to_sym] || po[:default]) || Papermill::options)[:through])
+ PapermillAsset.papermill(self.class.base_class.name, self.id, key.to_s, through)
+ end
- include Papermill::InstanceMethods
- after_create :rebase_assets
- has_many :papermill_assets, :as => "assetable", :dependent => :destroy
-
- [assoc_name, Papermill::options[:base_association_name].to_sym].uniq.each do |assoc|
- define_method assoc do |*options|
- scope = PapermillAsset.scoped(:conditions => {:assetable_id => self.id, :assetable_type => self.class.base_class.name})
- if assoc != Papermill::options[:base_association_name]
- scope = scope.scoped(:conditions => { :assetable_key => assoc.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
+ def respond_to_with_papermill?(method, *args, &block)
+ respond_to_without_papermill?(method, *args, &block) || (method.to_s =~ /^papermill_.+_ids=$/) == 0
+ end
+
+ def method_missing_with_papermill(method, *args, &block)
+ if method.to_s =~ /^papermill_.+_ids=$/
+ self.class.papermill(method.to_s[10..-6])
+ self.send(method, *args, &block)
+ else
+ method_missing_without_papermill(method, *args, &block)
end
end
- ActionController::Dispatcher.middleware.delete(FlashSessionCookieMiddleware) rescue true
- ActionController::Dispatcher.middleware.insert_before(ActionController::Base.session_store, FlashSessionCookieMiddleware, ActionController::Base.session_options[:key]) rescue true
end
+ base.send :alias_method_chain, :method_missing, :papermill
+ base.send :alias_method_chain, :respond_to?, :papermill
+ ActionController::Dispatcher.middleware.insert_before(ActionController::Base.session_store, FlashSessionCookieMiddleware, ActionController::Base.session_options[:key]) unless ActionController::Dispatcher.middleware.include?(FlashSessionCookieMiddleware)
+ end
+
+ module ClassMethods
+ attr_reader :papermill_options
def inherited(subclass)
- subclass.instance_variable_set("@papermill_associations", @papermill_associations)
+ subclass.instance_variable_set("@papermill_options", @papermill_options)
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 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)
+ def papermill(assoc_key, assoc_options = (@papermill_options && @papermill_options[:default] || {}))
+ return if @papermill_options && @papermill_options[assoc_key.to_sym] # already defined
+ raise PapermillException.new("Can't use '#{assoc_key}' association : #{self.name} instances already responds to it !") if self.new.respond_to?(assoc_key)
+ (@papermill_options ||= {}).merge!( { assoc_key.to_sym => Papermill::options.deep_merge(assoc_options) } )
+ return if assoc_key.to_sym == :default
+ unless papermill_options[assoc_key.to_sym][:through]
+ self.class_eval %{
+ has_many :#{assoc_key}, :as => "assetable", :dependent => :delete_all, :order => :position, :class_name => "PapermillAsset", :conditions => {:assetable_key => '#{assoc_key}'}, :before_add => Proc.new{|a, asset| asset.assetable_key = '#{assoc_key}'}
+ def papermill_#{assoc_key}_ids=(ids)
+ unless (assets_ids = ids.map(&:to_i).select{|i|i>0}) == self.#{assoc_key}.map(&:id)
+ assets = PapermillAsset.find(assets_ids)
+ self.#{assoc_key} = assets_ids.map{|asset_id| assets.select{|asset|asset.id==asset_id}.first}
+ PapermillAsset.update_all("position = CASE id " + assets_ids.map_with_index{|asset_id, index| " WHEN " + asset_id.to_s + " THEN " + (index+1).to_s }.join + " END",
+ :id => assets_ids) unless assets_ids.empty?
+ end
+ end
+ }
+ else
+ self.class_eval %{
+ has_many(:#{assoc_key}_associations, :as => "assetable", :class_name => "PapermillAssociation", :include => :papermill_asset, :dependent => :delete_all, :order => :position, :conditions => {:assetable_key => '#{assoc_key}'}, :before_add => Proc.new{|a, assoc| assoc.assetable_key = '#{assoc_key}'})
+ has_many(:#{assoc_key}, :through => :#{assoc_key}_associations, :source => :papermill_asset)
+ def papermill_#{assoc_key}_ids=(ids)
+ unless (assets_ids = ids.map(&:to_i).select{|i|i>0}) == self.#{assoc_key}_associations.map(&:papermill_asset_id)
+ self.#{assoc_key}_associations.delete_all
+ self.#{assoc_key}_associations = assets_ids.map_with_index do |asset_id, index|
+ PapermillAssociation.new(:papermill_asset_id => asset_id, :position => (index+1))
+ end
+ end
+ end
+ }
end
end
end
end