moduleRansackmoduleHelpersmoduleFormHelper# +search_form_for+## <%= search_form_for(@q) do |f| %>#defsearch_form_for(record,options={},&proc)ifrecord.is_a?Ransack::Searchsearch=recordoptions[:url]||=polymorphic_path(search.klass,format: options.delete(:format))elsifrecord.is_a?(Array)&&(search=record.detect{|o|o.is_a?(Ransack::Search)})options[:url]||=polymorphic_path(options_for(record),format: options.delete(:format))elseraiseArgumentError,'No Ransack::Search object was provided to search_form_for!'endoptions[:html]||={}html_options={class: html_option_for(options[:class],search),id: html_option_for(options[:id],search),method: :get}options[:as]||=Ransack.options[:search_key]options[:html].reverse_merge!(html_options)options[:builder]||=FormBuilderform_for(record,options,&proc)end# +sort_link+## <%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %>## You can also use a block:## <%= sort_link(@q, :name, [:name, 'kind ASC']) do %># <strong>Player Name</strong># <% end %>#defsort_link(search_object,attribute,*args,&block)search,routing_proxy=extract_search_and_routing_proxy(search_object)unlessSearch===searchraiseTypeError,'First argument must be a Ransack::Search!'endargs.unshift(capture(&block))ifblock_given?s=SortLink.new(search,attribute,args,params,&block)link_to(s.name,url(routing_proxy,s.url_options),s.html_options(args))endprivatedefoptions_for(record)record.map{|r|parse_record(r)}enddefparse_record(object)returnobject.klassifobject.is_a?(Ransack::Search)objectenddefhtml_option_for(option,search)returnoption.to_sifoption.present?"#{search.klass.to_s.underscore}_search"enddefextract_search_and_routing_proxy(search)return[search[1],search[0]]ifsearch.is_a?(Array)[search,nil]enddefurl(routing_proxy,options_for_url)ifrouting_proxy&&respond_to?(routing_proxy)send(routing_proxy).url_for(options_for_url)elseurl_for(options_for_url)endendclassSortLinkdefinitialize(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_orderenddefup_arrow'▼'.freezeenddefdown_arrow'▲'.freezeenddefname[ERB::Util.h(@label_text),order_indicator].compact.join(' '.freeze).html_safeenddefurl_options@params.merge(@options.merge(@search.context.search_key=>search_and_sort_params))enddefhtml_options(args)html_options=extract_options_and_mutate_args!(args)html_options.merge(class: [['sort_link'.freeze,@current_dir],html_options[:class]].compact.join(' '.freeze))endprivatedefparameters_hash(params)if::ActiveRecord::VERSION::MAJOR==5&¶ms.respond_to?(:to_unsafe_h)params.to_unsafe_helseparamsendenddefextract_sort_fields_and_mutate_args!(args)returnargs.shiftifargs[0].is_a?(Array)[@field]enddefextract_label_and_mutate_args!(args)returnargs.shiftifargs[0].is_a?(String)Translate.attribute(@field,context: @search.context)enddefextract_options_and_mutate_args!(args)returnargs.shift.with_indifferent_accessifargs[0].is_a?(Hash){}enddefsearch_and_sort_paramssearch_params.merge(s: sort_params)enddefsearch_params@params[@search.context.search_key].presence||{}enddefsort_paramssort_array=recursive_sort_params_build(@sort_fields)returnsort_array[0]ifsort_array.length==1sort_arrayenddefrecursive_sort_params_build(fields)return[]iffields.empty?[parse_sort(fields[0])]+recursive_sort_params_build(fields.drop1)enddefparse_sort(field)attr_name,new_dir=field.to_s.split(/\s+/)ifno_sort_direction_specified?(new_dir)new_dir=detect_previous_sort_direction_and_invert_it(attr_name)end"#{attr_name}#{new_dir}"enddefdetect_previous_sort_direction_and_invert_it(attr_name)ifsort_dir=existing_sort_direction(attr_name)direction_text(sort_dir)elsedefault_sort_order(attr_name)||'asc'.freezeendenddefexisting_sort_direction(f=@field)returnunlesssort=@search.sorts.detect{|s|s&&s.name==f}sort.direnddefdefault_sort_order(attr_name)return@default_order[attr_name]ifHash===@default_order@default_orderenddeforder_indicatorreturnif@hide_indicator||no_sort_direction_specified?if@current_dir=='desc'.freezeup_arrowelsedown_arrowendenddefno_sort_direction_specified?(dir=@current_dir)dir!='asc'.freeze&&dir!='desc'.freezeenddefdirection_text(dir)return'asc'.freezeifdir=='desc'.freeze'desc'.freezeendendendendend