Code cleanup

This commit is contained in:
jonatack 2013-12-10 10:52:46 +01:00
parent 984059e63f
commit 7d452627dd
12 changed files with 86 additions and 44 deletions

View File

@ -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,13 +39,21 @@ 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
@ -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

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_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=
@ -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
@ -189,7 +200,8 @@ module Ransack
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

View File

@ -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
@ -18,4 +19,3 @@ Sham.define do
end end
Schema.create Schema.create

View File

@ -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
@ -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