lib/nitro/ui/pager.rb in nitro-0.8.0 vs lib/nitro/ui/pager.rb in nitro-0.9.3

- old
+ new

@@ -1,31 +1,27 @@ -# code: # * George Moschovitis <gm@navel.gr> -# -# (c) 2004 Navel, all rights reserved. -# $Id: pager.rb 185 2004-12-10 13:29:09Z gmosx $ +# (c) 2004-2005 Navel, all rights reserved. +# $Id: pager.rb 223 2005-01-26 17:07:40Z gmosx $ require 'nitro/uri' module N; module UI -# = Pager -# # Displays a collection of entitities in multiple pages. # -# === Design: +# === Design # # The new version is carefully designed for scaleability. It stores # only the items for one page. The name parameter is needed, multiple # pagers can coexist in a single page. Unlike the v1/v2 pagers this # pager leverages the SQL LIMIT option to optimize database interaction. # # The pager does not extend Array (it includes an Array instead) to # avoid a concat() in the initialization step. # # -# === Example: +# === Example # # @pager = N::UI::Pager.new('entries', @request, 5) # @entries = N::BlogEntry.all("ORDER BY oid #{@pager.sql_limit}") # @pager.set(N::BlogEntry.count) # @@ -39,25 +35,28 @@ # <td width="64"><x:pager-prev>Previous</x:pager-prev></td> # <td width="100%"></td> # <td width="64"><x:pager-next>Next</x:pager-next></td> # </tr> # </table> -# -# === Investigate: -# +#-- # INVESTIGATE: # mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name # -> WHERE id > 100 LIMIT 10; # mysql> SELECT FOUND_ROWS(); -# +#++ + class Pager < Array + attr_accessor :name, :idx, :page, :page_count - # total count of items + + # Total count of items attr_accessor :total_count - # page items + + # Page items attr_accessor :page_items - # read needed variables from the request. + + # Read needed variables from the request. attr_accessor :request def initialize(name, request, items_per_page = 10, items = nil) raise "items_per_page should be > 0" unless items_per_page > 0 @@ -93,18 +92,18 @@ def next_page return [@page + 1, @page_count].min() end # Iterator - # + def each(&block) @page_items.each(&block) end # Iterator # Returns 1-based index. - # + def each_with_index idx = @start_idx for item in @page_items yield(idx + 1, item) idx += 1 @@ -118,20 +117,20 @@ def size return @items.size() end # Returns the range of the current page. - # + def page_range s = @idx e = [@idx + @items_per_page - 1, all_total_count].min return [s, e] end # Override if needed. - # + def nav_range # effective range = 10 pages. s = [@page - 5, 1].max() e = [@page + 9, @page_count].min() @@ -142,11 +141,11 @@ end # Override this method in your application # if needed. # TODO: better markup. - # + def navigation nav = "" unless @page == first_page() nav << %{ @@ -181,33 +180,31 @@ return nav end # Create an appropriate SQL limit clause. # Returns postgres/mysql compatible limit. - # + def sql_limit if @start_idx > 0 return "LIMIT #{@items_per_page} OFFSET #{@start_idx}" else # gmosx: perhaps this is optimized ? naaaaaah... return "LIMIT #{@items_per_page}" end end # Returns the current offset. The offset is zero-based. - # + def offset (@page-1) * @items_per_page end - # ------------------------------------------------------------------ - # Generate the target URI. # def target_uri(page) params = {"__pg#{@name}" => page} - return N::UriUtils.update_query_string(@request.request_uri.to_s, params) + return N::UriUtils.update_query_string(@request.uri.to_s, params) end end -end; end # module +end; end