Module: TagsHelper

Defined in:
app/helpers/tags_helper.rb

Instance Method Summary collapse

Instance Method Details

#category_sort_tags(tags, required_ids, topic_ids, moderator_ids) ⇒ Array<Tag>

Sort a list of tags by importance within the context of a category’s set of required, moderator, and topic tags.

Parameters:

  • tags (ActiveRecord::Relation<Tag>)

    A list of tags.

  • required_ids (Array<Integer>)

    A list of required tag IDs.

  • topic_ids (Array<Integer>)

    A list of topic tag IDs.

  • moderator_ids (Array<Integer>)

    A list of moderator-only tag IDs.

Returns:



9
10
11
12
13
14
15
16
# File 'app/helpers/tags_helper.rb', line 9

def category_sort_tags(tags, required_ids, topic_ids, moderator_ids)
  tags
    .to_a
    .sort_by do |t|
      [required_ids.include?(t.id) ? 0 : 1, moderator_ids.include?(t.id) ? 0 : 1,
       topic_ids.include?(t.id) ? 0 : 1, t.id]
    end
end

#post_ids_for_tags(tag_ids) ⇒ Array<Integer>

Get a list of post IDs that belong to any of the specified tag IDs.

Parameters:

  • tag_ids (Array<Integer>)

    A list of tag IDs.

Returns:

  • (Array<Integer>)

    A list of post IDs.



37
38
39
40
# File 'app/helpers/tags_helper.rb', line 37

def post_ids_for_tags(tag_ids)
  sql = "SELECT post_id FROM posts_tags WHERE tag_id IN #{ApplicationRecord.sanitize_sql_in(tag_ids)}"
  ActiveRecord::Base.connection.execute(sql).to_a.flatten
end

#tag_classes(tag, category) ⇒ String

Generate a list of classes to be applied to a tag.

Parameters:

  • tag (Tag)
  • category (Category)

    The category within the context of which the tag is being displayed.

Returns:

  • (String)


23
24
25
26
27
28
29
30
31
# File 'app/helpers/tags_helper.rb', line 23

def tag_classes(tag, category)
  required_ids = category&.required_tag_ids
  moderator_ids = category&.moderator_tag_ids
  topic_ids = category&.topic_tag_ids
  required = required_ids&.include?(tag.id) ? 'is-filled' : ''
  topic = topic_ids&.include?(tag.id) ? 'is-outlined' : ''
  moderator = moderator_ids&.include?(tag.id) ? 'is-red is-outlined' : ''
  "badge is-tag #{required} #{topic} #{moderator}"
end