Sha256: 6908077c30f620cb94baa5c5a4cbabe611f0e5b024aa26f1b71d6aa0132715d6

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module WithOrder
  module ActionViewExtension
    include WithOrder::HashExtraction

    def link_with_order(*args, &block)
      text = scope = field = html_options = nil

      if block_given?
        text         = capture(&block)
        scope        = args[0]
        field        = args[1]
        html_options = args[2] || {}
      else
        text         = args[0]
        scope        = args[1]
        field        = args[2]
        html_options = args[3] || {}
      end

      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_order-0.1.0 lib/with_order/action_view_extension.rb