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 89 def initialize(search, attribute, args, params) @search = search @params = 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 @default_order = @options.delete :default_order end
Public Instance Methods
html_options(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 114 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) ) end
name()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 101 def name [ERB::Util.h(@label_text), order_indicator] .compact .join(Constants::NON_BREAKING_SPACE) .html_safe end
url_options()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 108 def url_options @params.merge( @options.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 192 def default_sort_order(attr_name) if Hash === @default_order @default_order[attr_name] else @default_order end end
detect_previous_sort_direction_and_invert_it(attr_name)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 178 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 end end
direction_arrow()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 212 def direction_arrow if @current_dir == Constants::DESC Constants::DESC_ARROW else Constants::ASC_ARROW end end
direction_text(dir)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 220 def direction_text(dir) if dir == Constants::DESC Constants::ASC else Constants::DESC end end
existing_sort_direction(attr_name = @field)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 186 def existing_sort_direction(attr_name = @field) if sort = @search.sorts.detect { |s| s && s.name == attr_name } sort.dir end end
extract_label_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 132 def extract_label_and_mutate_args!(args) if args.first.is_a? String args.shift else Translate.attribute(@field, context: @search.context) end end
extract_options_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 140 def extract_options_and_mutate_args!(args) if args.first.is_a? Hash args.shift.with_indifferent_access else {} end end
extract_sort_fields_and_mutate_args!(args)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 124 def extract_sort_fields_and_mutate_args!(args) if args.first.is_a? Array args.shift else [@field] end end
no_sort_direction_specified?(dir = @current_dir)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 208 def no_sort_direction_specified?(dir = @current_dir) !Constants::ASC_DESC.include?(dir) end
order_indicator()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 200 def order_indicator if @hide_indicator || no_sort_direction_specified? nil else direction_arrow end end
parse_sort(field)
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 170 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 165 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 148 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 152 def search_params @params[@search.context.search_key].presence || {} end
sort_params()
click to toggle source
# File lib/ransack/helpers/form_helper.rb, line 156 def sort_params sort_array = recursive_sort_params_build(@sort_fields) if sort_array.size == 1 sort_array.first else sort_array end end