Sha256: b0a3df82d5d1890e0c89f4f0686768c667b807bb28e44fc06f1fe922af730607

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

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

module Admino
  module Query
    class Field
      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

4 entries across 4 versions & 1 rubygems

Version Path
admino-0.0.4 lib/admino/query/field.rb
admino-0.0.3 lib/admino/query/field.rb
admino-0.0.2 lib/admino/query/field.rb
admino-0.0.1 lib/admino/query/field.rb