mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
Cast values when determining if condition is valid. Fixes errors on blank dates
This commit is contained in:
parent
33655d3f95
commit
ff8138c32e
5 changed files with 16 additions and 24 deletions
|
@ -30,10 +30,6 @@ module Ransack
|
|||
end
|
||||
end
|
||||
|
||||
def cast_value(value)
|
||||
value.cast_to_type(type)
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
self.class == other.class &&
|
||||
self.name == other.name
|
||||
|
|
|
@ -4,8 +4,6 @@ module Ransack
|
|||
i18n_word :attribute, :predicate, :combinator, :value
|
||||
i18n_alias :a => :attribute, :p => :predicate, :m => :combinator, :v => :value
|
||||
|
||||
delegate :cast_value, :to => :first_attribute
|
||||
|
||||
attr_reader :predicate
|
||||
|
||||
class << self
|
||||
|
@ -20,7 +18,9 @@ module Ransack
|
|||
:m => combinator,
|
||||
:v => [values]
|
||||
)
|
||||
predicate.validate(condition.values) ? condition : nil
|
||||
# TODO: Figure out what to do with multiple types of attributes, if anything.
|
||||
# Tempted to go with "garbage in, garbage out" on this one
|
||||
predicate.validate(condition.values, condition.default_type) ? condition : nil
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -37,17 +37,13 @@ module Ransack
|
|||
end
|
||||
|
||||
def valid?
|
||||
attributes.detect(&:valid?) && predicate && valid_arity? && predicate.validate(values) && valid_combinator?
|
||||
attributes.detect(&:valid?) && predicate && valid_arity? && predicate.validate(values, default_type) && valid_combinator?
|
||||
end
|
||||
|
||||
def valid_arity?
|
||||
values.size <= 1 || predicate.compound || %w(in not_in).include?(predicate.name)
|
||||
end
|
||||
|
||||
def first_attribute
|
||||
attributes.first
|
||||
end
|
||||
|
||||
def attributes
|
||||
@attributes ||= []
|
||||
end
|
||||
|
@ -117,7 +113,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def value
|
||||
predicate.compound ? values.map {|v| cast_value(v)} : cast_value(values.first)
|
||||
predicate.compound ? values.map {|v| v.cast(default_type)} : values.first.cast(default_type)
|
||||
end
|
||||
|
||||
def build(params)
|
||||
|
@ -188,7 +184,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def casted_values_for_attribute(attr)
|
||||
validated_values.map {|v| v.cast_to_type(predicate.type || attr.type)}
|
||||
validated_values.map {|v| v.cast(predicate.type || attr.type)}
|
||||
end
|
||||
|
||||
def formatted_values_for_attribute(attr)
|
||||
|
@ -199,6 +195,10 @@ module Ransack
|
|||
end
|
||||
end
|
||||
|
||||
def default_type
|
||||
predicate.type || (attributes.first && attributes.first.type)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_combinator?
|
||||
|
|
|
@ -2,7 +2,7 @@ module Ransack
|
|||
module Nodes
|
||||
class Value < Node
|
||||
attr_accessor :value
|
||||
delegate :blank?, :present?, :to => :value
|
||||
delegate :present?, :blank?, :to => :value
|
||||
|
||||
def initialize(context, value = nil)
|
||||
super(context)
|
||||
|
@ -23,7 +23,7 @@ module Ransack
|
|||
value.hash
|
||||
end
|
||||
|
||||
def cast_to_type(type)
|
||||
def cast(type)
|
||||
case type
|
||||
when :date
|
||||
cast_to_date(value)
|
||||
|
|
|
@ -39,7 +39,7 @@ module Ransack
|
|||
@arel_predicate = opts[:arel_predicate]
|
||||
@type = opts[:type]
|
||||
@formatter = opts[:formatter]
|
||||
@validator = opts[:validator]
|
||||
@validator = opts[:validator] || lambda { |v| v.present? }
|
||||
@compound = opts[:compound]
|
||||
end
|
||||
|
||||
|
@ -61,12 +61,8 @@ module Ransack
|
|||
end
|
||||
end
|
||||
|
||||
def validate(vals)
|
||||
if validator
|
||||
vals.select {|v| validator.call(v.value)}.any?
|
||||
else
|
||||
vals.select {|v| v.present?}.any?
|
||||
end
|
||||
def validate(vals, type = @type)
|
||||
vals.select {|v| validator.call(type ? v.cast(type) : v.value)}.any?
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module Ransack
|
||||
VERSION = "0.5.1"
|
||||
VERSION = "0.5.2"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue