Sha256: 483c80141a7bc07ad18c9b9537ff81d23fd8a9da3813777d6eaedfe9ea02802f

Contents?: true

Size: 1.15 KB

Versions: 21

Compression:

Stored size: 1.15 KB

Contents

module RestFtpDaemon
  class Paginate
    include CommonHelpers

    # Class options
    attr_writer :filter
    attr_accessor :all

    def initialize data
      # Defaults
      @pages = 0
      @total = 0
      @data = []
      @filter = ''
      @page = 1
      @pages = 1
      @all = false

      # Ensure data set is countable
      return unless data.is_a? Enumerable
      @data = data

      # Count elements
      @total = @data.count

      # Count pages
      @pages = (@total.to_f / DEFAULT_PAGE_SIZE).ceil
      @pages = 1 if @pages < 1
    end

    def page= raw_page
      @page = [1, raw_page.to_i, @pages].sort[1]
    end

    def browser
      return if @all

      out = []
      1.upto(@pages) do |p|
        out << link(p)
      end
      out.join()
    end

    def subset
      return @data if @all

      size = DEFAULT_PAGE_SIZE.to_i
      offset = (@page-1) * size
      @data[offset, size]
    end

  private

    def link p
      klass = (p == @page)? "primary" : "default"
      url = dashboard_url(@filter)
      "<a class='btn btn-%s' href='%s?page=%d'>%p</a>" % [
        klass,
        @filter,
        p,
        p
      ]
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
rest-ftp-daemon-1.1.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.1.0 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.15 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.14 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.13 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.12 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.11 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.10 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.9 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.8 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.7 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.6 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.5 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.4 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.3 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.2 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-1.0.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.502.2 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.502.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.502.0 lib/rest-ftp-daemon/paginate.rb