Code cleanup
This commit is contained in:
parent
984059e63f
commit
7d452627dd
|
@ -2,7 +2,7 @@
|
||||||
if Arel::Nodes::And < Arel::Nodes::Binary
|
if Arel::Nodes::And < Arel::Nodes::Binary
|
||||||
class Ransack::Visitor
|
class Ransack::Visitor
|
||||||
def visit_Ransack_Nodes_And(object)
|
def visit_Ransack_Nodes_And(object)
|
||||||
nodes = object.values.map {|o| accept(o)}.compact
|
nodes = object.values.map { |o| accept(o) }.compact
|
||||||
return nil unless nodes.size > 0
|
return nil unless nodes.size > 0
|
||||||
|
|
||||||
if nodes.size > 1
|
if nodes.size > 1
|
||||||
|
|
|
@ -84,7 +84,7 @@ module Ransack
|
||||||
def get_association(str, parent = @base)
|
def get_association(str, parent = @base)
|
||||||
klass = klassify parent
|
klass = klassify parent
|
||||||
ransackable_association?(str, klass) &&
|
ransackable_association?(str, klass) &&
|
||||||
klass.reflect_on_all_associations.detect {|a| a.name.to_s == str}
|
klass.reflect_on_all_associations.detect { |a| a.name.to_s == str }
|
||||||
end
|
end
|
||||||
|
|
||||||
def join_dependency(relation)
|
def join_dependency(relation)
|
||||||
|
|
|
@ -6,15 +6,23 @@ module Ransack
|
||||||
if record.is_a?(Ransack::Search)
|
if record.is_a?(Ransack::Search)
|
||||||
search = record
|
search = record
|
||||||
options[:url] ||= polymorphic_path(search.klass)
|
options[:url] ||= polymorphic_path(search.klass)
|
||||||
elsif record.is_a?(Array) && (search = record.detect {|o| o.is_a?(Ransack::Search)})
|
elsif record.is_a?(Array) &&
|
||||||
options[:url] ||= polymorphic_path(record.map {|o| o.is_a?(Ransack::Search) ? o.klass : o})
|
(search = record.detect { |o| o.is_a?(Ransack::Search) })
|
||||||
|
options[:url] ||= polymorphic_path(record.
|
||||||
|
map { |o| o.is_a?(Ransack::Search) ? o.klass : o }
|
||||||
|
)
|
||||||
else
|
else
|
||||||
raise ArgumentError, "No Ransack::Search object was provided to search_form_for!"
|
raise ArgumentError,
|
||||||
|
"No Ransack::Search object was provided to search_form_for!"
|
||||||
end
|
end
|
||||||
options[:html] ||= {}
|
options[:html] ||= {}
|
||||||
html_options = {
|
html_options = {
|
||||||
:class => options[:class].present? ? "#{options[:class]}" : "#{search.klass.to_s.underscore}_search",
|
:class => options[:class].present? ?
|
||||||
:id => options[:id].present? ? "#{options[:id]}" : "#{search.klass.to_s.underscore}_search",
|
"#{options[:class]}" :
|
||||||
|
"#{search.klass.to_s.underscore}_search",
|
||||||
|
:id => options[:id].present? ?
|
||||||
|
"#{options[:id]}" :
|
||||||
|
"#{search.klass.to_s.underscore}_search",
|
||||||
:method => :get
|
:method => :get
|
||||||
}
|
}
|
||||||
options[:as] ||= 'q'
|
options[:as] ||= 'q'
|
||||||
|
@ -31,15 +39,23 @@ module Ransack
|
||||||
search = search.first
|
search = search.first
|
||||||
end
|
end
|
||||||
|
|
||||||
raise TypeError, "First argument must be a Ransack::Search!" unless Search === search
|
raise TypeError, "First argument must be a Ransack::Search!" unless
|
||||||
|
Search === search
|
||||||
|
|
||||||
search_params = params[search.context.search_key] || {}.with_indifferent_access
|
search_params = params[search.context.search_key] ||
|
||||||
|
{}.with_indifferent_access
|
||||||
|
|
||||||
attr_name = attribute.to_s
|
attr_name = attribute.to_s
|
||||||
|
|
||||||
name = (args.size > 0 && !args.first.is_a?(Hash)) ? args.shift.to_s : Translate.attribute(attr_name, :context => search.context)
|
name = (
|
||||||
|
if args.size > 0 && !args.first.is_a?(Hash)
|
||||||
|
args.shift.to_s
|
||||||
|
else
|
||||||
|
Translate.attribute(attr_name, :context => search.context)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
if existing_sort = search.sorts.detect {|s| s.name == attr_name}
|
if existing_sort = search.sorts.detect { |s| s.name == attr_name }
|
||||||
prev_attr, prev_dir = existing_sort.name, existing_sort.dir
|
prev_attr, prev_dir = existing_sort.name, existing_sort.dir
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,7 +73,8 @@ module Ransack
|
||||||
css = ['sort_link', current_dir].compact.join(' ')
|
css = ['sort_link', current_dir].compact.join(' ')
|
||||||
html_options[:class] = [css, html_options[:class]].compact.join(' ')
|
html_options[:class] = [css, html_options[:class]].compact.join(' ')
|
||||||
query_hash = {}
|
query_hash = {}
|
||||||
query_hash[search.context.search_key] = search_params.merge(:s => "#{attr_name} #{new_dir}")
|
query_hash[search.context.search_key] = search_params
|
||||||
|
.merge(:s => "#{attr_name} #{new_dir}")
|
||||||
options.merge!(query_hash)
|
options.merge!(query_hash)
|
||||||
options_for_url = params.merge options
|
options_for_url = params.merge options
|
||||||
|
|
||||||
|
@ -67,9 +84,14 @@ module Ransack
|
||||||
url_for(options_for_url)
|
url_for(options_for_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
link_to [ERB::Util.h(name), order_indicator_for(current_dir)].compact.join(' ').html_safe,
|
link_to(
|
||||||
|
[ERB::Util.h(name), order_indicator_for(current_dir)]
|
||||||
|
.compact
|
||||||
|
.join(' ')
|
||||||
|
.html_safe,
|
||||||
url,
|
url,
|
||||||
html_options
|
html_options
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -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_reader :predicate
|
attr_reader :predicate
|
||||||
|
|
||||||
|
@ -18,9 +19,11 @@ module Ransack
|
||||||
:m => combinator,
|
:m => combinator,
|
||||||
:v => predicate.wants_array ? Array(values) : [values]
|
:v => predicate.wants_array ? Array(values) : [values]
|
||||||
)
|
)
|
||||||
# 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,7 +40,8 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid?
|
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
|
end
|
||||||
|
|
||||||
def valid_arity?
|
def valid_arity?
|
||||||
|
@ -62,7 +66,8 @@ module Ransack
|
||||||
self.attributes << attr if attr.valid?
|
self.attributes << attr if attr.valid?
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Invalid argument (#{args.class}) supplied to attributes="
|
raise ArgumentError,
|
||||||
|
"Invalid argument (#{args.class}) supplied to attributes="
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :a= :attributes=
|
alias :a= :attributes=
|
||||||
|
@ -85,7 +90,8 @@ module Ransack
|
||||||
self.values << val
|
self.values << val
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Invalid argument (#{args.class}) supplied to values="
|
raise ArgumentError,
|
||||||
|
"Invalid argument (#{args.class}) supplied to values="
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :v= :values=
|
alias :v= :values=
|
||||||
|
@ -95,7 +101,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def combinator=(val)
|
def combinator=(val)
|
||||||
@combinator = ['and', 'or'].detect {|v| v == val.to_s} || nil
|
@combinator = ['and', 'or'].detect { |v| v == val.to_s } || nil
|
||||||
end
|
end
|
||||||
alias :m= :combinator=
|
alias :m= :combinator=
|
||||||
alias :m :combinator
|
alias :m :combinator
|
||||||
|
@ -113,7 +119,9 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
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
|
end
|
||||||
|
|
||||||
def build(params)
|
def build(params)
|
||||||
|
@ -131,7 +139,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)
|
||||||
|
@ -164,7 +173,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
|
||||||
|
@ -180,16 +191,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
|
||||||
|
@ -201,9 +213,15 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
data =[['attributes', a.try(:map, &:name)], ['predicate', p], ['combinator', m], ['values', v.try(:map, &:value)]].reject { |e|
|
data = [
|
||||||
e[1].blank?
|
['attributes', a.try(:map, &:name)],
|
||||||
}.map { |v| "#{v[0]}: #{v[1]}" }.join(', ')
|
['predicate', p],
|
||||||
|
['combinator', m],
|
||||||
|
['values', v.try(:map, &:value)]
|
||||||
|
]
|
||||||
|
.reject { |e| e[1].blank? }
|
||||||
|
.map { |v| "#{v[0]}: #{v[1]}" }
|
||||||
|
.join(', ')
|
||||||
"Condition <#{data}>"
|
"Condition <#{data}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def names_by_decreasing_length
|
def names_by_decreasing_length
|
||||||
names.sort {|a,b| b.length <=> a.length}
|
names.sort { |a,b| b.length <=> a.length }
|
||||||
end
|
end
|
||||||
|
|
||||||
def named(name)
|
def named(name)
|
||||||
|
|
|
@ -13,11 +13,11 @@ module Ransack
|
||||||
@formatter = opts[:formatter]
|
@formatter = opts[:formatter]
|
||||||
@callable = opts[:callable] || block ||
|
@callable = opts[:callable] || block ||
|
||||||
(@klass.method(name) if @klass.respond_to?(name)) ||
|
(@klass.method(name) if @klass.respond_to?(name)) ||
|
||||||
proc {|parent| parent.table[name]}
|
proc { |parent| parent.table[name] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def attr_from(bindable)
|
def attr_from(bindable)
|
||||||
call(*args.map {|arg| bindable.send(arg)})
|
call(*args.map { |arg| bindable.send(arg) })
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_Array(object)
|
def visit_Array(object)
|
||||||
object.map {|o| accept(o)}.compact
|
object.map { |o| accept(o) }.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_Ransack_Nodes_Condition(object)
|
def visit_Ransack_Nodes_Condition(object)
|
||||||
|
@ -22,7 +22,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_and(object)
|
def visit_and(object)
|
||||||
nodes = object.values.map {|o| accept(o)}.compact
|
nodes = object.values.map { |o| accept(o) }.compact
|
||||||
return nil unless nodes.size > 0
|
return nil unless nodes.size > 0
|
||||||
|
|
||||||
if nodes.size > 1
|
if nodes.size > 1
|
||||||
|
@ -33,7 +33,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def visit_or(object)
|
def visit_or(object)
|
||||||
nodes = object.values.map {|o| accept(o)}.compact
|
nodes = object.values.map { |o| accept(o) }.compact
|
||||||
return nil unless nodes.size > 0
|
return nil unless nodes.size > 0
|
||||||
|
|
||||||
if nodes.size > 1
|
if nodes.size > 1
|
||||||
|
|
|
@ -4,7 +4,8 @@ require 'sham'
|
||||||
require 'faker'
|
require 'faker'
|
||||||
require 'ransack'
|
require 'ransack'
|
||||||
|
|
||||||
Dir[File.expand_path('../../spec/{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
|
Dir[File.expand_path('../../spec/{helpers,support,blueprints}/*.rb', __FILE__)]
|
||||||
|
.each do |f|
|
||||||
require f
|
require f
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -12,10 +13,9 @@ Sham.define do
|
||||||
name { Faker::Name.name }
|
name { Faker::Name.name }
|
||||||
title { Faker::Lorem.sentence }
|
title { Faker::Lorem.sentence }
|
||||||
body { Faker::Lorem.paragraph }
|
body { Faker::Lorem.paragraph }
|
||||||
salary {|index| 30000 + (index * 1000)}
|
salary { |index| 30000 + (index * 1000) }
|
||||||
tag_name { Faker::Lorem.words(3).join(' ') }
|
tag_name { Faker::Lorem.words(3).join(' ') }
|
||||||
note { Faker::Lorem.words(7).join(' ') }
|
note { Faker::Lorem.words(7).join(' ') }
|
||||||
end
|
end
|
||||||
|
|
||||||
Schema.create
|
Schema.create
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
# in schema.rb, class Person:
|
# in schema.rb, class Person:
|
||||||
# ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
|
# ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent|
|
||||||
# parent.table[:name]
|
# parent.table[:name]
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
|
|
@ -6,7 +6,8 @@ require 'ransack'
|
||||||
Time.zone = 'Eastern Time (US & Canada)'
|
Time.zone = 'Eastern Time (US & Canada)'
|
||||||
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'support', '*.yml')]
|
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'support', '*.yml')]
|
||||||
|
|
||||||
Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
|
Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)]
|
||||||
|
.each do |f|
|
||||||
require f
|
require f
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ Sham.define do
|
||||||
name { Faker::Name.name }
|
name { Faker::Name.name }
|
||||||
title { Faker::Lorem.sentence }
|
title { Faker::Lorem.sentence }
|
||||||
body { Faker::Lorem.paragraph }
|
body { Faker::Lorem.paragraph }
|
||||||
salary {|index| 30000 + (index * 1000)}
|
salary { |index| 30000 + (index * 1000) }
|
||||||
tag_name { Faker::Lorem.words(3).join(' ') }
|
tag_name { Faker::Lorem.words(3).join(' ') }
|
||||||
note { Faker::Lorem.words(7).join(' ') }
|
note { Faker::Lorem.words(7).join(' ') }
|
||||||
only_admin { Faker::Lorem.words(3).join(' ') }
|
only_admin { Faker::Lorem.words(3).join(' ') }
|
||||||
|
@ -29,7 +30,8 @@ RSpec.configure do |config|
|
||||||
config.before(:suite) do
|
config.before(:suite) do
|
||||||
puts '=' * 80
|
puts '=' * 80
|
||||||
connection_name = ActiveRecord::Base.connection.adapter_name
|
connection_name = ActiveRecord::Base.connection.adapter_name
|
||||||
puts "Running specs against #{connection_name}, ActiveRecord #{ActiveRecord::VERSION::STRING} and ARel #{Arel::VERSION}..."
|
puts "Running specs against #{connection_name}, ActiveRecord #{
|
||||||
|
ActiveRecord::VERSION::STRING} and ARel #{Arel::VERSION}..."
|
||||||
puts '=' * 80
|
puts '=' * 80
|
||||||
Schema.create
|
Schema.create
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ class Person < ActiveRecord::Base
|
||||||
:source => :comments, :foreign_key => :person_id
|
:source => :comments, :foreign_key => :person_id
|
||||||
has_many :notes, :as => :notable
|
has_many :notes, :as => :notable
|
||||||
|
|
||||||
ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
|
ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent|
|
||||||
parent.table[:name]
|
parent.table[:name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue