Class: Dragonfly::ActiveModelExtensions::Attachment
- Inherits:
-
Object
- Object
- Dragonfly::ActiveModelExtensions::Attachment
show all
- Extended by:
- Forwardable
- Includes:
- HasFilename
- 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)
#basename, #basename=, #ext, #ext=
Constructor Details
- (Attachment) initialize(model)
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
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
|
+ (Object) attribute
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
|
+ (Object) config_block
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
|
+ (Object) model_class
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
- (Object) should_retain=(value)
Sets the attribute should_retain
123
124
125
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 123
def should_retain=(value)
@should_retain = value
end
|
- (Object) should_run_callbacks=(value)
Sets the attribute should_run_callbacks
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
+ (Object) allowed_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
|
+ (Object) 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
|
+ (Object) ensure_uses_cached_magic_attributes
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
|
+ (Object) evaluate_storage_opts(model, attachment)
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
|
+ (Object) init(model_class, attribute, app, config_block)
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
|
+ (Object) magic_attributes
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
|
+ (Object) run_callbacks(name, model, attachment)
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
|
+ (Object) storage_opts_specs
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
32
33
34
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 32
def app
self.class.app
end
|
103
104
105
106
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 103
def apply
job.apply
self
end
|
- (Object) assign(value)
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 =
self.class.run_callbacks(:after_assign, model, self) if should_run_callbacks?
retain! if should_retain?
end
model_uid_will_change!
value
end
|
- (Object) attribute
36
37
38
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 36
def attribute
self.class.attribute
end
|
- (Boolean) changed?
63
64
65
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 63
def changed?
!!@changed
end
|
- (Object) destroy!
67
68
69
70
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 67
def destroy!
destroy_previous!
destroy_content(uid) if uid?
end
|
- (Object) destroy_retained!
133
134
135
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 133
def destroy_retained!
destroy_content(retained_attrs[:uid])
end
|
- (Object) encode!(*args)
94
95
96
97
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 94
def encode!(*args)
assign(encode(*args))
self
end
|
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
|
- (Object) name=(name)
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
|
- (Object) process!(*args)
89
90
91
92
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 89
def process!(*args)
assign(process(*args))
self
end
|
- (Object) remote_url(opts = {})
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
|
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
|
- (Boolean) retained?
129
130
131
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 129
def retained?
!!@retained
end
|
- (Object) retained_attrs
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
|
- (Object) retained_attrs=(attrs)
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?
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
|
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
|
- (Boolean) should_retain?
125
126
127
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 125
def should_retain?
!!@should_retain
end
|
- (Boolean) should_run_callbacks?
110
111
112
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 110
def should_run_callbacks?
!!@should_run_callbacks
end
|
- (Object) to_value
80
81
82
|
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 80
def to_value
self if job
end
|