Module: HasAttachedTags

Defined in:
lib/has_attached_tags.rb,
lib/has_attached_tags/tag.rb,
lib/has_attached_tags/tagging.rb,
lib/has_attached_tags/version.rb,
lib/generators/has_attached_tags/install_generator.rb,
lib/generators/has_attached_tags/migration_generator.rb

Defined Under Namespace

Classes: InstallGenerator, MigrationGenerator, Tag, Tagging

Constant Summary collapse

TAGGINGS_EXISTS =
lambda { |tag, attachment, taggable|
  Tagging
    .where(tag: tag, attachment: attachment, taggable_type: taggable.polymorphic_name)
    .where(Tagging.arel_table[:taggable_id].eq(taggable.arel_table[:id]))
    .arel.exists
}
VERSION =
'0.1.1'

Instance Method Summary collapse

Instance Method Details

#has_many_tags(attachment, type: default_type_for_attachment(attachment), required: false) ⇒ void

This method returns an undefined value.

Parameters:

  • attachment (Symbol)
  • type (String) (defaults to: default_type_for_attachment(attachment))
  • required (Boolean) (defaults to: false)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/has_attached_tags.rb', line 37

def has_many_tags(attachment, type: default_type_for_attachment(attachment), required: false) # rubocop:disable Naming/PredicateName
  klass = define_attachment_tagging_class(attachment, type)

  has_many(:"#{attachment}_taggings", -> { where(attachment: attachment) },
           as: :taggable, autosave: true, class_name: klass.name, dependent: :destroy, inverse_of: :taggable)
  has_many(attachment, -> { of_type(type) }, source: :tag, through: :"#{attachment}_taggings")

  validates(attachment, presence: true) if required

  define_attachment_scopes(attachment)
end

#has_one_tag(attachment, type: default_type_for_attachment(attachment), required: false) ⇒ void

This method returns an undefined value.

Parameters:

  • attachment (Symbol)
  • type (String) (defaults to: default_type_for_attachment(attachment))
  • required (Boolean) (defaults to: false)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/has_attached_tags.rb', line 21

def has_one_tag(attachment, type: default_type_for_attachment(attachment), required: false) # rubocop:disable Naming/PredicateName
  klass = define_attachment_tagging_class(attachment, type)

  has_one(:"#{attachment}_tagging", -> { where(attachment: attachment) },
          as: :taggable, autosave: true, class_name: klass.name, dependent: :destroy, inverse_of: :taggable)
  has_one(attachment, -> { of_type(type) }, source: :tag, through: :"#{attachment}_tagging")

  validates(attachment, presence: true) if required

  define_attachment_scopes(attachment)
end