lib/dragonfly/active_model_extensions/attachment.rb in dragonfly-0.9.13 vs lib/dragonfly/active_model_extensions/attachment.rb in dragonfly-0.9.14

- old
+ new

@@ -9,32 +9,32 @@ # Exceptions class BadAssignmentKey < RuntimeError; end extend Forwardable def_delegators :job, - :data, :to_file, :file, :tempfile, :path, + :data, :b64_data, :to_file, :file, :tempfile, :path, :process, :encode, :analyse, :meta, :meta=, :name, :size, :url include HasFilename alias_method :length, :size - + def initialize(model) @model = model self.uid = model_uid - set_job_from_uid if uid + set_job_from_uid if uid? @should_run_callbacks = true self.class.ensure_uses_cached_magic_attributes end def app self.class.app end - + def attribute self.class.attribute end def assign(value) @@ -64,11 +64,11 @@ !!@changed end def destroy! destroy_previous! - destroy_content(uid) if uid + destroy_content(uid) if uid? end def save! sync_with_model store_job! if job && !uid @@ -93,22 +93,22 @@ def encode!(*args) assign(encode(*args)) self end - + def remote_url(opts={}) - app.remote_url_for(uid, opts) if uid + app.remote_url_for(uid, opts) if uid? end - + def apply job.apply self end attr_writer :should_run_callbacks - + def should_run_callbacks? !!@should_run_callbacks end # Retaining for avoiding uploading more than once @@ -119,30 +119,30 @@ self.retained = true end end attr_writer :should_retain - + def should_retain? !!@should_retain end - + def retained? !!@retained end - + def destroy_retained! destroy_content(retained_attrs[:uid]) end - + def retained_attrs attribute_keys.inject({}) do |hash, key| hash[key] = send(key) hash end if retained? end - + def retained_attrs=(attrs) if changed? # if already set, ignore and destroy this retained content destroy_content(attrs[:uid]) else attrs.each do |key, value| @@ -154,11 +154,11 @@ sync_with_model set_job_from_uid self.retained = true end end - + def inspect "<Dragonfly Attachment uid=#{uid.inspect}, app=#{app.name.inspect}>" end protected @@ -185,11 +185,11 @@ rescue DataStorage::DataNotFound, DataStorage::DestroyError => e app.log.warn("*** WARNING ***: tried to destroy data with uid #{uid}, but got error: #{e}") end def destroy_previous! - if previous_uid + if previous_uid? destroy_content(previous_uid) self.previous_uid = nil end end @@ -215,20 +215,28 @@ def model_uid_will_change! meth = "#{attribute}_uid_will_change!" model.send(meth) if model.respond_to?(meth) end - + attr_reader :model, :uid attr_writer :job attr_accessor :previous_uid def uid=(uid) self.previous_uid = @uid if @uid @uid = uid end + def uid? + !uid.nil? && !uid.empty? + end + + def previous_uid? + !previous_uid.nil? && !previous_uid.empty? + end + def magic_attributes self.class.magic_attributes end def set_magic_attribute(property, value) @@ -262,18 +270,18 @@ @extra_attributes ||= { :model_class => model.class.name, :model_attachment => attribute } end - + def all_extra_attributes magic_attributes_hash.merge(extra_attributes) end - + def set_job_from_uid self.job = app.fetch(uid) job.url_attrs = all_extra_attributes end end end -end \ No newline at end of file +end