Sha256: 6f555cb4863d868091a7cb7e1a637edc0870a30f25e408edb8efeaa2a1aa3ba3

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Paginary
  module Helpers
    class PageBuilder
      attr_reader :template, :relation
      alias_method :items, :relation
      
      delegate :content_tag, :link_to, :params, :translate, :to => :template
      
      def initialize(template, relation, options = {})
        @template = template
        @param_name = options.delete(:param) || :page
        @relation = relation.paginated? ? relation : relation.paginate(params[@param_name], options)
      end
      
      def page_url(page)
        template.url_for params.merge(@param_name => page)
      end

      def previous_url
        page_url @relation.current_page - 1
      end
      
      def next_url
        page_url @relation.current_page + 1
      end
      
      def links
        return unless @relation.paginated?
        content_tag :div, previous_link + page_links + next_link, :class => "pagination"
      end

      def page_links
        (1..@relation.page_count).collect do |page|
          link_to content_tag(:span, page), page_url(page), :class => "page#{@relation.current_page == page ? " selected" : ""}"
        end.inject(:+)
      end

      def previous_link
        text = content_tag(:span, translate("previous", :default => "< Previous"))
        unless @relation.first_page?
          link_to text, previous_url, :class => "previous"
        else
          content_tag :span, text, :class => "previous disabled"
        end
      end

      def next_link
        text = content_tag(:span, translate("previous", :default => "Next >"))
        unless @relation.last_page?
          link_to text, next_url, :class => "next"
        else
          content_tag :span, text, :class => "next disabled"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paginary-0.0.1.pre1 lib/paginary/helpers/page_builder.rb