Sha256: 22fb7c7f4fd97f5f37e470258425004fd298051a480158f5e28c1144b44f032a

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module ActiveAdmin
  module Filters

    class Humanized
      include ActiveAdmin::ViewHelpers

      def initialize(param)
        @body = param[0]
        @value = param[1]
      end

      def value
        @value.is_a?(::Array) ? @value.compact.join(', ') : @value
      end

      def body
        predicate = ransack_predicate_translation

        if current_predicate.nil?
          predicate = @body.titleize
        elsif translation_missing?(predicate)
          predicate = active_admin_predicate_translation
        end

        "#{parse_parameter_body} #{predicate}".strip
      end

      private

      def parse_parameter_body
        return if current_predicate.nil?

        # Accounting for strings that might contain other predicates. Example:
        # 'requires_approval' contains the substring 'eq'
        split_string = "_#{current_predicate}"

        @body.split(split_string)
          .first
          .gsub('_', ' ')
          .strip
          .titleize
          .gsub('Id', 'ID')
      end

      def current_predicate
        @current_predicate ||= predicates.detect { |p| @body.include?(p) }
      end

      def predicates
        Ransack::Predicate.names_by_decreasing_length
      end

      def ransack_predicate_translation
        I18n.t("ransack.predicates.#{current_predicate}")
      end

      def active_admin_predicate_translation
        translation = I18n.t("active_admin.filters.predicates.#{current_predicate}").downcase
      end

      def translation_missing?(predicate)
        predicate.downcase.include?('translation missing')
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin-1.0.0.pre4 lib/active_admin/filters/humanized.rb
activeadmin-1.0.0.pre3 lib/active_admin/filters/humanized.rb
activeadmin-1.0.0.pre2 lib/active_admin/filters/humanized.rb