Module: Papermill::ClassMethods

Defined in:
lib/papermill/papermill_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#papermill_associationsObject (readonly)

Returns the value of attribute papermill_associations.



86
87
88
# File 'lib/papermill/papermill_module.rb', line 86

def papermill_associations
  @papermill_associations
end

Instance Method Details

#inherited(subclass) ⇒ Object



147
148
149
150
# File 'lib/papermill/papermill_module.rb', line 147

def inherited(subclass)
  subclass.instance_variable_set("@papermill_associations", @papermill_associations)
  super
end

#papermill(assoc_name = :papermill_assets, options = {}) ⇒ Object

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)
  1. 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

In both case, you can specify a PapermillAsset subclass to use with :class_name => MyPapermillAssetSubclass in the option hash



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/papermill/papermill_module.rb', line 101

def papermill(assoc_name = :papermill_assets, options = {})
  if assoc_name.is_a? Hash
    options = assoc_name
    assoc_name = :papermill_assets
  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")
  end

  @papermill_associations.merge!({assoc_name => {: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
    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
  
  class_eval <<-EOV
    include Papermill::InstanceMethods
  EOV
end