class UiBibz::Ui::Ux::Tables::Sortable

Public Class Methods

new(store, options) click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 5
def initialize(store, options)
  @store   = store
  @options = options
end

Public Instance Methods

header(column, name = nil) click to toggle source

header use i18n

# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 11
def header(column, name = nil)
  @column  = column
  defaults = [
    header_name(name),
    translate_headers_by_defaults,
    translate_headers_by_defaults_active_record,
    translate_headers_by_active_record,
    default_header_name(name)
  ].compact
  @name = UiBibz::Utils::Internationalization.new(translate_headers_by_model, default: defaults).translate
  sortable? ? sortable_link : title
end

Private Instance Methods

caret() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 89
def caret
  UiBibz::Ui::Core::Icons::Glyph.new("caret-#{direction == 'desc' ? 'up' : 'down'}").render
end
cls() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 93
def cls
  sort_direction == 'asc' ? 'dropup' : nil
end
column_name() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 101
def column_name
  @column.data_index || @column
end
default_header_name(name) click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 34
def default_header_name(name)
  name || @column.data_index.to_s.try('titleize')
end
default_parameters() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 73
def default_parameters
  %w[column_id direction search store_id controller sort page per_page]
end
direction() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 97
def direction
  sort_name == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
end
header_name(name) click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 30
def header_name(name)
  name || @column.name
end
name_with_caret() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 85
def name_with_caret
  sort_name.to_s == sort_column.to_s && @column.id.to_s == @store.column_id.to_s ? @name + caret : @name
end
sort_column() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 113
def sort_column
  @store.sort
end
sort_column_name() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 105
def sort_column_name
  @column.sort.nil? ? "#{@store.model.to_s.underscore.pluralize}.#{@column.data_index}" : @column.sort
end
sort_direction() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 117
def sort_direction
  %w[asc desc].include?(@store.direction) ? @store.direction : 'asc'
end
sort_name() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 109
def sort_name
  @sort_name || sort_column_name
end
sortable?() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 77
def sortable?
  @options[:sortable].nil? ? true : @options[:sortable]
end
title() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 81
def title
  sortable? ? name_with_caret : @name
end
translate_headers_by_active_record() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 50
def translate_headers_by_active_record
  "activerecord.attributes.#{@store.model.to_s.underscore}.#{@column.data_index}"
end
translate_headers_by_defaults() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 42
def translate_headers_by_defaults
  "ui_bibz.grid.headers.defaults.#{@column.data_index}"
end
translate_headers_by_defaults_active_record() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 46
def translate_headers_by_defaults_active_record
  "activerecord.attributes.defaults.#{@column.data_index}"
end
translate_headers_by_model() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 38
def translate_headers_by_model
  "ui_bibz.grid.headers.#{@store.model.to_s.underscore}.#{@column.data_index}"
end
url_options_o() click to toggle source
# File lib/ui_bibz/ui/ux/tables/extensions/sortable.rb, line 54
def url_options_o
  args = {
    controller: @store.controller,
    action: @store.action,
    search: @store.search,
    sort: sort_name,
    column_id: @column.id,
    direction: direction,
    only_path: true
  }
  args = args.merge({ id: @store.param_id }) if @store.param_id
  args = args.merge({ custom_sort: true, column_name: @column.data_index }) if @column.custom_sort
  args = args.merge({ parent: true }) if @column.parent
  args = args.merge({ store_id: @store.id }) unless @store.id.nil?
  args = args.merge({ link_type: 'column' })
  args = args.merge(@store.parameters.reject { |k, _v| default_parameters.include?(k.to_s) })
  args.with_indifferent_access
end