Class: PaginatedTable::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/paginated_table/page.rb

Constant Summary collapse

SORT_DIRECTIONS =
%w(asc desc)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Page

Returns a new instance of Page.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/paginated_table/page.rb', line 13

def initialize(attributes)
  @number = Integer(attributes[:number] || 1)
  raise ArgumentError unless @number > 0
  @rows = Integer(attributes[:rows] || PaginatedTable.configuration.rows_per_page)
  raise ArgumentError unless @rows > 0
  @sort_column = attributes[:sort_column] || 'id'
  @sort_direction = attributes[:sort_direction] || 'asc'
  raise ArgumentError unless SORT_DIRECTIONS.include?(@sort_direction)
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/paginated_table/page.rb', line 6

def number
  @number
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/paginated_table/page.rb', line 6

def rows
  @rows
end

#sort_columnObject (readonly)

Returns the value of attribute sort_column.



6
7
8
# File 'lib/paginated_table/page.rb', line 6

def sort_column
  @sort_column
end

#sort_directionObject (readonly)

Returns the value of attribute sort_direction.



6
7
8
# File 'lib/paginated_table/page.rb', line 6

def sort_direction
  @sort_direction
end

Class Method Details

.opposite_sort_direction(sort_direction) ⇒ Object



8
9
10
11
# File 'lib/paginated_table/page.rb', line 8

def self.opposite_sort_direction(sort_direction)
  index = SORT_DIRECTIONS.index(sort_direction)
  SORT_DIRECTIONS[index - 1]
end

Instance Method Details

#page_for_number(number) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/paginated_table/page.rb', line 23

def page_for_number(number)
  Page.new(
    :number => number,
    :rows => rows,
    :sort_column => sort_column,
    :sort_direction => sort_direction
  )
end

#page_for_sort_column(new_sort_column) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/paginated_table/page.rb', line 32

def page_for_sort_column(new_sort_column)
  if sort_column == new_sort_column
    new_sort_direction = self.class.opposite_sort_direction(sort_direction)
  else
    new_sort_direction = nil
  end
  Page.new(
    :number => 1,
    :rows => rows,
    :sort_column => new_sort_column,
    :sort_direction => new_sort_direction
  )
end