Module: Alchemy::Admin::TagsHelper

Defined in:
app/helpers/alchemy/admin/tags_helper.rb

Instance Method Summary collapse

Instance Method Details

#filtered_by_tag?(tag) ⇒ Boolean

Returns true if the given tag is in params

Returns:

  • (Boolean)

34
35
36
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 34

def filtered_by_tag?(tag)
  tags_from_params.include?(tag.name)
end

#render_tag_list(class_name) ⇒ String

Renders tags list items for given class name

Parameters:

  • class_name (String)

    The class_name representing a tagged class

Returns:

  • (String)

    A HTML string containing <li> tags

Raises:

  • (ArgumentError)

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 14

def render_tag_list(class_name)
  raise ArgumentError, "Please provide a String as class_name" if class_name.nil?

  sorted_tags_from(class_name: class_name).map do |tag|
    ("li", name: tag.name, class: filtered_by_tag?(tag) ? "active" : nil) do
      link_to(
        "#{tag.name} (#{tag.taggings_count})",
        url_for(
          search_filter_params.except(:page, :tagged_with).merge(
            tagged_with: tags_for_filter(current: tag).presence,
          ),
        ),
        remote: request.xhr?,
      )
    end
  end.join.html_safe
end

#sorted_tags_from(class_name:) ⇒ Object


56
57
58
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 56

def sorted_tags_from(class_name:)
  class_name.constantize.tag_counts.sort { |x, y| x.name.downcase <=> y.name.downcase }
end

#tags_for_filter(current:) ⇒ Object

Returns the tags from params suitable for the tags filter.

Parameters:

  • current (Gutentag::Tag)
    • The current tag that will be added or removed if already present


42
43
44
45
46
47
48
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 42

def tags_for_filter(current:)
  if filtered_by_tag?(current)
    tags_from_params - Array(current.name)
  else
    tags_from_params.push(current.name)
  end.uniq.join(",")
end

#tags_from_paramsObject

Returns tags from params


52
53
54
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 52

def tags_from_params
  search_filter_params[:tagged_with].to_s.split(",")
end