Module: Papermill::ClassMethods
- Defined in:
- lib/papermill/papermill_module.rb
Instance Attribute Summary collapse
-
#papermill_associations ⇒ Object
readonly
Returns the value of attribute papermill_associations.
Instance Method Summary collapse
- #inherited(subclass) ⇒ Object
-
#papermill(assoc_name = :papermill_assets, options = {}) ⇒ Object
papermill comes in 2 flavors:.
Instance Attribute Details
#papermill_associations ⇒ Object (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:
-
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)
-
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, = {}) if assoc_name.is_a? Hash = assoc_name assoc_name = :papermill_assets end @papermill_associations ||= {} begin class_name = .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()}}) before_destroy :destroy_assets after_create :rebase_assets define_method assoc_name do |*| klass = self.class.papermill_associations[assoc_name.to_sym][:class] = .first || {} if (.is_a?(Symbol) || .is_a?(String)) key = = {} else key = nil end conditions = { :assetable_type => self.class.sti_name, :assetable_id => self.id }.merge(.delete(:conditions) || {}) key ||= (assoc_name != :papermill_assets) && assoc_name.to_s conditions.merge!({:assetable_key => key.to_s}) if key hash = { :conditions => conditions, :order => .delete(:order) || "position ASC" }.merge() asset_class.find(:all, hash) end class_eval <<-EOV include Papermill::InstanceMethods EOV end |