Sha256: b0086d65cc2960324f4fafc9a43b19a191f1b948e091948f996b589ce6cd3210

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module PaginatedTable
  class Page
    SORT_DIRECTIONS = %w(asc desc)

    attr_reader :number, :rows, :sort_column, :sort_direction

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

    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

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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paginated_table-0.0.9 lib/paginated_table/page.rb
paginated_table-0.0.8 lib/paginated_table/page.rb
paginated_table-0.0.7 lib/paginated_table/page.rb