Code cleanup

This commit is contained in:
jonatack 2013-12-15 19:27:50 +01:00
parent 43fe25c85e
commit 787eb3c1d1
9 changed files with 62 additions and 36 deletions

View File

@ -20,8 +20,8 @@ module Ransack
def valid? def valid?
bound? && attr && bound? && attr &&
context.klassify(parent).ransackable_attributes(context.auth_object) context.klassify(parent).ransackable_attributes(context.auth_object)
.include?(attr_name) .include?(attr_name)
end end
def type def type

View File

@ -5,7 +5,9 @@ module Ransack
attr_accessor :parent, :attr_name attr_accessor :parent, :attr_name
def attr 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 end
alias :arel_attribute :attr alias :arel_attribute :attr

View File

@ -2,7 +2,8 @@ module Ransack
module Nodes module Nodes
class Condition < Node class Condition < Node
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
attr_accessor :predicate attr_accessor :predicate
@ -20,7 +21,8 @@ module Ransack
) )
# TODO: Figure out what to do with multiple types of attributes, if anything. # TODO: Figure out what to do with multiple types of attributes, if anything.
# Tempted to go with "garbage in, garbage out" on this one # 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
end end
@ -37,8 +39,8 @@ module Ransack
end end
def valid? def valid?
attributes.detect(&:valid?) && predicate && valid_arity? && predicate. attributes.detect(&:valid?) && predicate && valid_arity? &&
validate(values, default_type) && valid_combinator? predicate.validate(values, default_type) && valid_combinator?
end end
def valid_arity? def valid_arity?
@ -116,8 +118,9 @@ module Ransack
end end
def value def value
predicate.wants_array ? values.map {|v| v.cast(default_type)} : values. predicate.wants_array ?
first.cast(default_type) values.map { |v| v.cast(default_type) } :
values.first.cast(default_type)
end end
def build(params) def build(params)
@ -135,7 +138,8 @@ module Ransack
end end
def key def key
@key ||= attributes.map(&:name).join("_#{combinator}_") + "_#{predicate.name}" @key ||= attributes.map(&:name).join("_#{combinator}_") +
"_#{predicate.name}"
end end
def eql?(other) def eql?(other)
@ -163,7 +167,9 @@ module Ransack
def arel_predicate def arel_predicate
predicates = attributes.map do |attr| 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 end
if predicates.size > 1 if predicates.size > 1
@ -179,16 +185,17 @@ module Ransack
end end
def validated_values def validated_values
values.select {|v| predicate.validator.call(v.value)} values.select { |v| predicate.validator.call(v.value) }
end end
def casted_values_for_attribute(attr) 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 end
def formatted_values_for_attribute(attr) def formatted_values_for_attribute(attr)
formatted = casted_values_for_attribute(attr).map do |val| 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 = predicate.format(val)
val val
end end
@ -205,17 +212,18 @@ module Ransack
['predicate', p], ['predicate', p],
['combinator', m], ['combinator', m],
['values', v.try(:map, &:value)] ['values', v.try(:map, &:value)]
]. ]
reject { |e| e[1].blank? }. .reject { |e| e[1].blank? }
map { |v| "#{v[0]}: #{v[1]}" }. .map { |v| "#{v[0]}: #{v[1]}" }
join(', ') .join(', ')
"Condition <#{data}>" "Condition <#{data}>"
end end
private private
def valid_combinator? def valid_combinator?
attributes.size < 2 || ['and', 'or'].include?(combinator) attributes.size < 2 ||
['and', 'or'].include?(combinator)
end end
end end

View File

@ -2,7 +2,7 @@ module Ransack
module Nodes module Nodes
class Node class Node
attr_reader :context attr_reader :context
delegate :contextualize, :to => :context delegate :contextualize, to: :context
class_attribute :i18n_words class_attribute :i18n_words
class_attribute :i18n_aliases class_attribute :i18n_aliases
self.i18n_words = [] self.i18n_words = []
@ -14,7 +14,7 @@ module Ransack
end end
def i18n_alias(opts = {}) 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
end end

View File

@ -9,7 +9,7 @@ module Ransack
class << self class << self
def extract(context, str) def extract(context, str)
attr, direction = str.split(/\s+/,2) attr, direction = str.split(/\s+/,2)
self.new(context).build(:name => attr, :dir => direction) self.new(context).build(name: attr, dir: direction)
end end
end end

View File

@ -2,7 +2,7 @@ module Ransack
module Nodes module Nodes
class Value < Node class Value < Node
attr_accessor :value attr_accessor :value
delegate :present?, :blank?, :to => :value delegate :present?, :blank?, to: :value
def initialize(context, value = nil) def initialize(context, value = nil)
super(context) super(context)

View File

@ -20,12 +20,17 @@ module Ransack
it 'avoids creating compound predicates if compounds: false' do it 'avoids creating compound predicates if compounds: false' do
Ransack.configure do |config| Ransack.configure do |config|
config.add_predicate :test_predicate_without_compound, compounds: false config.add_predicate(
:test_predicate_without_compound,
compounds: false
)
end end
Ransack.predicates
Ransack.predicates.should have_key 'test_predicate_without_compound' .should have_key 'test_predicate_without_compound'
Ransack.predicates.should_not have_key 'test_predicate_without_compound_any' Ransack.predicates
Ransack.predicates.should_not have_key 'test_predicate_without_compound_all' .should_not have_key 'test_predicate_without_compound_any'
Ransack.predicates
.should_not have_key 'test_predicate_without_compound_all'
end end
it 'should have default value for search key' do it 'should have default value for search key' do
@ -48,7 +53,11 @@ module Ransack
it 'adds predicates that take arrays, overriding compounds' do it 'adds predicates that take arrays, overriding compounds' do
Ransack.configure do |config| 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 end
Ransack.predicates['test_array_predicate'].wants_array.should eq true Ransack.predicates['test_array_predicate'].wants_array.should eq true

View File

@ -22,14 +22,17 @@ module Ransack
describe 'eq' do describe 'eq' do
it 'generates an equality condition for boolean true' do it 'generates an equality condition for boolean true' do
@s.awesome_eq = true @s.awesome_eq = true
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" field = "#{quote_table_name("people")}.#{
@s.result.to_sql.should match /#{field} = #{ActiveRecord::Base.connection.quoted_true}/ quote_column_name("awesome")}"
@s.result.to_sql.should match /#{field} = #{
ActiveRecord::Base.connection.quoted_true}/
end end
it 'generates an equality condition for boolean false' do it 'generates an equality condition for boolean false' do
@s.awesome_eq = false @s.awesome_eq = false
field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}" 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 end
it 'does not generate a condition for nil' do it 'does not generate a condition for nil' do

View File

@ -135,7 +135,8 @@ module Ransack
search = Search.new(Person, children_name_or_name_eq: 'Ernie') search = Search.new(Person, children_name_or_name_eq: 'Ernie')
search.result.should be_an ActiveRecord::Relation search.result.should be_an ActiveRecord::Relation
where = search.result.where_values.first 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 end
it 'evaluates polymorphic belongs_to association conditions contextually' do it 'evaluates polymorphic belongs_to association conditions contextually' do
@ -155,7 +156,8 @@ module Ransack
where = search.result.where_values.first where = search.result.where_values.first
where.to_sql.should match /#{children_people_name_field} = 'Ernie'/ where.to_sql.should match /#{children_people_name_field} = 'Ernie'/
where.to_sql.should match /#{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 end
it 'evaluates arrays of groupings' do it 'evaluates arrays of groupings' do
@ -309,7 +311,7 @@ module Ransack
end end
it 'raises NoMethodError when sent an invalid attribute' do it 'raises NoMethodError when sent an invalid attribute' do
expect {@s.blah}.to raise_error NoMethodError expect { @s.blah }.to raise_error NoMethodError
end end
it 'sets condition attributes when sent valid attributes' do it 'sets condition attributes when sent valid attributes' do
@ -318,7 +320,9 @@ module Ransack
end end
it 'allows chaining to access nested conditions' do 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' @s.groupings.first.children_name_eq.should eq 'Ernie'
end end
end end