Code cleanup
This commit is contained in:
parent
43fe25c85e
commit
787eb3c1d1
|
@ -20,8 +20,8 @@ module Ransack
|
|||
|
||||
def valid?
|
||||
bound? && attr &&
|
||||
context.klassify(parent).ransackable_attributes(context.auth_object)
|
||||
.include?(attr_name)
|
||||
context.klassify(parent).ransackable_attributes(context.auth_object)
|
||||
.include?(attr_name)
|
||||
end
|
||||
|
||||
def type
|
||||
|
|
|
@ -5,7 +5,9 @@ module Ransack
|
|||
attr_accessor :parent, :attr_name
|
||||
|
||||
def attr
|
||||
@attr ||= ransacker ? ransacker.attr_from(self) : context.table_for(parent)[attr_name]
|
||||
@attr ||= ransacker ?
|
||||
ransacker.attr_from(self) :
|
||||
context.table_for(parent)[attr_name]
|
||||
end
|
||||
alias :arel_attribute :attr
|
||||
|
||||
|
|
|
@ -2,7 +2,8 @@ module Ransack
|
|||
module Nodes
|
||||
class Condition < Node
|
||||
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
|
||||
|
||||
attr_accessor :predicate
|
||||
|
||||
|
@ -20,7 +21,8 @@ module Ransack
|
|||
)
|
||||
# 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
|
||||
predicate.validate(condition.values, condition.default_type) ?
|
||||
condition : nil
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -37,8 +39,8 @@ module Ransack
|
|||
end
|
||||
|
||||
def valid?
|
||||
attributes.detect(&:valid?) && predicate && valid_arity? && predicate.
|
||||
validate(values, default_type) && valid_combinator?
|
||||
attributes.detect(&:valid?) && predicate && valid_arity? &&
|
||||
predicate.validate(values, default_type) && valid_combinator?
|
||||
end
|
||||
|
||||
def valid_arity?
|
||||
|
@ -116,8 +118,9 @@ module Ransack
|
|||
end
|
||||
|
||||
def value
|
||||
predicate.wants_array ? values.map {|v| v.cast(default_type)} : values.
|
||||
first.cast(default_type)
|
||||
predicate.wants_array ?
|
||||
values.map { |v| v.cast(default_type) } :
|
||||
values.first.cast(default_type)
|
||||
end
|
||||
|
||||
def build(params)
|
||||
|
@ -135,7 +138,8 @@ module Ransack
|
|||
end
|
||||
|
||||
def key
|
||||
@key ||= attributes.map(&:name).join("_#{combinator}_") + "_#{predicate.name}"
|
||||
@key ||= attributes.map(&:name).join("_#{combinator}_") +
|
||||
"_#{predicate.name}"
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
|
@ -163,7 +167,9 @@ module Ransack
|
|||
|
||||
def arel_predicate
|
||||
predicates = attributes.map do |attr|
|
||||
attr.attr.send(predicate.arel_predicate, formatted_values_for_attribute(attr))
|
||||
attr.attr.send(
|
||||
predicate.arel_predicate, formatted_values_for_attribute(attr)
|
||||
)
|
||||
end
|
||||
|
||||
if predicates.size > 1
|
||||
|
@ -179,16 +185,17 @@ module Ransack
|
|||
end
|
||||
|
||||
def validated_values
|
||||
values.select {|v| predicate.validator.call(v.value)}
|
||||
values.select { |v| predicate.validator.call(v.value) }
|
||||
end
|
||||
|
||||
def casted_values_for_attribute(attr)
|
||||
validated_values.map {|v| v.cast(predicate.type || attr.type)}
|
||||
validated_values.map { |v| v.cast(predicate.type || attr.type) }
|
||||
end
|
||||
|
||||
def formatted_values_for_attribute(attr)
|
||||
formatted = casted_values_for_attribute(attr).map do |val|
|
||||
val = attr.ransacker.formatter.call(val) if attr.ransacker && attr.ransacker.formatter
|
||||
val = attr.ransacker.formatter.call(val) if
|
||||
attr.ransacker && attr.ransacker.formatter
|
||||
val = predicate.format(val)
|
||||
val
|
||||
end
|
||||
|
@ -205,17 +212,18 @@ module Ransack
|
|||
['predicate', p],
|
||||
['combinator', m],
|
||||
['values', v.try(:map, &:value)]
|
||||
].
|
||||
reject { |e| e[1].blank? }.
|
||||
map { |v| "#{v[0]}: #{v[1]}" }.
|
||||
join(', ')
|
||||
]
|
||||
.reject { |e| e[1].blank? }
|
||||
.map { |v| "#{v[0]}: #{v[1]}" }
|
||||
.join(', ')
|
||||
"Condition <#{data}>"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_combinator?
|
||||
attributes.size < 2 || ['and', 'or'].include?(combinator)
|
||||
attributes.size < 2 ||
|
||||
['and', 'or'].include?(combinator)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ module Ransack
|
|||
module Nodes
|
||||
class Node
|
||||
attr_reader :context
|
||||
delegate :contextualize, :to => :context
|
||||
delegate :contextualize, to: :context
|
||||
class_attribute :i18n_words
|
||||
class_attribute :i18n_aliases
|
||||
self.i18n_words = []
|
||||
|
@ -14,7 +14,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def i18n_alias(opts = {})
|
||||
self.i18n_aliases.merge! Hash[opts.map {|k, v| [k.to_s, v.to_s]}]
|
||||
self.i18n_aliases.merge! Hash[opts.map { |k, v| [k.to_s, v.to_s] }]
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ module Ransack
|
|||
class << self
|
||||
def extract(context, str)
|
||||
attr, direction = str.split(/\s+/,2)
|
||||
self.new(context).build(:name => attr, :dir => direction)
|
||||
self.new(context).build(name: attr, dir: direction)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ module Ransack
|
|||
module Nodes
|
||||
class Value < Node
|
||||
attr_accessor :value
|
||||
delegate :present?, :blank?, :to => :value
|
||||
delegate :present?, :blank?, to: :value
|
||||
|
||||
def initialize(context, value = nil)
|
||||
super(context)
|
||||
|
|
|
@ -20,12 +20,17 @@ module Ransack
|
|||
|
||||
it 'avoids creating compound predicates if compounds: false' do
|
||||
Ransack.configure do |config|
|
||||
config.add_predicate :test_predicate_without_compound, compounds: false
|
||||
config.add_predicate(
|
||||
:test_predicate_without_compound,
|
||||
compounds: false
|
||||
)
|
||||
end
|
||||
|
||||
Ransack.predicates.should have_key 'test_predicate_without_compound'
|
||||
Ransack.predicates.should_not have_key 'test_predicate_without_compound_any'
|
||||
Ransack.predicates.should_not have_key 'test_predicate_without_compound_all'
|
||||
Ransack.predicates
|
||||
.should have_key 'test_predicate_without_compound'
|
||||
Ransack.predicates
|
||||
.should_not have_key 'test_predicate_without_compound_any'
|
||||
Ransack.predicates
|
||||
.should_not have_key 'test_predicate_without_compound_all'
|
||||
end
|
||||
|
||||
it 'should have default value for search key' do
|
||||
|
@ -48,7 +53,11 @@ module Ransack
|
|||
|
||||
it 'adds predicates that take arrays, overriding compounds' do
|
||||
Ransack.configure do |config|
|
||||
config.add_predicate :test_array_predicate, wants_array: true, compounds: true
|
||||
config.add_predicate(
|
||||
:test_array_predicate,
|
||||
wants_array: true,
|
||||
compounds: true
|
||||
)
|
||||
end
|
||||
|
||||
Ransack.predicates['test_array_predicate'].wants_array.should eq true
|
||||
|
|
|
@ -22,14 +22,17 @@ module Ransack
|
|||
describe 'eq' do
|
||||
it 'generates an equality condition for boolean true' do
|
||||
@s.awesome_eq = true
|
||||
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
|
||||
@s.result.to_sql.should match /#{field} = #{ActiveRecord::Base.connection.quoted_true}/
|
||||
field = "#{quote_table_name("people")}.#{
|
||||
quote_column_name("awesome")}"
|
||||
@s.result.to_sql.should match /#{field} = #{
|
||||
ActiveRecord::Base.connection.quoted_true}/
|
||||
end
|
||||
|
||||
it 'generates an equality condition for boolean false' do
|
||||
@s.awesome_eq = false
|
||||
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
|
||||
@s.result.to_sql.should match /#{field} = #{ActiveRecord::Base.connection.quoted_false}/
|
||||
@s.result.to_sql.should match /#{field} = #{
|
||||
ActiveRecord::Base.connection.quoted_false}/
|
||||
end
|
||||
|
||||
it 'does not generate a condition for nil' do
|
||||
|
|
|
@ -135,7 +135,8 @@ module Ransack
|
|||
search = Search.new(Person, children_name_or_name_eq: 'Ernie')
|
||||
search.result.should be_an ActiveRecord::Relation
|
||||
where = search.result.where_values.first
|
||||
where.to_sql.should match /#{children_people_name_field} = 'Ernie' OR #{people_name_field} = 'Ernie'/
|
||||
where.to_sql.should match /#{children_people_name_field
|
||||
} = 'Ernie' OR #{people_name_field} = 'Ernie'/
|
||||
end
|
||||
|
||||
it 'evaluates polymorphic belongs_to association conditions contextually' do
|
||||
|
@ -155,7 +156,8 @@ module Ransack
|
|||
where = search.result.where_values.first
|
||||
where.to_sql.should match /#{children_people_name_field} = 'Ernie'/
|
||||
where.to_sql.should match /#{people_name_field} = 'Ernie'/
|
||||
where.to_sql.should match /#{quote_table_name("children_people_2")}.#{quote_column_name("name")} = 'Ernie'/
|
||||
where.to_sql.should match /#{quote_table_name("children_people_2")
|
||||
}.#{quote_column_name("name")} = 'Ernie'/
|
||||
end
|
||||
|
||||
it 'evaluates arrays of groupings' do
|
||||
|
@ -309,7 +311,7 @@ module Ransack
|
|||
end
|
||||
|
||||
it 'raises NoMethodError when sent an invalid attribute' do
|
||||
expect {@s.blah}.to raise_error NoMethodError
|
||||
expect { @s.blah }.to raise_error NoMethodError
|
||||
end
|
||||
|
||||
it 'sets condition attributes when sent valid attributes' do
|
||||
|
@ -318,7 +320,9 @@ module Ransack
|
|||
end
|
||||
|
||||
it 'allows chaining to access nested conditions' do
|
||||
@s.groupings = [{ m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }]
|
||||
@s.groupings = [
|
||||
{ m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }
|
||||
]
|
||||
@s.groupings.first.children_name_eq.should eq 'Ernie'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue