lib/paperweight/hooks.rb in paperweight-1.0.2 vs lib/paperweight/hooks.rb in paperweight-1.1.0
- old
+ new
@@ -4,56 +4,32 @@
# Overrides that hook into `paperclip` to add functionality.
module Hooks
# Overrides the `has_attached_file` method from `paperclip` so that
# `paperweight` can add extras when the macro is called.
module RecordHook
- # Converts a parent name to its respective component child names.
- class AttachmentName
- attr_reader :name
-
- def initialize(name)
- @name = name
- end
-
- def processing
- :"#{name}_processing"
- end
-
- def updated_at
- :"#{name}_updated_at"
- end
-
- def url
- :"#{name}_url"
- end
-
- def url_eq
- :"#{name}_url="
- end
-
- def url_attr
- :"@#{name}_url"
- end
- end
-
- def has_attached_file(name, *) # rubocop:disable Naming/PredicateName
+ # rubocop:disable Naming/PredicateName
+ def has_attached_file(name, options = {})
+ after_download = options.delete(:after_download)
super
name = AttachmentName.new(name)
attr_reader name.url
define_paperweight_setter_for(name)
define_paperweight_after_commit_for(name)
+ define_method(name.after_download, &after_download) if after_download
end
+ # rubocop:enable Naming/PredicateName
private
def define_paperweight_setter_for(name)
define_method(name.url_eq) do |value|
instance_variable_set(name.url_attr, value)
self[name.processing] = value
return unless value
+
self[name.updated_at] = Time.now
self.updated_at = Time.now
end
end