Class: Practical::Views::Datatable::SortLinkComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/practical/views/datatable/sort_link_component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ElementHelper

#grab, #mix

Constructor Details

#initialize(url:, datatable_form:, sort_key:, options: {}) ⇒ SortLinkComponent

Returns a new instance of SortLinkComponent.



6
7
8
9
10
11
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 6

def initialize(url:, datatable_form:, sort_key:, options: {})
  self.url = url
  self.datatable_form = datatable_form
  self.sort_key = sort_key
  self.options = options
end

Instance Attribute Details

#datatable_formObject

Returns the value of attribute datatable_form.



4
5
6
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 4

def datatable_form
  @datatable_form
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 4

def options
  @options
end

#sort_keyObject

Returns the value of attribute sort_key.



4
5
6
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 4

def sort_key
  @sort_key
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 4

def url
  @url
end

Class Method Details

.inverted_sort_direction(datatable_form:, sort_key:) ⇒ Object



37
38
39
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 37

def self.inverted_sort_direction(datatable_form:, sort_key:)
  datatable_form.inverted_sort_direction_for(key: sort_key)
end

.merged_payload(datatable_form:, sort_key:) ⇒ Object



41
42
43
44
45
46
47
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 41

def self.merged_payload(datatable_form:, sort_key:)
  datatable_form.merged_payload(sort_key: sort_key,
                                sort_direction: inverted_sort_direction(datatable_form: datatable_form,
                                                                        sort_key: sort_key
                                                                      )
                               )
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 13

def call
  tag.a(**mix({href: url}, options)) {
    safe_join([
      render(icon),
      content
    ])
  }
end

#iconObject



22
23
24
25
26
27
28
29
30
31
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 22

def icon
  case sort_direction
  when "desc"
    return icon_set.descending_icon
  when "asc"
    return icon_set.ascending_icon
  else
    return icon_set.sort_icon
  end
end

#sort_directionObject



33
34
35
# File 'app/components/practical/views/datatable/sort_link_component.rb', line 33

def sort_direction
  datatable_form.sort_direction_for(key: sort_key)
end