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.



24
25
26
27
28
29
30
# 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

Class Attribute Details

.appObject (readonly)

Returns the value of attribute app



78
79
80
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 78

def app
  @app
end

.attributeObject (readonly)

Returns the value of attribute attribute



78
79
80
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 78

def attribute
  @attribute
end

.config_blockObject (readonly)

Returns the value of attribute config_block



78
79
80
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 78

def config_block
  @config_block
end

.model_classObject (readonly)

Returns the value of attribute model_class



78
79
80
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 78

def model_class
  @model_class
end

Instance Attribute Details

#should_retain=(value) ⇒ Object (writeonly)

Sets the attribute should_retain

Parameters:

  • value

    the value to set the attribute should_retain to.



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

def should_retain=(value)
  @should_retain = value
end

#should_run_callbacks=(value) ⇒ Object (writeonly)

Sets the attribute should_run_callbacks

Parameters:

  • value

    the value to set the attribute should_run_callbacks to.



108
109
110
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 108

def should_run_callbacks=(value)
  @should_run_callbacks = value
end

Class Method Details

.allowed_magic_attributesObject

Magic attributes



97
98
99
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 97

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

.callbacksObject

Callbacks



81
82
83
# 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_attributesObject



114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_attributesObject



101
102
103
104
105
106
107
108
109
110
111
112
# 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

.run_callbacks(name, model, attachment) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# 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_specsObject

Storage options



125
126
127
# File 'lib/dragonfly/active_model_extensions/attachment_class_methods.rb', line 125

def storage_opts_specs
  @storage_opts_specs ||= []
end

Instance Method Details

#appObject



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

def app
  self.class.app
end

#applyObject



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

def apply
  job.apply
  self
end

#assign(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

#attributeObject



36
37
38
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 36

def attribute
  self.class.attribute
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  !!@changed
end

#destroy!Object



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

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

#destroy_retained!Object



133
134
135
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 133

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

#encode!(*args) ⇒ Object



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

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

#inspectObject



160
161
162
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 160

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

#name=(name) ⇒ Object



84
85
86
87
# 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) ⇒ Object



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

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

#remote_url(opts = {}) ⇒ Object



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

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

#retain!Object

Retaining for avoiding uploading more than once



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

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

#retained?Boolean

Returns:

  • (Boolean)


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

def retained?
  !!@retained
end

#retained_attrsObject



137
138
139
140
141
142
# 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) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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!Object



72
73
74
75
76
77
78
# 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?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 125

def should_retain?
  !!@should_retain
end

#should_run_callbacks?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 110

def should_run_callbacks?
  !!@should_run_callbacks
end

#to_valueObject



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

def to_value
  self if job
end