Class: Dragonfly::Model::Attachment

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HasFilename
Defined in:
lib/dragonfly/model/attachment.rb,
lib/dragonfly/model/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

Methods included from HasFilename

#basename, #basename=, #ext, #ext=

Constructor Details

#initialize(model) ⇒ Attachment

Returns a new instance of Attachment.



26
27
28
29
30
31
32
# 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

Class Attribute Details

.appObject (readonly)

Returns the value of attribute app.



65
66
67
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 65

def app
  @app
end

.attributeObject (readonly)

Returns the value of attribute attribute.



65
66
67
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 65

def attribute
  @attribute
end

.config_blockObject (readonly)

Returns the value of attribute config_block.



65
66
67
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 65

def config_block
  @config_block
end

.default_pathObject

Returns the value of attribute default_path.



65
66
67
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 65

def default_path
  @default_path
end

.model_classObject (readonly)

Returns the value of attribute model_class.



65
66
67
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 65

def model_class
  @model_class
end

Instance Attribute Details

#jobObject

Returns the value of attribute job.



167
168
169
# File 'lib/dragonfly/model/attachment.rb', line 167

def job
  @job
end

#should_retain=(value) ⇒ Object (writeonly)

Sets the attribute should_retain

Parameters:

  • value

    the value to set the attribute should_retain to.



121
122
123
# File 'lib/dragonfly/model/attachment.rb', line 121

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.



106
107
108
# File 'lib/dragonfly/model/attachment.rb', line 106

def should_run_callbacks=(value)
  @should_run_callbacks = value
end

Class Method Details

.allowed_magic_attributesObject

Magic attributes



93
94
95
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 93

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

.callbacksObject

Callbacks



77
78
79
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 77

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

.default_jobObject



72
73
74
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 72

def default_job
  app.fetch_file(default_path) if default_path
end

.ensure_uses_cached_magic_attributesObject



110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Object



58
59
60
61
62
63
# 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_attributesObject



97
98
99
100
101
102
103
104
105
106
107
108
# 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

.run_callbacks(name, model, attachment) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# 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_specsObject

Storage options



121
122
123
# File 'lib/dragonfly/model/attachment_class_methods.rb', line 121

def storage_options_specs
  @storage_options_specs ||= []
end

Instance Method Details

#add_meta(meta) ⇒ Object



158
159
160
161
# File 'lib/dragonfly/model/attachment.rb', line 158

def add_meta(meta)
  job.add_meta(meta)
  self
end

#appObject



34
35
36
# File 'lib/dragonfly/model/attachment.rb', line 34

def app
  self.class.app
end

#applyObject



101
102
103
104
# File 'lib/dragonfly/model/attachment.rb', line 101

def apply
  job.apply
  self
end

#assign(value) ⇒ Object



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

#attributeObject



38
39
40
# File 'lib/dragonfly/model/attachment.rb', line 38

def attribute
  self.class.attribute
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  !!@changed
end

#destroy!Object



70
71
72
73
# File 'lib/dragonfly/model/attachment.rb', line 70

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

#destroy_retained!Object



131
132
133
# File 'lib/dragonfly/model/attachment.rb', line 131

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

#inspectObject



163
164
165
# File 'lib/dragonfly/model/attachment.rb', line 163

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

#name=(name) ⇒ Object



87
88
89
90
# 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) ⇒ Object



92
93
94
95
# File 'lib/dragonfly/model/attachment.rb', line 92

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

#remote_url(opts = {}) ⇒ Object



97
98
99
# File 'lib/dragonfly/model/attachment.rb', line 97

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

#retain!Object

Retaining for avoiding uploading more than once



114
115
116
117
118
119
# File 'lib/dragonfly/model/attachment.rb', line 114

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

#retained?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/dragonfly/model/attachment.rb', line 127

def retained?
  !!@retained
end

#retained_attrsObject



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



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



75
76
77
78
79
80
81
# 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?Boolean

Returns:

  • (Boolean)


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

def should_retain?
  !!@should_retain
end

#should_run_callbacks?Boolean

Returns:

  • (Boolean)


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

def should_run_callbacks?
  !!@should_run_callbacks
end

#stored?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dragonfly/model/attachment.rb', line 66

def stored?
  uid?
end

#to_valueObject



83
84
85
# File 'lib/dragonfly/model/attachment.rb', line 83

def to_value
  (self if job) || self.class.default_job
end