Class: Practical::Views::Navigation::PaginationComponent

Inherits:
ApplicationComponent
  • Object
show all
Defined in:
app/components/practical/views/navigation/pagination_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pagy:, request:, item_name: nil, i18n_key: nil) ⇒ PaginationComponent

Returns a new instance of PaginationComponent.



7
8
9
10
11
12
# File 'app/components/practical/views/navigation/pagination_component.rb', line 7

def initialize(pagy:, request:, item_name: nil, i18n_key: nil)
  @pagy = pagy
  @request = request
  @item_name = item_name
  @i18n_key = i18n_key
end

Instance Attribute Details

#i18n_keyObject

Returns the value of attribute i18n_key.



5
6
7
# File 'app/components/practical/views/navigation/pagination_component.rb', line 5

def i18n_key
  @i18n_key
end

#item_nameObject

Returns the value of attribute item_name.



5
6
7
# File 'app/components/practical/views/navigation/pagination_component.rb', line 5

def item_name
  @item_name
end

#pagyObject

Returns the value of attribute pagy.



5
6
7
# File 'app/components/practical/views/navigation/pagination_component.rb', line 5

def pagy
  @pagy
end

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'app/components/practical/views/navigation/pagination_component.rb', line 4

def request
  @request
end

Instance Method Details

#goto_page_dialog_idObject



64
65
66
# File 'app/components/practical/views/navigation/pagination_component.rb', line 64

def goto_page_dialog_id
  return [item_name, "pagy-goto-form"].compact.join("-")
end

#next_itemObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/components/practical/views/navigation/pagination_component.rb', line 50

def next_item
  classes = helpers.class_names(:page, :next, disabled: !pagy.next)

  tag.div(class: classes, role: :listitem){
    if pagy.next
      tag.a(href: pagy.page_url(pagy.next), title: Pagy::I18n.translate("pagy.aria_label.next")) {
        render icon_set.next_arrow
      }
    else
      render icon_set.next_arrow
    end
  }
end

#page_detail_textObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/components/practical/views/navigation/pagination_component.rb', line 14

def page_detail_text
  pagy_count = pagy.count
  if pagy_count == 0
    key = "pagy.info_tag.no_items"
  elsif pagy.pages == 1
    key = "pagy.info_tag.single_page"
  else
    key = "pagy.info_tag.multiple_pages"
  end

  item_name = item_name.presence || Pagy::I18n.translate(i18n_key || pagy.vars[:i18n_key], count: pagy_count)

  item_text = Pagy::I18n.translate(key,
                     item_name: item_name,
                     count: pagy_count, from: pagy.from, to: pagy.to
  )

  page_count_text = Pagy::I18n.translate("pagy.info_tag.page_count", page: pagy.page, count: pagy.pages)

  return Pagy::I18n.translate("pagy.info_tag.page_detail_text", item_text: item_text, page_count_text: page_count_text)
end

#page_item(item) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/components/practical/views/navigation/pagination_component.rb', line 68

def page_item(item)
  case item
  when Integer
    tag.div(class: :page, role: :listitem) {
      tag.a(item, href: pagy.page_url(item), title: Pagy::I18n.translate("pagy.nav.page_title", page_number: item))
    }
  when String
    tag.div(
      item,
      class: "page current", role: :listitem, title: Pagy::I18n.translate("pagy.nav.current_page_title", page_number: item)
    )
  when :gap
    render Practical::Views::Navigation::Pagination::GotoFormComponent.new(
      pagy: pagy,
      dialog_id: goto_page_dialog_id,
      page_detail_text: page_detail_text
    )
  end
end

#previous_itemObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/components/practical/views/navigation/pagination_component.rb', line 36

def previous_item
  classes = helpers.class_names(:page, :previous, disabled: !pagy.previous)

  tag.div(class: classes, role: :listitem){
    if pagy.previous
      tag.a(href: pagy.page_url(pagy.previous), title: Pagy::I18n.translate("pagy.aria_label.previous")) {
        render icon_set.previous_arrow
      }
    else
      render icon_set.previous_arrow
    end
  }
end