Class: PaginatedTable::Page
- Inherits:
-
Object
- Object
- PaginatedTable::Page
- Defined in:
- lib/paginated_table/page.rb
Constant Summary collapse
- SORT_DIRECTIONS =
%w(asc desc)
- DEFAULT_PER_PAGE =
10
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#sort_column ⇒ Object
readonly
Returns the value of attribute sort_column.
-
#sort_direction ⇒ Object
readonly
Returns the value of attribute sort_direction.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Page
constructor
A new instance of Page.
- #page_for_number(number) ⇒ Object
- #page_for_sort_column(new_sort_column) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Page
Returns a new instance of Page.
14 15 16 17 18 19 20 21 22 |
# File 'lib/paginated_table/page.rb', line 14 def initialize(attributes) @number = Integer(attributes[:number] || 1) raise ArgumentError unless @number > 0 @rows = Integer(attributes[:rows] || DEFAULT_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
#number ⇒ Object (readonly)
Returns the value of attribute number.
7 8 9 |
# File 'lib/paginated_table/page.rb', line 7 def number @number end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
7 8 9 |
# File 'lib/paginated_table/page.rb', line 7 def rows @rows end |
#sort_column ⇒ Object (readonly)
Returns the value of attribute sort_column.
7 8 9 |
# File 'lib/paginated_table/page.rb', line 7 def sort_column @sort_column end |
#sort_direction ⇒ Object (readonly)
Returns the value of attribute sort_direction.
7 8 9 |
# File 'lib/paginated_table/page.rb', line 7 def sort_direction @sort_direction end |
Class Method Details
.opposite_sort_direction(sort_direction) ⇒ Object
9 10 11 12 |
# File 'lib/paginated_table/page.rb', line 9 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
24 25 26 27 28 29 30 31 |
# File 'lib/paginated_table/page.rb', line 24 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
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/paginated_table/page.rb', line 33 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 |