Module: Polytag::ClassMethods

Defined in:
lib/polytag.rb

Instance Method Summary collapse

Instance Method Details

#has_tags(*tags) ⇒ Object Also known as: has_tag

Get records with tags



54
55
56
# File 'lib/polytag.rb', line 54

def has_tags(*tags)
  includes(polytag_tag_relations: :polytag_tag).references(:polytag_tags).where("polytag_tags.name IN (?)", tags).group("#{table_name}.id")
end

#in_tag_group(_tag_group = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/polytag.rb', line 60

def in_tag_group(_tag_group = {})
  if _tag_group[:group_ids]
    tag_groups = _tag_group[:group_ids]
  else
    tag_groups = Polytag::TagGroup.select('polytag_tag_groups.id').search_by_hash(_tag_group).map{ |tg| tg.try(:id) }.flatten
  end

  return tag_groups if _tag_group[:multi]

  includes(polytag_tag_relations: :polytag_tag).references(:polytag_tags).where('polytag_tags.polytag_tag_group_id IN (?)', tag_groups)
end

#in_tag_groups(*tag_groups) ⇒ Object



72
73
74
75
# File 'lib/polytag.rb', line 72

def in_tag_groups(*tag_groups)
  tag_groups = tag_groups.map{ |tg| in_tag_group(tg.merge(multi: true)) }.flatten.compact
  in_tag_group(group_ids: tag_groups)
end