class Wallaby::Sorting::LinkBuilder

Build link for sorting

Constants

SORT_STRATEGIES

Public Class Methods

new(model_decorator, params, helper, strategy) click to toggle source

@param model_decorator [Wallaby::ModelDecorator] @param params [ActionController::Parameters] @param helper [ActionView::Helpers]

# File lib/services/wallaby/sorting/link_builder.rb, line 15
def initialize(model_decorator, params, helper, strategy)
  @model_decorator = model_decorator
  @params = params
  @helper = helper
  @strategy = strategy
end

Public Instance Methods

build(field_name) click to toggle source

Build sort link for given field name:

“` <a title=“Product” href=“/admin/products?sort=published_at+asc”>Name</a> “`

If the field is not sortable, it returns a text, e.g.:

“` Name “` @param field_name [String] @return [String] link or text

# File lib/services/wallaby/sorting/link_builder.rb, line 42
def build(field_name)
  metadata = @model_decorator.index_metadata_of field_name
  label = @model_decorator.index_label_of field_name
  return label unless sortable? field_name, metadata

  sort_field_name = metadata[:sort_field_name] || field_name
  url_params = next_builder.next_params sort_field_name
  url = url_for url_params.merge(with_query: true)
  index_link(model_class, options: { url: url }) { label }
end
current_sort() click to toggle source

To return the sort hash converted from string value, e.g. `{ 'title' => 'asc', 'updated_at' => 'desc' }` converted from `'title asc, updated_at desc'` @return [Hash] current sort hash

# File lib/services/wallaby/sorting/link_builder.rb, line 25
def current_sort
  @current_sort ||= HashBuilder.build @params[:sort]
end

Private Instance Methods

next_builder() click to toggle source

@return [Wallaby::Sorting::NextBuilder]

# File lib/services/wallaby/sorting/link_builder.rb, line 56
def next_builder
  @next_builder ||= begin
    klass = SORT_STRATEGIES[@strategy] || NextBuilder
    klass.new @params, current_sort
  end
end
sortable?(field_name, metadata) click to toggle source

@return [Boolean] true if sortable or `sort_field_name` is provided in the metadata

# File lib/services/wallaby/sorting/link_builder.rb, line 64
def sortable?(field_name, metadata)
  !metadata[:sort_disabled] && (@model_decorator.fields[field_name] || metadata[:sort_field_name])
end