Module: Extensions::Attachable::ActiveRecord::Base::HasManyAttachments

Extended by:
ActiveSupport::Concern
Defined in:
lib/extensions/attachable/active_record/base.rb

Constant Summary collapse

ATTACHMENT_REMOVED_SUFFIX =
'_attachment_references_removed'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_attachment_references_readers(attachable_columns) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/extensions/attachable/active_record/base.rb', line 63

def self.define_attachment_references_readers(attachable_columns)
  attachable_columns.each do |column|
    method_name = "#{column}#{ATTACHMENT_REMOVED_SUFFIX}"
    next if method_defined?(method_name)

    # Define a reader `#{column_name}_attachment_references_removed` to allow clients
    # to implement logic when attachments are removed.
    # This method returns the attachment_reference_ids of attachments that are removed,
    # by comparing `column` and `column_was` (from ActiveRecord::Dirty).
    define_method(method_name) do
      return [] unless send("#{column}_changed?")
      ids_was = parse_attachment_reference_ids_from_content(send("#{column}_was"))
      ids = parse_attachment_reference_ids_from_content(send(column))
      ids_was - ids
    end
  end
end

Instance Method Details

#files=(files) ⇒ Object


81
82
83
84
85
# File 'lib/extensions/attachable/active_record/base.rb', line 81

def files=(files)
  files.each do |file|
    attachment_references.build(file: file)
  end
end