Module: Sortabl::ActionViewExtensions::SortablHelper
- Defined in:
- lib/sortabl/sortabl_helper.rb
Instance Method Summary collapse
-
#sortabl_link(name = nil, attribute = nil, html_options = nil, &block) ⇒ Object
Renders sortabl link.
Instance Method Details
#sortabl_link(name = nil, attribute = nil, html_options = nil, &block) ⇒ Object
Renders sortabl link
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sortabl/sortabl_helper.rb', line 7 def sortabl_link(name = nil, attribute = nil, = nil, &block) , attribute, name = attribute, name, block if block_given? = || {} # Support custom sort_param # If sort_param isn't given, fall back to :sortabl as param sort_param = [:sort_param] || :sortabl .except!(:sort_param) # Remove current sortabl param from url and add default sortabl param sortabl_params = params.permit!.except(sort_param) sortabl_params[sort_param] = "#{attribute}_asc" # If there was already a sortabl param, invert direction or remove sortabl param if params[sort_param].present? if attribute.to_s == params[sort_param].gsub(/(_asc$|_desc$)/, '') sort_direction = params[sort_param].gsub(/^((?!desc$|asc$).)*/, '') sortabl_params[sort_param] = "#{attribute}_desc" if sort_direction == 'asc' sortabl_params[sort_param] = nil if sort_direction == 'desc' end end # Add sortabl html class to html_options [:class] = 'sortabl' + (sort_direction.present? ? " #{sort_direction}" : "") + ([:class].present? ? " #{[:class]}" : "") # Support remote true .except!(:remote).merge!({'data-remote': 'true'}) if [:remote] # Generate url from params url = url_for(sortabl_params) ["href".freeze] ||= url content_tag("a".freeze, name || url, , &block) end |