Module: Ransack::Helpers::FormHelper
- Defined in:
- lib/ransack/helpers/form_helper.rb
Instance Method Summary collapse
- #asc ⇒ Object
- #asc_arrow ⇒ Object
- #desc ⇒ Object
- #desc_arrow ⇒ Object
- #non_breaking_space ⇒ Object
- #search_form_for(record, options = {}, &proc) ⇒ Object
- #sort_link(search, attribute, *args) ⇒ Object
Instance Method Details
#asc ⇒ Object
5 6 7 |
# File 'lib/ransack/helpers/form_helper.rb', line 5 def asc 'asc'.freeze end |
#asc_arrow ⇒ Object
13 14 15 |
# File 'lib/ransack/helpers/form_helper.rb', line 13 def asc_arrow '▲'.freeze end |
#desc ⇒ Object
9 10 11 |
# File 'lib/ransack/helpers/form_helper.rb', line 9 def desc 'desc'.freeze end |
#desc_arrow ⇒ Object
17 18 19 |
# File 'lib/ransack/helpers/form_helper.rb', line 17 def desc_arrow '▼'.freeze end |
#non_breaking_space ⇒ Object
21 22 23 |
# File 'lib/ransack/helpers/form_helper.rb', line 21 def non_breaking_space ' '.freeze end |
#search_form_for(record, options = {}, &proc) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ransack/helpers/form_helper.rb', line 25 def search_form_for(record, = {}, &proc) if record.is_a?(Ransack::Search) search = record [:url] ||= polymorphic_path( search.klass, format: .delete(:format) ) elsif record.is_a?(Array) && (search = record.detect { |o| o.is_a?(Ransack::Search) }) [:url] ||= polymorphic_path( record.map { |o| o.is_a?(Ransack::Search) ? o.klass : o }, format: .delete(:format) ) else raise ArgumentError, "No Ransack::Search object was provided to search_form_for!" end [:html] ||= {} = { :class => [:class].present? ? "#{options[:class]}" : "#{search.klass.to_s.underscore}_search", :id => [:id].present? ? "#{options[:id]}" : "#{search.klass.to_s.underscore}_search", :method => :get } [:as] ||= 'q'.freeze [:html].reverse_merge!() [:builder] ||= FormBuilder form_for(record, , &proc) end |
#sort_link(search, attribute, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ransack/helpers/form_helper.rb', line 58 def sort_link(search, attribute, *args) # Extract out a routing proxy for url_for scoping later if search.is_a?(Array) routing_proxy = search.shift search = search.first end raise TypeError, "First argument must be a Ransack::Search!" unless Search === search search_params = params[search.context.search_key].presence || {}.with_indifferent_access attr_name = attribute.to_s 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 } prev_attr, prev_dir = existing_sort.name, existing_sort.dir end = args.first.is_a?(Hash) ? args.shift.dup : {} default_order = .delete :default_order current_dir = prev_attr == attr_name ? prev_dir : nil if current_dir new_dir = current_dir == desc ? asc : desc else new_dir = default_order || asc end = args.first.is_a?(Hash) ? args.shift.dup : {} css = ['sort_link', current_dir].compact.join(' ') [:class] = [css, [:class]].compact.join(' ') query_hash = {} query_hash[search.context.search_key] = search_params .merge(:s => "#{attr_name} #{new_dir}") .merge!(query_hash) = params.merge url = if routing_proxy && respond_to?(routing_proxy) send(routing_proxy).url_for() else url_for() end link_to( [ERB::Util.h(name), order_indicator_for(current_dir)] .compact .join(non_breaking_space) .html_safe, url, ) end |