lib/ransack/helpers/form_helper.rb in ransack-1.7.0 vs lib/ransack/helpers/form_helper.rb in ransack-1.8.0
- old
+ new
@@ -36,47 +36,45 @@
# +sort_link+
#
# <%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %>
#
- def sort_link(search_object, attribute, *args)
+ # You can also use a block:
+ #
+ # <%= sort_link(@q, :name, [:name, 'kind ASC']) do %>
+ # <strong>Player Name</strong>
+ # <% end %>
+ #
+ def sort_link(search_object, attribute, *args, &block)
search, routing_proxy = extract_search_and_routing_proxy(search_object)
unless Search === search
raise TypeError, 'First argument must be a Ransack::Search!'
end
- s = SortLink.new(search, attribute, args, params)
+ args.unshift(capture(&block)) if block_given?
+ s = SortLink.new(search, attribute, args, params, &block)
link_to(s.name, url(routing_proxy, s.url_options), s.html_options(args))
end
private
def options_for(record)
- record.map &method(:parse_record)
+ record.map { |r| parse_record(r) }
end
def parse_record(object)
- if object.is_a?(Ransack::Search)
- object.klass
- else
- object
- end
+ return object.klass if object.is_a?(Ransack::Search)
+ object
end
def html_option_for(option, search)
- if option.present?
- option.to_s
- else
- "#{search.klass.to_s.underscore}_search"
- end
+ return option.to_s if option.present?
+ "#{search.klass.to_s.underscore}_search"
end
def extract_search_and_routing_proxy(search)
- if search.is_a? Array
- [search.second, search.first]
- else
- [search, nil]
- end
+ return [search[1], search[0]] if search.is_a?(Array)
+ [search, nil]
end
def url(routing_proxy, options_for_url)
if routing_proxy && respond_to?(routing_proxy)
send(routing_proxy).url_for(options_for_url)
@@ -86,24 +84,25 @@
end
class SortLink
def initialize(search, attribute, args, params)
@search = search
- @params = params
+ @params = parameters_hash(params)
@field = attribute.to_s
@sort_fields = extract_sort_fields_and_mutate_args!(args).compact
@current_dir = existing_sort_direction
@label_text = extract_label_and_mutate_args!(args)
@options = extract_options_and_mutate_args!(args)
- @hide_indicator = @options.delete :hide_indicator
+ @hide_indicator = @options.delete(:hide_indicator) ||
+ Ransack.options[:hide_sort_order_indicators]
@default_order = @options.delete :default_order
end
def name
[ERB::Util.h(@label_text), order_indicator]
.compact
- .join(Constants::NON_BREAKING_SPACE)
+ .join(' '.freeze)
.html_safe
end
def url_options
@params.merge(
@@ -112,39 +111,38 @@
end
def html_options(args)
html_options = extract_options_and_mutate_args!(args)
html_options.merge(
- class: [[Constants::SORT_LINK, @current_dir], html_options[:class]]
- .compact.join(Constants::SPACE)
+ class: [['sort_link'.freeze, @current_dir], html_options[:class]]
+ .compact.join(' '.freeze)
)
end
private
- def extract_sort_fields_and_mutate_args!(args)
- if args.first.is_a? Array
- args.shift
+ def parameters_hash(params)
+ if ::ActiveRecord::VERSION::MAJOR == 5 && params.respond_to?(:to_unsafe_h)
+ params.to_unsafe_h
else
- [@field]
+ params
end
end
+ def extract_sort_fields_and_mutate_args!(args)
+ return args.shift if args[0].is_a?(Array)
+ [@field]
+ end
+
def extract_label_and_mutate_args!(args)
- if args.first.is_a? String
- args.shift
- else
- Translate.attribute(@field, context: @search.context)
- end
+ return args.shift if args[0].is_a?(String)
+ Translate.attribute(@field, context: @search.context)
end
def extract_options_and_mutate_args!(args)
- if args.first.is_a? Hash
- args.shift.with_indifferent_access
- else
- {}
- end
+ return args.shift.with_indifferent_access if args[0].is_a?(Hash)
+ {}
end
def search_and_sort_params
search_params.merge(s: sort_params)
end
@@ -153,15 +151,12 @@
@params[@search.context.search_key].presence || {}
end
def sort_params
sort_array = recursive_sort_params_build(@sort_fields)
- if sort_array.size == 1
- sort_array.first
- else
- sort_array
- end
+ return sort_array[0] if sort_array.length == 1
+ sort_array
end
def recursive_sort_params_build(fields)
return [] if fields.empty?
[parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1)
@@ -177,55 +172,41 @@
def detect_previous_sort_direction_and_invert_it(attr_name)
if sort_dir = existing_sort_direction(attr_name)
direction_text(sort_dir)
else
- default_sort_order(attr_name) || Constants::ASC
+ default_sort_order(attr_name) || 'asc'.freeze
end
end
- def existing_sort_direction(attr_name = @field)
- if sort = @search.sorts.detect { |s| s && s.name == attr_name }
- sort.dir
- end
+ def existing_sort_direction(f = @field)
+ return unless sort = @search.sorts.detect { |s| s && s.name == f }
+ sort.dir
end
def default_sort_order(attr_name)
- if Hash === @default_order
- @default_order[attr_name]
- else
- @default_order
- end
+ return @default_order[attr_name] if Hash === @default_order
+ @default_order
end
def order_indicator
- if @hide_indicator || no_sort_direction_specified?
- nil
- else
- direction_arrow
- end
+ return if @hide_indicator || no_sort_direction_specified?
+ direction_arrow
end
def no_sort_direction_specified?(dir = @current_dir)
- !Constants::ASC_DESC.include?(dir)
+ !['asc'.freeze, 'desc'.freeze].freeze.include?(dir)
end
def direction_arrow
- if @current_dir == Constants::DESC
- Constants::DESC_ARROW
- else
- Constants::ASC_ARROW
- end
+ return Constants::DESC_ARROW if @current_dir == 'desc'.freeze
+ Constants::ASC_ARROW
end
def direction_text(dir)
- if dir == Constants::DESC
- Constants::ASC
- else
- Constants::DESC
- end
+ return 'asc'.freeze if dir == 'desc'.freeze
+ 'desc'.freeze
end
-
end
end
end
end