Sha256: a57c2ef8edd30c59b8e9f9c56a944560ddb6d174bab5ff7e920e608c3ad90ba6

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Ransack
  class Predicate
    attr_reader :name, :arel_predicate, :type, :formatter, :validator, :compound

    class << self
      def named(name)
        Configuration.predicates[name.to_s]
      end

      def for_attribute_name(attribute_name)
        self.named(Configuration.predicate_keys.detect {|p| attribute_name.to_s.match(/_#{p}$/)})
      end

      def collection
        Configuration.predicates.map {|k, v| [k, Translate.predicate(k)]}
      end
    end

    def initialize(opts = {})
      @name = opts[:name]
      @arel_predicate = opts[:arel_predicate]
      @type = opts[:type]
      @formatter = opts[:formatter]
      @validator = opts[:validator]
      @compound = opts[:compound]
    end

    def eql?(other)
      self.class == other.class &&
      self.name == other.name
    end
    alias :== :eql?

    def hash
      name.hash
    end

    def validate(vals)
      if validator
        vals.select {|v| validator.call(v.value)}.any?
      else
        vals.select {|v| !v.blank?}.any?
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ransack-0.3.0 lib/ransack/predicate.rb
ransack-0.2.1 lib/ransack/predicate.rb
ransack-0.2.0 lib/ransack/predicate.rb