Sha256: d2bb224395a2319344674b63d02fc10b33154a1a13949d5ad4d78abce4e55302

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

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

module Admino
  module Query
    class SearchField
      attr_reader :params
      attr_reader :config

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

      def augment_scope(scope)
        if present?
          scope.send(scope_name, value)
        else
          scope
        end
      end

      def value
        value = params.fetch(:query, {}).fetch(param_name, nil)
        if config.coerce_to
          begin
            coercer = Coercible::Coercer.new
            coercer[value.class].send(config.coerce_to, value)
          rescue Coercible::UnsupportedCoercion
            nil
          end
        else
          value
        end
      end

      def present?
        value.present?
      end

      def param_name
        config.name
      end

      def scope_name
        config.name
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
admino-0.0.10 lib/admino/query/search_field.rb
admino-0.0.9 lib/admino/query/search_field.rb
admino-0.0.8 lib/admino/query/search_field.rb
admino-0.0.7 lib/admino/query/search_field.rb
admino-0.0.6 lib/admino/query/search_field.rb
admino-0.0.5 lib/admino/query/search_field.rb