Module: Alchemy::Admin::TagsHelper

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

Instance Method Summary collapse

Instance Method Details

#add_to_tag_filter(tag) ⇒ Object

Adds the given tag to the tag filter.



58
59
60
61
62
63
64
65
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 58

def add_to_tag_filter(tag)
  if params[:tagged_with].present?
    tags = params[:tagged_with].split(',')
    tags << tag.name
  else
    [tag.name]
  end
end

#filtered_by_tag?(tag) ⇒ Boolean

Checks if the tagged_with param contains the given tag

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 48

def filtered_by_tag?(tag)
  if params[:tagged_with].present?
    tags = params[:tagged_with].split(',')
    tags.include?(tag.name)
  else
    false
  end
end

#remove_from_tag_filter(tag) ⇒ Object

Removes the given tag from the tag filter.



68
69
70
71
72
73
74
75
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 68

def remove_from_tag_filter(tag)
  if params[:tagged_with].present?
    tags = params[:tagged_with].split(',')
    tags.delete_if { |t| t == tag.name }
  else
    []
  end
end

#render_tag_list(class_name, params) ⇒ 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)


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

def render_tag_list(class_name, params)
  raise ArgumentError.new('Please provide a String as class_name') if class_name.nil?
  li_s = []
  class_name.constantize.tag_counts.sort { |x, y| x.name.downcase <=> y.name.downcase }.each do |tag|
    tags = filtered_by_tag?(tag) ? tag_filter(remove: tag) : tag_filter(add: tag)
    li_s << ('li', name: tag.name, class: tag_list_tag_active?(tag, params) ? 'active' : nil) do
      link_to(
        "#{tag.name} (#{tag.count})",
        url_for(
          params.delete_if { |k, v| k == "page" }.merge(
            action: 'index',
            tagged_with: tags
          )
        ),
        remote: request.xhr?,
        class: 'please_wait'
      )
    end
  end
  li_s.join.html_safe
end

#tag_filter(options = {}) ⇒ Object

Returns the tag filter from params.

A tag can be added to the filter. A tag can also be removed.

Options are:

* options (Hash):
** :add (ActsAsTaggableOn::Tag) - The tag that should be added to the tag-filter
** :remove (ActsAsTaggableOn::Tag) - The tag that should be removed from the tag-filter


87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/alchemy/admin/tags_helper.rb', line 87

def tag_filter(options={})
  case
    when options[:add]
      taglist = add_to_tag_filter(options[:add]) if options[:add]
    when options[:remove]
      taglist = remove_from_tag_filter(options[:remove]) if options[:remove]
    else
      return params[:tagged_with]
  end
  return nil if taglist.blank?
  taglist.uniq.join(',')
end

#tag_list_tag_active?(tag, params) ⇒ Boolean

Returns true if the given tag is in params[:tag_list]

Parameters:

  • tag (ActsAsTaggableOn::Tag)

    the tag

  • params (Hash)

    url params

Returns:

  • (Boolean)


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

def tag_list_tag_active?(tag, params)
  params[:tagged_with].to_s.split(',').include?(tag.name)
end