Module: SortableAttachments::ClassMethods

Defined in:
lib/sortable_attachments.rb

Instance Method Summary collapse

Instance Method Details

#has_and_belongs_to_many_attachments(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sortable_attachments.rb', line 7

def has_and_belongs_to_many_attachments(options = {})
  has_many :attachment_links, :as => :element, :order => :position
  has_many :attachments, { :through => :attachment_links, :class_name => 'Attachment', :order => :position }.merge(options)
  has_many :pictures, :through => :attachment_links, :class_name => 'Picture', :source => :attachment, :order => :position
  has_many :docs, :through => :attachment_links, :class_name => 'Doc', :source => :attachment, :order => :position
  has_many :videos, :through => :attachment_links, :class_name => 'Video', :source => :attachment, :order => :position
  has_many :pdfs, :through => :attachment_links, :class_name => 'Pdf', :source => :attachment, :order => :position
  has_many :audios, :through => :attachment_links, :class_name => 'Audio', :source => :attachment, :order => :position
  has_many :medias, :through => :attachment_links, :class_name => 'Media', :source => :attachment, :order => :position

  unless self.instance_methods.include?('attachment_ids_with_position=')
    include SortableAttachments::InstanceMethods
    alias_method_chain :attachment_ids=, :position
  end
end