Class: FlexiAdmin::Components::Shared::Table::HeaderItemComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
lib/flexi_admin/components/shared/table/header_item_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ HeaderItemComponent

Returns a new instance of HeaderItemComponent.



7
8
9
10
11
12
13
14
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 7

def initialize(column)
  @column = column
  @justify = column.options[:justify] || :start
  @width = column.options[:width] || ""
  @attr = column.attribute
  @sort_by = column.sort_by
  @label = column.label
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



5
6
7
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 5

def attr
  @attr
end

#columnObject (readonly)

Returns the value of attribute column.



5
6
7
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 5

def column
  @column
end

#justifyObject (readonly)

Returns the value of attribute justify.



5
6
7
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 5

def justify
  @justify
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



5
6
7
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 5

def sort_by
  @sort_by
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 5

def width
  @width
end

Instance Method Details

#justify_classObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 36

def justify_class
  case justify.to_sym
  when :start
    "justify-content-start"
  when :end
    "justify-content-end"
  else
    "justify-content-start"
  end
end

#labelObject



16
17
18
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 16

def label
  @label || attr.to_s.humanize
end

#merged_classesObject



28
29
30
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 28

def merged_classes
  [width_class, justify_class].join(" ")
end

#orderObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 60

def order
  # Sorting by other column, so we need to reset the order
  return "asc" if view_context.context.params[:sort].to_s != sort_by.to_s

  order = case view_context.context.params[:order]
  when "asc"
    "desc"
  when "desc"
    "default"
  when "default"
    "asc"
  else
    "asc"
  end

  order
end

#sort_iconObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 47

def sort_icon
  return "bi-arrow-up-square" if view_context.context.params[:sort].to_s != sort_by.to_s

  case view_context.context.params[:order]
  when "asc"
    "bi-arrow-up-square-fill"
  when "desc"
    "bi-arrow-down-square-fill"
  else
    "bi-arrow-up-square"
  end
end

#sort_pathObject



20
21
22
23
24
25
26
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 20

def sort_path
  if order.blank?
    view_context.context.params.to_path(request.path)
  else
    view_context.context.params.merge({ sort: sort_by, order: }).to_path(request.path)
  end
end

#width_classObject



32
33
34
# File 'lib/flexi_admin/components/shared/table/header_item_component.rb', line 32

def width_class
  width.present? ? "col-#{width}" : "col"
end