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
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_value(value)
|
|
||||||
value.cast_to_type(type)
|
|
||||||
end
|
|
||||||
|
|
||||||
def eql?(other)
|
def eql?(other)
|
||||||
self.class == other.class &&
|
self.class == other.class &&
|
||||||
self.name == other.name
|
self.name == other.name
|
||||||
|
|
|
@ -4,8 +4,6 @@ module Ransack
|
||||||
i18n_word :attribute, :predicate, :combinator, :value
|
i18n_word :attribute, :predicate, :combinator, :value
|
||||||
i18n_alias :a => :attribute, :p => :predicate, :m => :combinator, :v => :value
|
i18n_alias :a => :attribute, :p => :predicate, :m => :combinator, :v => :value
|
||||||
|
|
||||||
delegate :cast_value, :to => :first_attribute
|
|
||||||
|
|
||||||
attr_reader :predicate
|
attr_reader :predicate
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
@ -20,7 +18,9 @@ module Ransack
|
||||||
:m => combinator,
|
:m => combinator,
|
||||||
:v => [values]
|
: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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -37,17 +37,13 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid?
|
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
|
end
|
||||||
|
|
||||||
def valid_arity?
|
def valid_arity?
|
||||||
values.size <= 1 || predicate.compound || %w(in not_in).include?(predicate.name)
|
values.size <= 1 || predicate.compound || %w(in not_in).include?(predicate.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_attribute
|
|
||||||
attributes.first
|
|
||||||
end
|
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
@attributes ||= []
|
@attributes ||= []
|
||||||
end
|
end
|
||||||
|
@ -117,7 +113,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
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
|
end
|
||||||
|
|
||||||
def build(params)
|
def build(params)
|
||||||
|
@ -188,7 +184,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def casted_values_for_attribute(attr)
|
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
|
end
|
||||||
|
|
||||||
def formatted_values_for_attribute(attr)
|
def formatted_values_for_attribute(attr)
|
||||||
|
@ -199,6 +195,10 @@ module Ransack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def default_type
|
||||||
|
predicate.type || (attributes.first && attributes.first.type)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def valid_combinator?
|
def valid_combinator?
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Ransack
|
||||||
module Nodes
|
module Nodes
|
||||||
class Value < Node
|
class Value < Node
|
||||||
attr_accessor :value
|
attr_accessor :value
|
||||||
delegate :blank?, :present?, :to => :value
|
delegate :present?, :blank?, :to => :value
|
||||||
|
|
||||||
def initialize(context, value = nil)
|
def initialize(context, value = nil)
|
||||||
super(context)
|
super(context)
|
||||||
|
@ -23,7 +23,7 @@ module Ransack
|
||||||
value.hash
|
value.hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_to_type(type)
|
def cast(type)
|
||||||
case type
|
case type
|
||||||
when :date
|
when :date
|
||||||
cast_to_date(value)
|
cast_to_date(value)
|
||||||
|
|
|
@ -39,7 +39,7 @@ module Ransack
|
||||||
@arel_predicate = opts[:arel_predicate]
|
@arel_predicate = opts[:arel_predicate]
|
||||||
@type = opts[:type]
|
@type = opts[:type]
|
||||||
@formatter = opts[:formatter]
|
@formatter = opts[:formatter]
|
||||||
@validator = opts[:validator]
|
@validator = opts[:validator] || lambda { |v| v.present? }
|
||||||
@compound = opts[:compound]
|
@compound = opts[:compound]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,12 +61,8 @@ module Ransack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate(vals)
|
def validate(vals, type = @type)
|
||||||
if validator
|
vals.select {|v| validator.call(type ? v.cast(type) : v.value)}.any?
|
||||||
vals.select {|v| validator.call(v.value)}.any?
|
|
||||||
else
|
|
||||||
vals.select {|v| v.present?}.any?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
module Ransack
|
module Ransack
|
||||||
VERSION = "0.5.1"
|
VERSION = "0.5.2"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue