Module: Wallaby::IndexHelper

Included in:
ResourcesHelper
Defined in:
lib/helpers/wallaby/index_helper.rb

Overview

Helper methods for index action

Instance Method Summary collapse

Instance Method Details

#all_labelString

Just a label

Returns:

  • (String)


8
9
10
# File 'lib/helpers/wallaby/index_helper.rb', line 8

def all_label
  wt 'filters.all'
end

Returns Export link for the given model_class.

Parameters:

  • model_class (Class)
  • url_params (Hash, ActionController::Parameters) (defaults to: {})

    extra URL params

Returns:

  • (String)

    Export link for the given model_class.



63
64
65
66
67
68
69
70
71
# File 'lib/helpers/wallaby/index_helper.rb', line 63

def export_link(model_class, url_params: {}, html_options: {})
  index_link(
    model_class,
    url_params: url_params.merge(format: 'csv', page: nil, per: nil, with_query: true),
    html_options: html_options
  ) do
    wt 'links.export', ext: 'CSV'
  end
end

#filter_label(filter_name, filters) ⇒ String

Returns filter label for the given field name.

Parameters:

  • filter_name (String, Symbol)
  • filters (Hash)

Returns:

  • (String)

    filter label for the given field name



29
30
31
32
# File 'lib/helpers/wallaby/index_helper.rb', line 29

def filter_label(filter_name, filters)
  # TODO: use locale for filter_name label
  filters[filter_name].try(:[], :label) || filter_name.to_s.humanize
end

Link for a given model class and filter name

Parameters:

  • model_class (Class)
  • filter_name (String, Symbol)
  • filters (Hash) (defaults to: {})
  • url_params (Hash, ActionController::Parameters) (defaults to: {})

Returns:

  • (String)

    HTML anchor link



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/helpers/wallaby/index_helper.rb', line 48

def filter_link(model_class, filter_name, filters: {}, url_params: {}, html_options: {})
  is_all = filter_name == :all
  config = filters[filter_name] || {}
  label = is_all ? all_label : filter_label(filter_name, filters)
  url_params[:filter] = config[:default] ? nil : filter_name

  index_link(
    model_class,
    url_params: url_params.merge(with_query: true), html_options: html_options
  ) { label }
end

#filter_name_by(filter_name, filters) ⇒ String

Returns filter name.

Parameters:

  • filter_name (String, Symbol)
  • filters (Hash)

Returns:

  • (String)

    filter name

See Also:



38
39
40
# File 'lib/helpers/wallaby/index_helper.rb', line 38

def filter_name_by(filter_name, filters)
  FilterUtils.filter_name_by filter_name, filters
end

#json_fields_of(decorated_collection, fields_from_params = params[:fields]) ⇒ Array<String>

If ‘:fields` parameter is given, only display fields that is in `index_field_names` Otherwise, `index_field_names`

Parameters:

Returns:

  • (Array<String>)

    a list of field names for json builder



17
18
19
20
21
22
23
24
# File 'lib/helpers/wallaby/index_helper.rb', line 17

def json_fields_of(decorated_collection, fields_from_params = params[:fields])
  return [] if decorated_collection.blank?

  decorated = decorated_collection.first
  index_field_names = decorated.index_field_names.map(&:to_s)
  fields = (fields_from_params.presence || index_field_names).split(/\s*,\s*/).flatten
  fields & index_field_names
end


75
76
77
78
79
80
# File 'lib/helpers/wallaby/index_helper.rb', line 75

def sort_link_builder(sorting_strategy = wallaby_controller.sorting_strategy)
  @sort_link_builder ||=
    Sorting::LinkBuilder.new(
      current_model_decorator, params.slice(:sort).permit!, self, sorting_strategy
    )
end