Class: Dragonfly::ActiveModelExtensions::Attachment

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

Instance Method Summary collapse

Constructor Details

#initialize(app, parent_model, attribute_name) ⇒ Attachment

Returns a new instance of Attachment.



15
16
17
18
19
20
21
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 15

def initialize(app, parent_model, attribute_name)
  @app, @parent_model, @attribute_name = app, parent_model, attribute_name
  self.extend app.analyser.analysis_methods
  self.extend app.job_definitions
  self.uid = parent_uid
  self.job = app.fetch(uid) if uid
end

Instance Method Details

#analyse(meth, *args) ⇒ Object



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

def analyse(meth, *args)
  has_magic_attribute_for?(meth) ? magic_attribute_for(meth) : job.send(meth)
end

#assign(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 23

def assign(value)
  if value.nil?
    self.job = nil
    reset_magic_attributes
  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
  end
  set_uid_and_parent_uid(nil)
  value
end

#destroy!Object



39
40
41
42
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 39

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

#encode!(*args) ⇒ Object



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

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

#name=(name) ⇒ Object



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

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

#process!(*args) ⇒ Object



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

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

#save!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dragonfly/active_model_extensions/attachment.rb', line 44

def save!
  sync_with_parent!
  destroy_previous!
  if job && !uid
    set_uid_and_parent_uid job.store(
      :meta => {
        :model_class => parent_model.class.name,
        :model_attachment => attribute_name
      }
    )
    self.job = job.to_fetched_job(uid)
  end
end

#to_valueObject



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

def to_value
  self if job
end