Class: DryCrudJsonapi::Pager

Inherits:
Object
  • Object
show all
Defined in:
app/domain/dry_crud_jsonapi/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, model_class, params = {}) ⇒ Pager

Returns a new instance of Pager.



9
10
11
12
13
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 9

def initialize(scope, model_class, params = {})
  @scope = scope
  @model_class = model_class
  @params = params.except(:controller, :action, :format)
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



5
6
7
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 5

def model_class
  @model_class
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 5

def params
  @params
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 5

def scope
  @scope
end

Instance Method Details

#first_pageObject



19
20
21
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 19

def first_page
  [:first, path(page: 1)]
end

#last_pageObject



23
24
25
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 23

def last_page
  [:last, path(page: total_pages)]
end

#next_pageObject



27
28
29
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 27

def next_page
  [:next, path(page: current_page + 1)] unless current_page >= total_pages
end

#path(params = {}) ⇒ Object



35
36
37
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 35

def path(params = {})
  polymorphic_path(model_class, params.merge(params))
end

#prev_pageObject



31
32
33
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 31

def prev_page
  [:prev, path(page: current_page - 1)] unless current_page <= 1
end

#renderObject



15
16
17
# File 'app/domain/dry_crud_jsonapi/pager.rb', line 15

def render
  [first_page, last_page, prev_page, next_page].compact.to_h
end