Code cleanup
This commit is contained in:
parent
984059e63f
commit
7d452627dd
|
@ -2,7 +2,7 @@
|
|||
if Arel::Nodes::And < Arel::Nodes::Binary
|
||||
class Ransack::Visitor
|
||||
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
|
||||
|
||||
if nodes.size > 1
|
||||
|
|
|
@ -84,7 +84,7 @@ module Ransack
|
|||
def get_association(str, parent = @base)
|
||||
klass = klassify parent
|
||||
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
|
||||
|
||||
def join_dependency(relation)
|
||||
|
|
|
@ -6,15 +6,23 @@ module Ransack
|
|||
if record.is_a?(Ransack::Search)
|
||||
search = record
|
||||
options[:url] ||= polymorphic_path(search.klass)
|
||||
elsif record.is_a?(Array) && (search = record.detect {|o| o.is_a?(Ransack::Search)})
|
||||
options[:url] ||= polymorphic_path(record.map {|o| o.is_a?(Ransack::Search) ? o.klass : o})
|
||||
elsif record.is_a?(Array) &&
|
||||
(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
|
||||
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
|
||||
options[:html] ||= {}
|
||||
html_options = {
|
||||
:class => options[:class].present? ? "#{options[:class]}" : "#{search.klass.to_s.underscore}_search",
|
||||
:id => options[:id].present? ? "#{options[:id]}" : "#{search.klass.to_s.underscore}_search",
|
||||
:class => options[:class].present? ?
|
||||
"#{options[:class]}" :
|
||||
"#{search.klass.to_s.underscore}_search",
|
||||
:id => options[:id].present? ?
|
||||
"#{options[:id]}" :
|
||||
"#{search.klass.to_s.underscore}_search",
|
||||
:method => :get
|
||||
}
|
||||
options[:as] ||= 'q'
|
||||
|
@ -31,15 +39,23 @@ module Ransack
|
|||
search = search.first
|
||||
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
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -57,7 +73,8 @@ module Ransack
|
|||
css = ['sort_link', current_dir].compact.join(' ')
|
||||
html_options[:class] = [css, html_options[:class]].compact.join(' ')
|
||||
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_for_url = params.merge options
|
||||
|
||||
|
@ -67,9 +84,14 @@ module Ransack
|
|||
url_for(options_for_url)
|
||||
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,
|
||||
html_options
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -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_reader :predicate
|
||||
|
||||
|
@ -18,9 +19,11 @@ module Ransack
|
|||
:m => combinator,
|
||||
: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
|
||||
predicate.validate(condition.values, condition.default_type) ? condition : nil
|
||||
predicate.validate(condition.values, condition.default_type) ?
|
||||
condition : nil
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -37,7 +40,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?
|
||||
|
@ -62,7 +66,8 @@ module Ransack
|
|||
self.attributes << attr if attr.valid?
|
||||
end
|
||||
else
|
||||
raise ArgumentError, "Invalid argument (#{args.class}) supplied to attributes="
|
||||
raise ArgumentError,
|
||||
"Invalid argument (#{args.class}) supplied to attributes="
|
||||
end
|
||||
end
|
||||
alias :a= :attributes=
|
||||
|
@ -85,7 +90,8 @@ module Ransack
|
|||
self.values << val
|
||||
end
|
||||
else
|
||||
raise ArgumentError, "Invalid argument (#{args.class}) supplied to values="
|
||||
raise ArgumentError,
|
||||
"Invalid argument (#{args.class}) supplied to values="
|
||||
end
|
||||
end
|
||||
alias :v= :values=
|
||||
|
@ -95,7 +101,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def combinator=(val)
|
||||
@combinator = ['and', 'or'].detect {|v| v == val.to_s} || nil
|
||||
@combinator = ['and', 'or'].detect { |v| v == val.to_s } || nil
|
||||
end
|
||||
alias :m= :combinator=
|
||||
alias :m :combinator
|
||||
|
@ -113,7 +119,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)
|
||||
|
@ -131,7 +139,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)
|
||||
|
@ -164,7 +173,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
|
||||
|
@ -180,16 +191,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
|
||||
|
@ -201,9 +213,15 @@ module Ransack
|
|||
end
|
||||
|
||||
def inspect
|
||||
data =[['attributes', a.try(:map, &:name)], ['predicate', p], ['combinator', m], ['values', v.try(:map, &:value)]].reject { |e|
|
||||
e[1].blank?
|
||||
}.map { |v| "#{v[0]}: #{v[1]}" }.join(', ')
|
||||
data = [
|
||||
['attributes', a.try(:map, &:name)],
|
||||
['predicate', p],
|
||||
['combinator', m],
|
||||
['values', v.try(:map, &:value)]
|
||||
]
|
||||
.reject { |e| e[1].blank? }
|
||||
.map { |v| "#{v[0]}: #{v[1]}" }
|
||||
.join(', ')
|
||||
"Condition <#{data}>"
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def names_by_decreasing_length
|
||||
names.sort {|a,b| b.length <=> a.length}
|
||||
names.sort { |a,b| b.length <=> a.length }
|
||||
end
|
||||
|
||||
def named(name)
|
||||
|
|
|
@ -13,11 +13,11 @@ module Ransack
|
|||
@formatter = opts[:formatter]
|
||||
@callable = opts[:callable] || block ||
|
||||
(@klass.method(name) if @klass.respond_to?(name)) ||
|
||||
proc {|parent| parent.table[name]}
|
||||
proc { |parent| parent.table[name] }
|
||||
end
|
||||
|
||||
def attr_from(bindable)
|
||||
call(*args.map {|arg| bindable.send(arg)})
|
||||
call(*args.map { |arg| bindable.send(arg) })
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -10,7 +10,7 @@ module Ransack
|
|||
end
|
||||
|
||||
def visit_Array(object)
|
||||
object.map {|o| accept(o)}.compact
|
||||
object.map { |o| accept(o) }.compact
|
||||
end
|
||||
|
||||
def visit_Ransack_Nodes_Condition(object)
|
||||
|
@ -22,7 +22,7 @@ module Ransack
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
if nodes.size > 1
|
||||
|
@ -33,7 +33,7 @@ module Ransack
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
if nodes.size > 1
|
||||
|
|
|
@ -4,7 +4,8 @@ require 'sham'
|
|||
require 'faker'
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -12,10 +13,9 @@ Sham.define do
|
|||
name { Faker::Name.name }
|
||||
title { Faker::Lorem.sentence }
|
||||
body { Faker::Lorem.paragraph }
|
||||
salary {|index| 30000 + (index * 1000)}
|
||||
salary { |index| 30000 + (index * 1000) }
|
||||
tag_name { Faker::Lorem.words(3).join(' ') }
|
||||
note { Faker::Lorem.words(7).join(' ') }
|
||||
end
|
||||
|
||||
Schema.create
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ module Ransack
|
|||
end
|
||||
|
||||
# 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]
|
||||
# end
|
||||
#
|
||||
|
|
|
@ -6,7 +6,8 @@ require 'ransack'
|
|||
Time.zone = 'Eastern Time (US & Canada)'
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -14,7 +15,7 @@ Sham.define do
|
|||
name { Faker::Name.name }
|
||||
title { Faker::Lorem.sentence }
|
||||
body { Faker::Lorem.paragraph }
|
||||
salary {|index| 30000 + (index * 1000)}
|
||||
salary { |index| 30000 + (index * 1000) }
|
||||
tag_name { Faker::Lorem.words(3).join(' ') }
|
||||
note { Faker::Lorem.words(7).join(' ') }
|
||||
only_admin { Faker::Lorem.words(3).join(' ') }
|
||||
|
@ -29,7 +30,8 @@ RSpec.configure do |config|
|
|||
config.before(:suite) do
|
||||
puts '=' * 80
|
||||
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
|
||||
Schema.create
|
||||
end
|
||||
|
|
|
@ -38,7 +38,7 @@ class Person < ActiveRecord::Base
|
|||
:source => :comments, :foreign_key => :person_id
|
||||
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]
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue