Module: RailsCom::AttachedOne

Included in:
ActiveStorage::Attached::One
Defined in:
lib/rails_com/active_storage/attached_macros.rb

Instance Method Summary collapse

Instance Method Details

#attached?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rails_com/active_storage/attached_macros.rb', line 14

def attached?
  attachment&.id?
end

#attachmentObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/rails_com/active_storage/attached_macros.rb', line 3

def attachment
  if super
    return super
  elsif defined?(@attachment)
    return @attachment
  end

  id = ActiveStorage::BlobDefault.defaults["#{record.class.name}_#{name}"]
  @attachment = build_attachment(blob: ActiveStorage::Blob.find(id)) if id
end