lib/papermill/papermill_module.rb in papermill-0.7.0 vs lib/papermill/papermill_module.rb in papermill-0.8.0
- old
+ new
@@ -53,15 +53,16 @@
:button_text_left_padding => 4,
:debug => "false",
:prevent_swf_caching => "false"
# See swfupload.js for details.
},
- :images_only => false, # set to true to forbid upload of anything else than images
- :file_size_limit_mb => 10, # file max size
- :button_after_container => false, # set to true to move the upload button below the container
-
- # DO NOT CHANGE THESE IN YOUR CLASSES. Only application wide (routes depend on it..)
+ :images_only => false, # set to true to forbid upload of anything else than images
+ :file_size_limit_mb => 10, # file max size
+ :button_after_container => false, # set to true to move the upload button below the container
+
+ # DO NOT CHANGE THESE IN YOUR CLASSES. Only application wide (routes or associations may depend on it)
+ :base_association_name => 'assets',
:alias_only => false, # set to true so that only aliases are authorized in url/path
:aliases => {
# "example" => "100x100#",
},
# path to the root of your public directory
@@ -88,11 +89,11 @@
# papermill comes in 2 flavors:
#
# 1. generic declaration =>
# declare associations with => papermill {my_option_hash}
# create assets with => assets_upload(:my_key, {optional_option_hash})
- # access assets with => assetable.papermill_assets(:key => :my_key)
+ # access assets with => assetable.assets(:my_key)
#
# 2. association declaration =>
# declare associations with => papermill :my_association, {my_option_hash}
# create assets with => assets_upload(my_association, {optional_option_hash})
# access assets with => assetable.my_association
@@ -101,43 +102,82 @@
def papermill(assoc_name = :papermill_assets, options = {})
if assoc_name.is_a? Hash
options = assoc_name
assoc_name = :papermill_assets
end
-
+ base_association_name = PAPERMILL_DEFAULTS[:base_association_name]
+ if [base_association_name.to_s.singularize, base_association_name.to_s].include?(assoc_name.to_s)
+ raise PapermillException.new(
+ "':#{assoc_name.to_s}' and ':#{assoc_name.to_s.singularize}' are the default papermill associations name.\n" +
+ "You can take one of these actions: \n" +
+ "1. use another association name instead of ':#{assoc_name.to_s}'. Eg: 'papermill :my_lovely_assets {my_options}'\n" +
+ "2. change :base_association_name to something else than '#{base_association_name.to_s}' in the application-wide papermill option hash (change 'Papermill::OPTIONS[:base_association_name]' in your environment.rb)\n" +
+ "3. use a catch-all 'papermill {your_options}' declaration instead of 'papermill :#{assoc_name.to_s} {your_options}'\n\n" +
+ "If you want to take advantage of pluralized/singularized associations, always specify a singularizable name. Eg: 'my_assets' is ok, 'my_assix' is not ;). \n" +
+ "(Anyway you can add exceptions in your config/initializer/inflections.rb)\n\n\n")
+ end
@papermill_associations ||= {}
begin
class_name = options.delete(:class_name)
asset_class = class_name && class_name.to_s.constantize || PapermillAsset
- rescue
- raise Exception.new("Papermill: can't find class #{class_name.to_s}.\n#{class_name.to_s} should be a subclass of PapermillAsset")
+ rescue NameError
+ raise PapermillException.new("'#{class_name.to_s}' class doesn't exist.\n'#{class_name.to_s}' should be a subclass of PapermillAsset")
end
- @papermill_associations.merge!({assoc_name => {:class => asset_class, :options => Papermill::PAPERMILL_DEFAULTS.deep_merge(options)}})
+ @papermill_associations.merge!({assoc_name.to_sym => {:class => asset_class, :options => Papermill::PAPERMILL_DEFAULTS.deep_merge(options)}})
+
before_destroy :destroy_assets
after_create :rebase_assets
-
- define_method assoc_name do |*options|
- klass = self.class.papermill_associations[assoc_name.to_sym][:class]
- options = options.first || {}
- if (options.is_a?(Symbol) || options.is_a?(String))
- key = options
- options = {}
- else
- key = nil
+
+ # Defines for catch-all association :
+ # Assetable#assets(*options)
+ # Assetable#asset(*options)
+ unless self.respond_to?(base_association_name)
+ [base_association_name.to_s.singularize, base_association_name].each_with_index do |association, index|
+ define_method association do |*options|
+ # case Assetable#asset<s>(:key)
+ if (options.is_a?(Symbol) || options.is_a?(String))
+ key = options
+ options = {}
+ # case Assetable#asset<s>(:key, options = {})
+ elsif (options.first.is_a?(Symbol) || options.first.is_a?(String))
+ key = options.first
+ options = options[1..-1]
+ end
+ options = options.first || {}
+ conditions = {
+ :assetable_type => self.class.sti_name,
+ :assetable_id => self.id
+ }.merge(options.delete(:conditions) || {})
+ conditions.merge!({:assetable_key => key.to_s}) if key
+ hash = {
+ :conditions => conditions,
+ :order => options.delete(:order) || "position ASC"
+ }.merge(options)
+ PapermillAsset.find((index == 0 ? :first : :all), hash)
+ end
end
- conditions = {
- :assetable_type => self.class.sti_name,
- :assetable_id => self.id
- }.merge(options.delete(:conditions) || {})
- key ||= (assoc_name != :papermill_assets) && assoc_name.to_s
- conditions.merge!({:assetable_key => key.to_s}) if key
-
- hash = {
- :conditions => conditions,
- :order => options.delete(:order) || "position ASC"
- }.merge(options)
- asset_class.find(:all, hash)
+ end
+
+ # Defines for declared association :
+ # Assetable#assoc.singularize(*options)
+ # Assetable#assoc(*options)
+ unless assoc_name.to_sym == :papermill_assets
+ [assoc_name.to_s.singularize, assoc_name.to_s].each_with_index do |association, index|
+ define_method assoc_name do |*options|
+ options = options.first || {}
+ conditions = {
+ :assetable_type => self.class.sti_name,
+ :assetable_id => self.id,
+ :assetable_key => assoc_name.to_s
+ }.merge(options.delete(:conditions) || {})
+ hash = {
+ :conditions => conditions,
+ :order => options.delete(:order) || "position ASC"
+ }.merge(options)
+ asset_class.find((index == 0 ? :first : :all), hash)
+ end
+ end
end
class_eval <<-EOV
include Papermill::InstanceMethods
EOV