class Dragonfly::Model::Attachment
Attributes
app[R]
attribute[R]
config_block[R]
default_path[R]
model_class[R]
changed[W]
job[RW]
model[R]
previous_uid[RW]
retained[W]
should_retain[W]
should_run_callbacks[W]
uid[R]
Public Class Methods
allowed_magic_attributes()
click to toggle source
Magic attributes
# File lib/dragonfly/model/attachment_class_methods.rb, line 93 def allowed_magic_attributes app.analyser_methods + [:size, :name] end
callbacks()
click to toggle source
Callbacks
# File lib/dragonfly/model/attachment_class_methods.rb, line 77 def callbacks @callbacks ||= Hash.new{|h,k| h[k] = [] } end
default_job()
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 72 def default_job app.fetch_file(default_path) if default_path end
default_path=(path)
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 67 def default_path=(path) @default_path = path app.fetch_file_whitelist.push(path) end
ensure_uses_cached_magic_attributes()
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 110 def ensure_uses_cached_magic_attributes return if @uses_cached_magic_attributes magic_attributes.each do |attr| define_method attr do magic_attribute_for(attr) end end @uses_cached_magic_attributes = true end
evaluate_storage_options(model, attachment)
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 125 def evaluate_storage_options(model, attachment) storage_options_specs.inject({}) do |opts, spec| options = case spec when Proc then model.instance_exec(attachment, &spec) when Symbol meth = model.method(spec) (1 === meth.arity) ? meth.call(attachment) : meth.call else spec end opts.merge!(options) opts end end
init(model_class, attribute, app, config_block)
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 58 def init(model_class, attribute, app, config_block) @model_class, @attribute, @app, @config_block = model_class, attribute, app, config_block include app.job_methods ConfigProxy.new(self, config_block) if config_block self end
magic_attributes()
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 97 def magic_attributes @magic_attributes ||= begin prefix = attribute.to_s + '_' model_class.public_instance_methods.inject([]) do |attrs, name| _, __, suffix = name.to_s.partition(prefix) if !suffix.empty? && allowed_magic_attributes.include?(suffix.to_sym) attrs << suffix.to_sym end attrs end end end
new(model)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 26 def initialize(model) @model = model self.uid = model_uid set_job_from_uid if uid? @should_run_callbacks = true self.class.ensure_uses_cached_magic_attributes end
run_callbacks(name, model, attachment)
click to toggle source
# File lib/dragonfly/model/attachment_class_methods.rb, line 81 def run_callbacks(name, model, attachment) attachment.should_run_callbacks = false callbacks[name].each do |callback| case callback when Symbol then model.send(callback) when Proc then model.instance_exec(attachment, &callback) end end attachment.should_run_callbacks = true end
storage_options_specs()
click to toggle source
Storage options
# File lib/dragonfly/model/attachment_class_methods.rb, line 121 def storage_options_specs @storage_options_specs ||= [] end
Public Instance Methods
add_meta(meta)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 158 def add_meta(meta) job.add_meta(meta) self end
app()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 34 def app self.class.app end
apply()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 101 def apply job.apply self end
assign(value)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 42 def assign(value) self.changed = true destroy_retained! if retained? set_uid_and_model_uid(nil) if value.nil? self.job = nil reset_magic_attributes self.class.run_callbacks(:after_unassign, model, self) if should_run_callbacks? else self.job = app.new_job(value) set_magic_attributes job.update_url_attributes(magic_attributes_hash) meta.merge!(standard_meta_attributes) self.class.run_callbacks(:after_assign, model, self) if should_run_callbacks? retain! if should_retain? end model_uid_will_change! value end
attribute()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 38 def attribute self.class.attribute end
changed?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 62 def changed? !!@changed end
destroy!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 70 def destroy! destroy_previous! destroy_content(uid) if uid? end
destroy_retained!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 131 def destroy_retained! destroy_content(retained_attrs['uid']) end
inspect()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 163 def inspect "<Dragonfly Attachment uid=#{uid.inspect}, app=#{app.name.inspect}>" end
name=(name)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 87 def name=(name) set_magic_attribute(:name, name) if has_magic_attribute_for?(:name) job.name = name end
process!(*args)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 92 def process!(*args) assign(process(*args)) self end
remote_url(opts={})
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 97 def remote_url(opts={}) app.remote_url_for(uid, opts) if uid? end
retain!()
click to toggle source
Retaining for avoiding uploading more than once
# File lib/dragonfly/model/attachment.rb, line 114 def retain! if changed? && job store_job! self.retained = true end end
retained?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 127 def retained? !!@retained end
retained_attrs()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 135 def retained_attrs attribute_keys.inject({}) do |hash, key| hash[key] = send(key) hash end if retained? end
retained_attrs=(attrs)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 142 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| unless attribute_keys.include?(key) raise BadAssignmentKey, "trying to call #{attribute}_#{key} = #{value.inspect} via retained_#{attribute} but this is not allowed!" end model.send("#{attribute}_#{key}=", value) end sync_with_model set_job_from_uid self.retained = true end end
save!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 75 def save! sync_with_model store_job! if job && !uid destroy_previous! self.changed = false self.retained = false end
should_retain?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 123 def should_retain? !!@should_retain end
should_run_callbacks?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 108 def should_run_callbacks? !!@should_run_callbacks end
stored?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 66 def stored? uid? end
to_value()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 83 def to_value (self if job) || self.class.default_job end
Private Instance Methods
attribute_keys()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 173 def attribute_keys @attribute_keys ||= ['uid'] + magic_attributes.map{|attribute| attribute.to_s } end
destroy_content(uid)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 183 def destroy_content(uid) app.datastore.destroy(uid) end
destroy_previous!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 187 def destroy_previous! if previous_uid? destroy_content(previous_uid) self.previous_uid = nil end end
has_magic_attribute_for?(property)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 260 def has_magic_attribute_for?(property) magic_attributes.include?(property.to_sym) end
magic_attribute_for(property)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 264 def magic_attribute_for(property) model.send("#{attribute}_#{property}") end
magic_attributes()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 236 def magic_attributes self.class.magic_attributes end
magic_attributes_hash()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 268 def magic_attributes_hash magic_attributes.inject({}) do |attrs, property| attrs[property.to_s] = model.send("#{attribute}_#{property}") attrs end end
model_uid()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 210 def model_uid model.send("#{attribute}_uid") end
model_uid=(uid)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 206 def model_uid=(uid) model.send("#{attribute}_uid=", uid) end
model_uid_will_change!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 214 def model_uid_will_change! meth = "#{attribute}_uid_will_change!" model.send(meth) if model.respond_to?(meth) end
previous_uid?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 232 def previous_uid? !previous_uid.nil? && !previous_uid.empty? end
reset_magic_attributes()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 256 def reset_magic_attributes magic_attributes.each{|property| set_magic_attribute(property, nil) } end
set_job_from_uid()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 282 def set_job_from_uid self.job = app.fetch(uid) job.update_url_attributes(magic_attributes_hash) end
set_magic_attribute(property, value)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 240 def set_magic_attribute(property, value) model.send("#{attribute}_#{property}=", value) end
set_magic_attributes()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 244 def set_magic_attributes magic_attributes.each do |property| value = begin job.send(property) rescue RuntimeError => e Dragonfly.warn("setting magic attribute for #{property} to nil in #{self.inspect} because got error #{e}") nil end set_magic_attribute(property, value) end end
set_uid_and_model_uid(uid)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 201 def set_uid_and_model_uid(uid) self.uid = uid self.model_uid = uid end
standard_meta_attributes()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 275 def standard_meta_attributes @standard_meta_attributes ||= { 'model_class' => model.class.name, 'model_attachment' => attribute.to_s } end
store_job!()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 177 def store_job! opts = self.class.evaluate_storage_options(model, self) set_uid_and_model_uid job.store(opts) self.job = job.to_fetched_job(uid) end
sync_with_model()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 194 def sync_with_model # If the model uid has been set manually if uid != model_uid self.uid = model_uid end end
uid=(uid)
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 223 def uid=(uid) self.previous_uid = @uid if @uid @uid = uid end
uid?()
click to toggle source
# File lib/dragonfly/model/attachment.rb, line 228 def uid? !uid.nil? && !uid.empty? end