Class: Wallaby::Sorting::LinkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/services/wallaby/sorting/link_builder.rb

Overview

Build link for sorting

Constant Summary collapse

SORT_STRATEGIES =
{ single: SingleBuilder }.with_indifferent_access.freeze

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator, params, helper, strategy) ⇒ LinkBuilder

Returns a new instance of LinkBuilder.

Parameters:

  • model_decorator (ModelDecorator)
  • params (ActionController::Parameters)
  • helper (ActionView::Helpers)


15
16
17
18
19
20
# 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

Instance Method Details

#build(field_name) ⇒ String

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

Parameters:

  • field_name (String)

Returns:

  • (String)

    link or text



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/services/wallaby/sorting/link_builder.rb', line 38

def build(field_name)
   = @model_decorator. field_name
  label = @model_decorator.index_label_of field_name
  return label unless sortable? field_name, 

  sort_field_name = [:sort_field_name] || field_name
  url_params = next_builder.next_params sort_field_name
  index_link(
    model_class, url_params: url_params.merge(with_query: true)
  ) { label }
end

#current_sortHash

To return the sort hash converted from string value, e.g. ‘{ ’title’ => ‘asc’, ‘updated_at’ => ‘desc’ }‘ converted from `’title asc, updated_at desc’‘

Returns:

  • (Hash)

    current sort hash



25
26
27
# File 'lib/services/wallaby/sorting/link_builder.rb', line 25

def current_sort
  @current_sort ||= HashBuilder.build @params[:sort]
end