Sha256: 221f93a866fd2fea0d37a4175a1563ab5c8c4817cd39b746fb3e6a0f92b29d5e

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module ActionSet
  module ViewHelpers
    def sort_link(column, title = nil)
      column = column.to_s
      title ||= sort_link_title(column)

      link_to title, sort_link_url(column)
    end

    private

    def sort_link_url(column)
      request.query_parameters.merge(controller: controller.controller_path,
                                     action: controller.action_name,
                                     sort: {
                                       column => sort_link_direction(column)
                                     })
    end

    def sort_link_title(column)
      I18n.t(column,
             scope: :attributes,
             default: default_sort_link_title(column))
    end

    def sort_link_direction(column)
      return 'asc' unless params[:sort]&.keys&.include?(column.to_s)

      sort_direction_inverses = { 'asc' => 'desc', 'desc' => 'asc' }
      sort_direction_inverses[params.dig(:sort, column)]
    end

    def default_sort_link_title(column)
      ActiveSet::Instructions::Entry::KeyPath.new(column).titleized
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
actionset-0.2.1 lib/action_set/view_helpers.rb
actionset-0.2.0 lib/action_set/view_helpers.rb
actionset-0.1.3 lib/action_set/view_helpers.rb
actionset-0.1.2 lib/action_set/view_helpers.rb
actionset-0.1.1 lib/action_set/view_helpers.rb