Sha256: 6b4c08fa189a237795e5e5eef6e81803fb5170f2175e2f1874140c9c27a3bcca

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Alchemy
  module Admin
    module TagsHelper
      # Renders tags list items for given class name
      #
      # @param class_name [String]
      #   The class_name representing a tagged class
      #
      # @return [String]
      #   A HTML string containing <tt><li></tt> tags
      #
      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|
          content_tag('li', name: tag.name, class: filtered_by_tag?(tag) ? 'active' : nil) do
            link_to(
              "#{tag.name} (#{tag.count})",
              url_for(
                search_filter_params.except(:page, :tagged_with).merge(
                  tagged_with: tags_for_filter(current: tag).presence
                )
              ),
              remote: request.xhr?,
              class: 'please_wait'
            )
          end
        end.join.html_safe
      end

      # Returns true if the given tag is in +params[:tagged_with]+
      #
      def filtered_by_tag?(tag)
        tags_from_params.include?(tag.name)
      end

      # Returns the tags from params suitable for the tags filter.
      #
      # @param current [ActsAsTaggableOn::Tag] - The current tag that will be added or removed if already present
      # @returns [String]
      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

      # Returns tags from params
      # @returns [Array]
      def tags_from_params
        search_filter_params[:tagged_with].to_s.split(',')
      end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
alchemy_cms-4.0.6 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.5 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.4 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.3 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.2 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.1 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.0 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.0.rc2 app/helpers/alchemy/admin/tags_helper.rb
alchemy_cms-4.0.0.rc1 app/helpers/alchemy/admin/tags_helper.rb