Module: WithOrder::ActionViewExtension

Includes:
HashExtraction
Defined in:
lib/with_order/action_view_extension.rb

Instance Method Summary (collapse)

Methods included from HashExtraction

#extract_hash_value

Instance Method Details



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/with_order/action_view_extension.rb', line 5

def link_with_order(*args, &block)
  text = block_given? ? capture(&block) : args.shift
  scope, field, html_options = *args
  html_options ||= {}

  dir = html_options.delete(:dir) || (
    (scope.current_order[:field] == field and (scope.current_order[:dir].blank? or scope.current_order[:dir].downcase == :asc)) ?
    'desc' :
    'asc'
  )

  param_namespace = scope.current_order[:param_namespace]
  scoped_params = (param_namespace ? self.extract_hash_value(params, param_namespace) : params).try(:dup) || {}
  scoped_params.merge!({order: "#{field}-#{dir}"})

  link_to(text, param_namespace ? params.merge({param_namespace => scoped_params}) : scoped_params, html_options)
end