Module: Extensions::Attachable::ActiveRecord::Base::ClassMethods

Defined in:
lib/extensions/attachable/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#has_many_attachments(options = {}) ⇒ Object

This method should be declared in model, to let it have attachments.

Examples:

Has many attachments on a column

has_many_attachments on: :description #=> description is associated with the attachments
of the model, updating description will result in attachments changing.

You can further implement `description_attachment_references_removed` reader in this case to
override the default method. The attachment_references ids returned by it will be removed.

For deletion of attachments, it is necessary for the model to implement the
+:destroy_attachment+ CanCanCan permission on the +attachable+ object.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :on (Symbol|Array<Symbol>)

    The column which is associated with attachments, the column type should be string or text. This can be a symbol or an array of symbols. An attribute named column_name_attachment_references will be defined, you can override it to customise the way to retrieve the attachment_references for the specific column.


21
22
23
24
25
26
27
28
29
# File 'lib/extensions/attachable/active_record/base.rb', line 21

def has_many_attachments(options = {}) # rubocop:disable Naming/PredicateName
  include HasManyAttachments

  return unless options[:on]
  self.attachable_columns = Array(options[:on])
  before_save :update_attachment_references

  HasManyAttachments.define_attachment_references_readers(attachable_columns)
end

#has_one_attachmentObject

rubocop:disable Naming/PredicateName


31
32
33
# File 'lib/extensions/attachable/active_record/base.rb', line 31

def has_one_attachment # rubocop:disable Naming/PredicateName
  include HasOneAttachment
end