class Ransack::Helpers::FormHelper::SortLink
Public Class Methods
new(search, attribute, args, params)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 99 def initialize(search, attribute, args, params) @search = search @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) || Ransack.options[:hide_sort_order_indicators] @default_order = @options.delete :default_order end
Public Instance Methods
default_arrow()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 120 def default_arrow Ransack.options[:default_arrow] end
down_arrow()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 116 def down_arrow Ransack.options[:down_arrow] end
html_options(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 137 def html_options(args) if args.empty? html_options = @options else deprecation_message = "Passing two trailing hashes to `sort_link` is deprecated, merge the trailing hashes into a single one." caller_location = caller_locations(2, 2).first warn "#{deprecation_message} (called at #{caller_location.path}:#{caller_location.lineno})" html_options = extract_options_and_mutate_args!(args) end html_options.merge( class: [['sort_link'.freeze, @current_dir], html_options[:class]] .compact.join(' '.freeze) ) end
name()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 124 def name [ERB::Util.h(@label_text), order_indicator] .compact .join(' '.freeze) .html_safe end
up_arrow()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 112 def up_arrow Ransack.options[:up_arrow] end
url_options()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 131 def url_options @params.except(:host).merge( @options.except(:class, :data, :host).merge( @search.context.search_key => search_and_sort_params)) end
Private Instance Methods
default_sort_order(attr_name)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 219 def default_sort_order(attr_name) return @default_order[attr_name] if Hash === @default_order @default_order end
detect_previous_sort_direction_and_invert_it(attr_name)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 206 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) || 'asc'.freeze end end
direction_text(dir)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 238 def direction_text(dir) return 'asc'.freeze if dir == 'desc'.freeze 'desc'.freeze end
existing_sort_direction(f = @field)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 214 def existing_sort_direction(f = @field) return unless sort = @search.sorts.detect { |s| s && s.name == f } sort.dir end
extract_label_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 168 def extract_label_and_mutate_args!(args) return args.shift if args[0].is_a?(String) Translate.attribute(@field, context: @search.context) end
extract_options_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 173 def extract_options_and_mutate_args!(args) return args.shift.with_indifferent_access if args[0].is_a?(Hash) {} end
extract_sort_fields_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 163 def extract_sort_fields_and_mutate_args!(args) return args.shift if args[0].is_a?(Array) [@field] end
no_sort_direction_specified?(dir = @current_dir)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 234 def no_sort_direction_specified?(dir = @current_dir) dir != 'asc'.freeze && dir != 'desc'.freeze end
order_indicator()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 224 def order_indicator return if @hide_indicator return default_arrow if no_sort_direction_specified? if @current_dir == 'desc'.freeze up_arrow else down_arrow end end
parameters_hash(params)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 155 def parameters_hash(params) if params.respond_to?(:to_unsafe_h) params.to_unsafe_h else params end end
parse_sort(field)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 198 def parse_sort(field) attr_name, new_dir = field.to_s.split(/\s+/) if no_sort_direction_specified?(new_dir) new_dir = detect_previous_sort_direction_and_invert_it(attr_name) end "#{attr_name} #{new_dir}" end
recursive_sort_params_build(fields)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 193 def recursive_sort_params_build(fields) return [] if fields.empty? [parse_sort(fields[0])] + recursive_sort_params_build(fields.drop 1) end
search_and_sort_params()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 178 def search_and_sort_params search_params.merge(s: sort_params) end
search_params()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 182 def search_params query_params = @params[@search.context.search_key] query_params.is_a?(Hash) ? query_params : {} end
sort_params()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 187 def sort_params sort_array = recursive_sort_params_build(@sort_fields) return sort_array[0] if sort_array.length == 1 sort_array end