Class: ActiveShrine::Attached::Changes::CreateOne

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shrine/attached/changes/create_one.rb

Overview

:nodoc:

Direct Known Subclasses

CreateOneOfMany

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, record, attachment_class, attachable) ⇒ CreateOne



12
13
14
15
16
17
18
19
# File 'lib/active_shrine/attached/changes/create_one.rb', line 12

def initialize(name, record, attachment_class, attachable)
  @name = name
  @record = record
  @attachment_class = attachment_class
  @attachable = attachable

  attach
end

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable.



10
11
12
# File 'lib/active_shrine/attached/changes/create_one.rb', line 10

def attachable
  @attachable
end

#attachment_classObject (readonly)

Returns the value of attribute attachment_class.



10
11
12
# File 'lib/active_shrine/attached/changes/create_one.rb', line 10

def attachment_class
  @attachment_class
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/active_shrine/attached/changes/create_one.rb', line 10

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



10
11
12
# File 'lib/active_shrine/attached/changes/create_one.rb', line 10

def record
  @record
end

Instance Method Details

#attachmentObject



21
22
23
# File 'lib/active_shrine/attached/changes/create_one.rb', line 21

def attachment
  @attachment ||= find_or_build_attachment
end

#saveObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_shrine/attached/changes/create_one.rb', line 25

def save
  unless attachment.valid?
    attachment.errors.each do |error|
      record.errors.add(name, error.message)
    end

    raise ActiveRecord::RecordInvalid.new(record)
  end

  record.public_send(:"#{name}_attachment=", attachment)
end