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)

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

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
# 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


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

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


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

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


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

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