Class: Dragonfly::ActiveModelExtensions::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/active_model_extensions/attachment.rb,
lib/dragonfly/active_model_extensions/attachment_class_methods.rb

Defined Under Namespace

Classes: BadAssignmentKey, ConfigProxy

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Attachment

Returns a new instance of Attachment.



20
21
22
23
24
25
26
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 20

def initialize(model)
  @model = model
  self.uid = model_uid
  update_from_uid if uid
  @should_run_callbacks = true
  self.class.ensure_uses_cached_magic_attributes
end

Class Attribute Details

.appObject (readonly)

Returns the value of attribute app



82
83
84
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 82

def app
  @app
end

.attributeObject (readonly)

Returns the value of attribute attribute



82
83
84
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 82

def attribute
  @attribute
end

.config_blockObject (readonly)

Returns the value of attribute config_block



82
83
84
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 82

def config_block
  @config_block
end

.model_classObject (readonly)

Returns the value of attribute model_class



82
83
84
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 82

def model_class
  @model_class
end

Instance Attribute Details

#should_run_callbacks=(value) ⇒ Object (writeonly)

Sets the attribute should_run_callbacks

Parameters:

  • value

    the value to set the attribute should_run_callbacks to.



103
104
105
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 103

def should_run_callbacks=(value)
  @should_run_callbacks = value
end

Class Method Details

.allowed_magic_attributesObject

Magic attributes



101
102
103
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 101

def allowed_magic_attributes
  app.analyser.analysis_method_names + [:size, :basename, :name, :ext, :meta]
end

.callbacksObject

Callbacks



85
86
87
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 85

def callbacks
  @callbacks ||= Hash.new{|h,k| h[k] = [] }
end

.ensure_uses_cached_magic_attributesObject



118
119
120
121
122
123
124
125
126
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 118

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) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 133

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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 68

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_attributesObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 105

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

.run_callbacks(name, model, attachment) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 89

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_specsObject

Storage options



129
130
131
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 129

def storage_opts_specs
  @storage_opts_specs ||= []
end

Instance Method Details

#appObject



28
29
30
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 28

def app
  self.class.app
end

#applyObject



98
99
100
101
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 98

def apply
  job.apply
  self
end

#assign(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 36

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
    update_meta
    self.class.run_callbacks(:after_assign, model, self) if should_run_callbacks?
  end
  value
end

#attributeObject



32
33
34
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 32

def attribute
  self.class.attribute
end

#changed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 57

def changed?
  !!@changed
end

#destroy!Object



61
62
63
64
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 61

def destroy!
  destroy_previous!
  destroy_content(uid) if uid
end

#destroy_retained!Object



122
123
124
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 122

def destroy_retained!
  destroy_content(retained_attrs[:uid])
end

#encode!(*args) ⇒ Object



89
90
91
92
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 89

def encode!(*args)
  assign(encode(*args))
  self
end

#inspectObject



149
150
151
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 149

def inspect
  "<Dragonfly Attachment uid=#{uid.inspect}, app=#{app.inspect}>"
end

#name=(name) ⇒ Object



78
79
80
81
82
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 78

def name=(name)
  job.name = name
  set_magic_attribute(:name, name) if has_magic_attribute_for?(:name)
  name
end

#process!(*args) ⇒ Object



84
85
86
87
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 84

def process!(*args)
  assign(process(*args))
  self
end

#remote_url(opts = {}) ⇒ Object



94
95
96
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 94

def remote_url(opts={})
  app.remote_url_for(uid, opts) if uid
end

#retain!Object

Retaining for avoiding uploading more than once



111
112
113
114
115
116
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 111

def retain!
  if changed?
    store_job!
    self.retained = true
  end
end

#retained?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 118

def retained?
  !!@retained
end

#retained_attrsObject



126
127
128
129
130
131
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 126

def retained_attrs
  attribute_keys.inject({}) do |hash, key|
    hash[key] = send(key)
    hash
  end if retained?
end

#retained_attrs=(attrs) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 133

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
    update_from_uid
    self.retained = true
  end
end

#save!Object



66
67
68
69
70
71
72
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 66

def save!
  sync_with_model
  store_job! if job && !uid
  destroy_previous!
  self.changed = false
  self.retained = false
end

#should_run_callbacks?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 105

def should_run_callbacks?
  @should_run_callbacks
end

#to_valueObject



74
75
76
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 74

def to_value
  self if job
end