Sha256: f2ddfe149ebe78accf4cb9fc7fb6cb3814751d611fcedcf523c19f4845c50904

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

module PaginatingFind
  module Helpers
    DEFAULT_OPTIONS = { :name => :page, :window_size => 2, 
      :always_show_anchors => true, :link_to_current_page => false, 
      :params => {} }

    def paginating_links(paginator, options = {}, html_options = {})
      name = options[:name] || DEFAULT_OPTIONS[:name]
      params = (options[:params] || DEFAULT_OPTIONS[:params]).clone

      paginating_links_each(paginator, options) do |n|
        params[name] = n
        link_to(n, params, html_options)
      end
    end

    def paginating_links_each(paginator, options = {})
      options = DEFAULT_OPTIONS.merge(options)

      window = ((paginator.page - options[:window_size] + 1)..(paginator.page + options[:window_size] - 1)).select {|w| w >= paginator.first_page && w <= paginator.last_page }

      html = ''

      if options[:always_show_anchors] && !window.include?(paginator.first_page)
        html << yield(paginator.first_page)
        html << ' ... ' unless window.first - 1 == paginator.first_page
        html << ' '
      end

      window.each do |p|
        if paginator.page == p && !options[:link_to_current_page]
          html << p.to_s
        else
          html << yield(p)
        end
        html << ' '
      end

      if options[:always_show_anchors] && !window.include?(paginator.last_page)
        html << ' ... ' unless window.last + 1 == paginator.last_page
        html << yield(paginator.last_page)
      end

      html
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
railscart-0.0.1 starter-app/vendor/plugins/paginating_find/lib/paging_helper.rb
railscart-0.0.2 starter_app/vendor/plugins/paginating_find/lib/paging_helper.rb
railscart-0.0.4 starter_app/vendor/plugins/paginating_find/lib/paging_helper.rb
railscart-0.0.3 starter_app/vendor/plugins/paginating_find/lib/paging_helper.rb
spree-0.0.5 starter-app/vendor/plugins/paginating_find/lib/paging_helper.rb
spree-0.0.6 starter-app/vendor/plugins/paginating_find/lib/paging_helper.rb
spree-0.0.7 starter-app/vendor/plugins/paginating_find/lib/paging_helper.rb
spree-0.0.9 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.0.8 starter-app/vendor/plugins/paginating_find/lib/paging_helper.rb
spree-0.2.0 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.4.0 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.4.1 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.5.0 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.5.1 lib/plugins/paginating_find/lib/paging_helper.rb
spree-0.6.0 lib/plugins/paginating_find/lib/paging_helper.rb