Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

Dragonfly::ActiveModelExtensions::Attachment

Attributes

app[R]
attribute[R]
config_block[R]
model_class[R]
job[RW]
should_retain[W]
should_run_callbacks[W]

Public Class Methods

allowed_magic_attributes() click to toggle source

Magic attributes

# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 97
def allowed_magic_attributes
  app.analyser.analysis_method_names + [:size, :name]
end
callbacks() click to toggle source

Callbacks

# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 81
def callbacks
  @callbacks ||= Hash.new{|h,k| h[k] = [] }
end
ensure_uses_cached_magic_attributes() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 114
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_opts(model, attachment) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 129
def evaluate_storage_opts(model, attachment)
  storage_opts_specs.inject({}) do |opts, spec|
    options = case spec
    when Proc then model.instance_exec(attachment, &spec)
    when Symbol then model.send(spec)
    else spec
    end
    opts.merge!(options)
    opts
  end
end
init(model_class, attribute, app, config_block) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 64
def init(model_class, attribute, app, config_block)
  @model_class, @attribute, @app, @config_block = model_class, attribute, app, config_block
  include app.analyser.analysis_methods
  include app.job_definitions
  define_method :format do
    job.format
  end
  define_method :mime_type do
    job.mime_type
  end
  ConfigProxy.new(self, config_block) if config_block
  self
end
magic_attributes() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 101
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/active_model_extensions/attachment.rb, line 24
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/active_model_extensions/attachment_class_methods.rb, line 85
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_opts_specs() click to toggle source

Storage options

# File lib/dragonfly/active_model_extensions/attachment_class_methods.rb, line 125
def storage_opts_specs
  @storage_opts_specs ||= []
end

Public Instance Methods

app() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 32
def app
  self.class.app
end
apply() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 103
def apply
  job.apply
  self
end
assign(value) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 40
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 = case value
    when Job then value.dup
    when self.class then value.job.dup
    else app.new_job(value)
    end
    set_magic_attributes
    job.url_attrs = all_extra_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/active_model_extensions/attachment.rb, line 36
def attribute
  self.class.attribute
end
changed?() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 63
def changed?
  !!@changed
end
destroy!() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 67
def destroy!
  destroy_previous!
  destroy_content(uid) if uid?
end
destroy_retained!() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 133
def destroy_retained!
  destroy_content(retained_attrs[:uid])
end
encode!(*args) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 94
def encode!(*args)
  assign(encode(*args))
  self
end
inspect() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 160
def inspect
  "<Dragonfly Attachment uid=#{uid.inspect}, app=#{app.name.inspect}>"
end
name=(name) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 84
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/active_model_extensions/attachment.rb, line 89
def process!(*args)
  assign(process(*args))
  self
end
remote_url(opts={}) click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 99
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/active_model_extensions/attachment.rb, line 116
def retain!
  if changed? && job
    store_job!
    self.retained = true
  end
end
retained?() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 129
def retained?
  !!@retained
end
retained_attrs() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 137
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/active_model_extensions/attachment.rb, line 144
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/active_model_extensions/attachment.rb, line 72
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/active_model_extensions/attachment.rb, line 125
def should_retain?
  !!@should_retain
end
should_run_callbacks?() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 110
def should_run_callbacks?
  !!@should_run_callbacks
end
to_value() click to toggle source
# File lib/dragonfly/active_model_extensions/attachment.rb, line 80
def to_value
  self if job
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.