Sha256: b99277de047b33141d2d0f2f79dfed2aae6b8fc8e76e47210bc282609772eaba

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/hash'

module Admino
  module Query
    class FilterGroup
      attr_reader :params
      attr_reader :config
      attr_reader :query_i18n_key

      def initialize(config, params, query_i18n_key = nil)
        @config = config
        @params = ActiveSupport::HashWithIndifferentAccess.new(params)
        @query_i18n_key = query_i18n_key
      end

      def augment_scope(scope)
        if active_scope && active_scope != :empty
          scope.send(active_scope)
        else
          scope
        end
      end

      def active_scope
        if value && scopes.include?(value)
          value
        else
          nil
        end
      end

      def is_scope_active?(scope)
        active_scope == scope.to_sym
      end

      def value
        value = params.fetch(:query, {}).fetch(param_name, default_value)
        if value
          value.to_sym
        else
          nil
        end
      end

      def default_value
        if config.default_scope
          config.default_scope
        elsif config.include_empty_scope?
          :empty
        else
          nil
        end
      end

      def param_name
        config.name
      end

      def scopes
        @scopes ||= config.scopes.dup.tap do |scopes|
          if config.include_empty_scope?
            scopes.unshift :empty
          end
        end
      end

      def i18n_key
        config.name
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
admino-0.0.15 lib/admino/query/filter_group.rb
admino-0.0.14 lib/admino/query/filter_group.rb
admino-0.0.13 lib/admino/query/filter_group.rb
admino-0.0.12 lib/admino/query/filter_group.rb
admino-0.0.11 lib/admino/query/filter_group.rb