class SortableBy::TableHeader

Attributes

context[R]
header_html[RW]

Public Class Methods

new(path_helper:, model: nil, params: {}, context:, icon:) click to toggle source
# File lib/sortable_by/table_header.rb, line 11
def initialize(path_helper:, model: nil, params: {}, context:, icon:)
  @path_helper = path_helper
  @model = model
  @params = params
  @context = context
  @icon = icon.to_sym
end

Public Instance Methods

capture(block) click to toggle source
# File lib/sortable_by/table_header.rb, line 19
def capture(block)
  @html = context.capture do
    block.call(self)
  end
end
header(attribute, label: nil) click to toggle source
# File lib/sortable_by/table_header.rb, line 32
def header(attribute, label: nil)
  label ||= translated_attribute(attribute)
  content_tag :th, label
end
sortable(attribute, label: nil) click to toggle source
# File lib/sortable_by/table_header.rb, line 25
def sortable(attribute, label: nil)
  content_tag :th, class: classes_for_attribute(attribute) do
    concat(link_to_for_attribute(attribute, label))
    concat(sort_arrow_for_attribute(attribute))
  end
end
to_html() click to toggle source
# File lib/sortable_by/table_header.rb, line 41
def to_html
  content_tag :thead, to_row, class: 'tb-table-header'
end
to_row() click to toggle source
# File lib/sortable_by/table_header.rb, line 37
def to_row
  content_tag(:tr, @html)
end

Private Instance Methods

classes_for_attribute(attribute) click to toggle source
# File lib/sortable_by/table_header.rb, line 71
def classes_for_attribute(attribute)
  classes = ['sortable']
  if @params[:sort] == attribute.to_s
    classes << 'sortable-active'
    classes << "sortable-#{current_direction}"
  end
  classes.join(' ')
end
current_direction() click to toggle source
# File lib/sortable_by/table_header.rb, line 55
def current_direction
  @params[:dir] || 'asc'
end
determine_direction(attribute) click to toggle source
# File lib/sortable_by/table_header.rb, line 63
def determine_direction(attribute)
  if @params[:sort].nil? || @params[:sort] != attribute.to_s
    'asc'
  else
    inverted_direction
  end
end
inverted_direction() click to toggle source
# File lib/sortable_by/table_header.rb, line 59
def inverted_direction
  current_direction == 'asc' ? 'desc' : 'asc'
end
sort_arrow_for_attribute(attribute) click to toggle source
# File lib/sortable_by/table_header.rb, line 89
def sort_arrow_for_attribute(attribute)
  return '' unless @params[:sort] == attribute.to_s
  IconStrategy.send(@icon, context, current_direction) if IconStrategy.respond_to?(@icon)
end
translated_attribute(attribute) click to toggle source
# File lib/sortable_by/table_header.rb, line 47
def translated_attribute(attribute)
  if @model
    @model.human_attribute_name(attribute)
  else
    context.t(attribute)
  end
end