Class: Pulse::Table::Cell

Inherits:
Component
  • Object
show all
Defined in:
app/components/pulse/table/cell.rb

Overview

Renders a table cell

Constant Summary collapse

HEAD_DEFAULT_SCHEME =
:xs
HEAD_SCHEME_MAPPINGS =
{
  sm: 'pulse-px-3 pulse-py-4 print:pulse-px-2 print:pulse-py-3 ' \
      'pulse-text-sm print:pulse-text-xs pulse-font-base',
  xs: 'pulse-px-2 pulse-py-4 print:pulse-py-2 pulse-text-xs ' \
      'print:pulse-text-2xs pulse-font-medium'
}.freeze
HEAD_SCHEME_OPTIONS =
HEAD_SCHEME_MAPPINGS.keys.freeze
DEFAULT_CELL_CLASSES =
'pulse-text-start first:pulse-ps-4 ' \
'last:pulse-pe-4 pulse-px-2 pulse-text-gray ' \
'print:first:pulse-ps-2 print:last:pulse-pe-2'
DEFAULT_TH_CLASSES =
'pulse-uppercase pulse-tracking-wider'
DEFAULT_TD_CLASSES =
'pulse-py-2 pulse-whitespace-nowrap ' \
'pulse-text-sm print:pulse-text-xs pulse-text-gray'

Constants inherited from Component

Component::TW_MERGE

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

generate_id, #merge_attributes, #merge_classes

Methods included from SvgHelper

#render_svg

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean

Methods included from AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Constructor Details

#initialize(sort_by: nil, current_sort_column: nil, current_sort_direction: nil, additional_params: {}, head_scheme: HEAD_DEFAULT_SCHEME, footer: nil, title_classes: '', **options) ⇒ Cell

Returns a new instance of Cell.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/pulse/table/cell.rb', line 26

def initialize(sort_by: nil, current_sort_column: nil,
               current_sort_direction: nil, additional_params: {},
               head_scheme: HEAD_DEFAULT_SCHEME, footer: nil,
               title_classes: '', **options)
  @sort_by = sort_by
  @current_sort_column = current_sort_column
  @current_sort_direction = current_sort_direction
  @additional_params = additional_params
  @footer = footer
  @title_classes = title_classes
  @options = options
  @options[:classes] = merge_classes(
    DEFAULT_CELL_CLASSES,
    header_or_footer? ? DEFAULT_TH_CLASSES : DEFAULT_TD_CLASSES,
    header_or_footer? ? HEAD_SCHEME_MAPPINGS[head_scheme] : '',
    options[:classes]
  )
end

Instance Attribute Details

#additional_paramsObject (readonly)

Returns the value of attribute additional_params.



7
8
9
# File 'app/components/pulse/table/cell.rb', line 7

def additional_params
  @additional_params
end

#current_sort_columnObject (readonly)

Returns the value of attribute current_sort_column.



7
8
9
# File 'app/components/pulse/table/cell.rb', line 7

def current_sort_column
  @current_sort_column
end

#current_sort_directionObject (readonly)

Returns the value of attribute current_sort_direction.



7
8
9
# File 'app/components/pulse/table/cell.rb', line 7

def current_sort_direction
  @current_sort_direction
end

Returns the value of attribute footer.



7
8
9
# File 'app/components/pulse/table/cell.rb', line 7

def footer
  @footer
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



7
8
9
# File 'app/components/pulse/table/cell.rb', line 7

def sort_by
  @sort_by
end

Instance Method Details

#activeObject



57
58
59
# File 'app/components/pulse/table/cell.rb', line 57

def active
  sort_by && sort_by == current_sort_column
end

#dirObject



65
66
67
# File 'app/components/pulse/table/cell.rb', line 65

def dir
  current_sort_direction
end

#directionObject



81
82
83
# File 'app/components/pulse/table/cell.rb', line 81

def direction
  params[:sort].nil? || params[:sort] == sort_by ? sort_dir : nil
end

#header_or_footer?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/components/pulse/table/cell.rb', line 73

def header_or_footer?
  @options[:tag] == 'th' || @footer
end

#iconObject



69
70
71
# File 'app/components/pulse/table/cell.rb', line 69

def icon
  active ? "sort_#{dir}" : 'sort'
end

#optionsObject



45
46
47
48
49
50
51
# File 'app/components/pulse/table/cell.rb', line 45

def options
  { tag: 'td' }.deep_merge(@options).tap do |args|
    args[:classes] = class_names(
      args[:classes]
    )
  end
end

#sort_dirObject



85
86
87
# File 'app/components/pulse/table/cell.rb', line 85

def sort_dir
  params[:direction] == 'asc' ? 'desc' : 'asc'
end

#sortable?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/components/pulse/table/cell.rb', line 61

def sortable?
  sort_by && params[:q].blank?
end

#title_classesObject



53
54
55
# File 'app/components/pulse/table/cell.rb', line 53

def title_classes
  header_or_footer? ? @title_classes : ''
end

#url_optionsObject



77
78
79
# File 'app/components/pulse/table/cell.rb', line 77

def url_options
  { sort: sort_by, direction: }.merge(additional_params)
end