will_paginate 3.0.pre4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of will_paginate might be problematic. Click here for more details.

Files changed (51) hide show
  1. data/README.md +61 -0
  2. data/Rakefile +19 -36
  3. data/lib/will_paginate.rb +16 -14
  4. data/lib/will_paginate/{finders/active_record.rb → active_record.rb} +66 -35
  5. data/lib/will_paginate/array.rb +5 -5
  6. data/lib/will_paginate/collection.rb +12 -36
  7. data/lib/will_paginate/core_ext.rb +0 -28
  8. data/lib/will_paginate/data_mapper.rb +91 -0
  9. data/lib/will_paginate/i18n.rb +22 -0
  10. data/lib/will_paginate/locale/en.yml +33 -0
  11. data/lib/will_paginate/page_number.rb +57 -0
  12. data/lib/will_paginate/per_page.rb +27 -0
  13. data/lib/will_paginate/railtie.rb +13 -4
  14. data/lib/will_paginate/sequel.rb +36 -0
  15. data/lib/will_paginate/version.rb +1 -1
  16. data/lib/will_paginate/view_helpers.rb +136 -22
  17. data/lib/will_paginate/view_helpers/action_view.rb +129 -117
  18. data/lib/will_paginate/view_helpers/link_renderer.rb +10 -11
  19. data/lib/will_paginate/view_helpers/link_renderer_base.rb +2 -8
  20. data/lib/will_paginate/view_helpers/merb.rb +20 -7
  21. data/lib/will_paginate/view_helpers/sinatra.rb +41 -0
  22. data/spec/ci.rb +29 -0
  23. data/spec/collection_spec.rb +7 -23
  24. data/spec/console +9 -1
  25. data/spec/console_fixtures.rb +1 -3
  26. data/spec/database.yml +10 -10
  27. data/spec/finders/active_record_spec.rb +82 -28
  28. data/spec/finders/activerecord_test_connector.rb +9 -1
  29. data/spec/finders/data_mapper_spec.rb +59 -48
  30. data/spec/finders/data_mapper_test_connector.rb +8 -1
  31. data/spec/finders/sequel_spec.rb +9 -3
  32. data/spec/fixtures/project.rb +2 -0
  33. data/spec/page_number_spec.rb +65 -0
  34. data/spec/per_page_spec.rb +41 -0
  35. data/spec/spec_helper.rb +5 -29
  36. data/spec/view_helpers/action_view_spec.rb +48 -32
  37. data/spec/view_helpers/base_spec.rb +59 -7
  38. data/spec/view_helpers/link_renderer_base_spec.rb +7 -12
  39. data/spec/view_helpers/view_example_group.rb +1 -0
  40. metadata +22 -23
  41. data/CHANGELOG.rdoc +0 -105
  42. data/README.rdoc +0 -111
  43. data/lib/will_paginate/deprecation.rb +0 -50
  44. data/lib/will_paginate/finders.rb +0 -9
  45. data/lib/will_paginate/finders/active_resource.rb +0 -51
  46. data/lib/will_paginate/finders/base.rb +0 -112
  47. data/lib/will_paginate/finders/data_mapper.rb +0 -30
  48. data/lib/will_paginate/finders/sequel.rb +0 -23
  49. data/lib/will_paginate/view_helpers/base.rb +0 -126
  50. data/spec/finders/active_resource_spec.rb +0 -52
  51. data/spec/finders_spec.rb +0 -76
@@ -1,136 +1,148 @@
1
- require 'will_paginate/view_helpers/base'
1
+ require 'will_paginate/view_helpers'
2
2
  require 'will_paginate/view_helpers/link_renderer'
3
3
 
4
4
  module WillPaginate
5
- module ViewHelpers
6
- # = ActionView helpers
7
- #
8
- # This module serves for availability in ActionView templates. It also adds a new
9
- # view helper: +paginated_section+.
5
+ # = ActionView helpers
6
+ #
7
+ # This module serves for availability in ActionView templates. It also adds a new
8
+ # view helper: +paginated_section+.
9
+ #
10
+ # == Using the helper without arguments
11
+ # If the helper is called without passing in the collection object, it will
12
+ # try to read from the instance variable inferred by the controller name.
13
+ # For example, calling +will_paginate+ while the current controller is
14
+ # PostsController will result in trying to read from the <tt>@posts</tt>
15
+ # variable. Example:
16
+ #
17
+ # <%= will_paginate :id => true %>
18
+ #
19
+ # ... will result in <tt>@post</tt> collection getting paginated:
20
+ #
21
+ # <div class="pagination" id="posts_pagination"> ... </div>
22
+ #
23
+ module ActionView
24
+ include ViewHelpers
25
+
26
+ def will_paginate(collection = nil, options = {}) #:nodoc:
27
+ options, collection = collection, nil if collection.is_a? Hash
28
+ collection ||= infer_collection_from_controller
29
+
30
+ options = options.symbolize_keys
31
+ options[:renderer] ||= LinkRenderer
32
+
33
+ super(collection, options).try(:html_safe)
34
+ end
35
+
36
+ def page_entries_info(collection = nil, options = {}) #:nodoc:
37
+ options, collection = collection, nil if collection.is_a? Hash
38
+ collection ||= infer_collection_from_controller
39
+
40
+ super(collection, options.symbolize_keys)
41
+ end
42
+
43
+ # Wrapper for rendering pagination links at both top and bottom of a block
44
+ # of content.
10
45
  #
11
- # == Using the helper without arguments
12
- # If the helper is called without passing in the collection object, it will
13
- # try to read from the instance variable inferred by the controller name.
14
- # For example, calling +will_paginate+ while the current controller is
15
- # PostsController will result in trying to read from the <tt>@posts</tt>
16
- # variable. Example:
46
+ # <% paginated_section @posts do %>
47
+ # <ol id="posts">
48
+ # <% for post in @posts %>
49
+ # <li> ... </li>
50
+ # <% end %>
51
+ # </ol>
52
+ # <% end %>
17
53
  #
18
- # <%= will_paginate :id => true %>
54
+ # will result in:
19
55
  #
20
- # ... will result in <tt>@post</tt> collection getting paginated:
56
+ # <div class="pagination"> ... </div>
57
+ # <ol id="posts">
58
+ # ...
59
+ # </ol>
60
+ # <div class="pagination"> ... </div>
21
61
  #
22
- # <div class="pagination" id="posts_pagination"> ... </div>
23
- #
24
- module ActionView
25
- include WillPaginate::ViewHelpers::Base
26
-
27
- def will_paginate(collection = nil, options = {}) #:nodoc:
28
- options, collection = collection, nil if collection.is_a? Hash
29
- collection ||= infer_collection_from_controller
30
-
31
- super(collection, options.symbolize_keys).try(:html_safe)
62
+ # Arguments are passed to a <tt>will_paginate</tt> call, so the same options
63
+ # apply. Don't use the <tt>:id</tt> option; otherwise you'll finish with two
64
+ # blocks of pagination links sharing the same ID (which is invalid HTML).
65
+ def paginated_section(*args, &block)
66
+ pagination = will_paginate(*args)
67
+ if pagination
68
+ pagination + capture(&block) + pagination
69
+ else
70
+ capture(&block)
71
+ end
72
+ end
73
+
74
+ def will_paginate_translate(keys, options = {})
75
+ if Array === keys
76
+ defaults = keys.dup
77
+ key = defaults.shift
78
+ else
79
+ defaults = nil
80
+ key = keys
32
81
  end
33
-
34
- def page_entries_info(collection = nil, options = {}) #:nodoc:
35
- options, collection = collection, nil if collection.is_a? Hash
36
- collection ||= infer_collection_from_controller
37
-
38
- super(collection, options.symbolize_keys).try(:html_safe)
82
+ translate(key, options.merge(:default => defaults, :scope => :will_paginate))
83
+ end
84
+
85
+ protected
86
+
87
+ def infer_collection_from_controller
88
+ collection_name = "@#{controller.controller_name}"
89
+ collection = instance_variable_get(collection_name)
90
+ raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " +
91
+ "forget to pass the collection object for will_paginate?" if collection.nil?
92
+ collection
93
+ end
94
+
95
+ class LinkRenderer < ViewHelpers::LinkRenderer
96
+ protected
97
+
98
+ def default_url_params
99
+ {}
39
100
  end
40
-
41
- # Wrapper for rendering pagination links at both top and bottom of a block
42
- # of content.
43
- #
44
- # <% paginated_section @posts do %>
45
- # <ol id="posts">
46
- # <% for post in @posts %>
47
- # <li> ... </li>
48
- # <% end %>
49
- # </ol>
50
- # <% end %>
51
- #
52
- # will result in:
53
- #
54
- # <div class="pagination"> ... </div>
55
- # <ol id="posts">
56
- # ...
57
- # </ol>
58
- # <div class="pagination"> ... </div>
59
- #
60
- # Arguments are passed to a <tt>will_paginate</tt> call, so the same options
61
- # apply. Don't use the <tt>:id</tt> option; otherwise you'll finish with two
62
- # blocks of pagination links sharing the same ID (which is invalid HTML).
63
- def paginated_section(*args, &block)
64
- pagination = will_paginate(*args)
65
- if pagination
66
- pagination + capture(&block) + pagination
101
+
102
+ def url(page)
103
+ @base_url_params ||= begin
104
+ url_params = base_url_params
105
+ merge_optional_params(url_params)
106
+ url_params
107
+ end
108
+
109
+ url_params = @base_url_params.dup
110
+ add_current_page_param(url_params, page)
111
+
112
+ @template.url_for(url_params)
113
+ end
114
+
115
+ def base_url_params
116
+ url_params = default_url_params
117
+ # page links should preserve GET parameters
118
+ symbolized_update(url_params, @template.params) if get_request?
119
+ url_params
120
+ end
121
+
122
+ def merge_optional_params(url_params)
123
+ symbolized_update(url_params, @options[:params]) if @options[:params]
124
+ end
125
+
126
+ def add_current_page_param(url_params, page)
127
+ unless param_name.index(/[^\w-]/)
128
+ url_params[param_name.to_sym] = page
67
129
  else
68
- capture(&block)
130
+ page_param = parse_query_parameters("#{param_name}=#{page}")
131
+ symbolized_update(url_params, page_param)
69
132
  end
70
133
  end
71
-
72
- protected
73
134
 
74
- def infer_collection_from_controller
75
- collection_name = "@#{controller.controller_name}"
76
- collection = instance_variable_get(collection_name)
77
- raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " +
78
- "forget to pass the collection object for will_paginate?" if collection.nil?
79
- collection
135
+ def get_request?
136
+ @template.request.get?
80
137
  end
81
- end
82
- end
83
- end
84
138
 
85
- # :stopdoc:
139
+ private
86
140
 
87
- WillPaginate::ViewHelpers::LinkRenderer.class_eval do
88
- protected
89
-
90
- def default_url_params
91
- {}
92
- end
93
-
94
- def url(page)
95
- @base_url_params ||= begin
96
- url_params = base_url_params
97
- merge_optional_params(url_params)
98
- url_params
99
- end
100
-
101
- url_params = @base_url_params.dup
102
- add_current_page_param(url_params, page)
103
-
104
- @template.url_for(url_params)
105
- end
106
-
107
- def base_url_params
108
- url_params = default_url_params
109
- # page links should preserve GET parameters
110
- symbolized_update(url_params, @template.params) if get_request?
111
- url_params
112
- end
113
-
114
- def merge_optional_params(url_params)
115
- symbolized_update(url_params, @options[:params]) if @options[:params]
116
- end
117
-
118
- def add_current_page_param(url_params, page)
119
- unless param_name.index(/[^\w-]/)
120
- url_params[param_name.to_sym] = page
121
- else
122
- page_param = parse_query_parameters("#{param_name}=#{page}")
123
- symbolized_update(url_params, page_param)
141
+ def parse_query_parameters(params)
142
+ Rack::Utils.parse_nested_query(params)
143
+ end
124
144
  end
125
- end
126
-
127
- def get_request?
128
- @template.request.get?
129
- end
130
-
131
- private
132
-
133
- def parse_query_parameters(params)
134
- Rack::Utils.parse_nested_query(params)
145
+
146
+ ::ActionView::Base.send :include, self
135
147
  end
136
148
  end
@@ -1,5 +1,6 @@
1
1
  require 'cgi'
2
2
  require 'will_paginate/core_ext'
3
+ require 'will_paginate/view_helpers'
3
4
  require 'will_paginate/view_helpers/link_renderer_base'
4
5
 
5
6
  module WillPaginate
@@ -26,7 +27,7 @@ module WillPaginate
26
27
  item.is_a?(Fixnum) ?
27
28
  page_number(item) :
28
29
  send(item)
29
- end.join(@options[:separator])
30
+ end.join(@options[:link_separator])
30
31
 
31
32
  @options[:container] ? html_container(html) : html
32
33
  end
@@ -34,14 +35,7 @@ module WillPaginate
34
35
  # Returns the subset of +options+ this instance was initialized with that
35
36
  # represent HTML attributes for the container element of pagination links.
36
37
  def container_attributes
37
- @container_attributes ||= begin
38
- attributes = @options.except *(WillPaginate::ViewHelpers.pagination_options.keys - [:class])
39
- # pagination of Post models will have the ID of "posts_pagination"
40
- if @options[:container] and @options[:id] === true
41
- attributes[:id] = @collection.first.class.name.underscore.pluralize + '_pagination'
42
- end
43
- attributes
44
- end
38
+ @container_attributes ||= @options.except(*(ViewHelpers.pagination_options.keys - [:class]))
45
39
  end
46
40
 
47
41
  protected
@@ -50,12 +44,13 @@ module WillPaginate
50
44
  unless page == current_page
51
45
  link(page, page, :rel => rel_value(page))
52
46
  else
53
- tag(:em, page)
47
+ tag(:em, page, :class => 'current')
54
48
  end
55
49
  end
56
50
 
57
51
  def gap
58
- '<span class="gap">&hellip;</span>'
52
+ text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
53
+ %(<span class="gap">#{text}</span>)
59
54
  end
60
55
 
61
56
  def previous_page
@@ -88,6 +83,10 @@ module WillPaginate
88
83
 
89
84
  private
90
85
 
86
+ def param_name
87
+ @options[:param_name].to_s
88
+ end
89
+
91
90
  def link(text, target, attributes = {})
92
91
  if target.is_a? Fixnum
93
92
  attributes[:rel] = rel_value(target)
@@ -1,5 +1,3 @@
1
- require 'will_paginate/view_helpers'
2
-
3
1
  module WillPaginate
4
2
  module ViewHelpers
5
3
  # This class does the heavy lifting of actually building the pagination
@@ -14,7 +12,7 @@ module WillPaginate
14
12
  @options = options
15
13
 
16
14
  # reset values in case we're re-using this instance
17
- @total_pages = @param_name = nil
15
+ @total_pages = nil
18
16
  end
19
17
 
20
18
  def pagination
@@ -72,11 +70,7 @@ module WillPaginate
72
70
  end
73
71
 
74
72
  def total_pages
75
- @collection.total_pages
76
- end
77
-
78
- def param_name
79
- @param_name ||= @options[:param_name].to_s
73
+ @total_pages ||= @collection.total_pages
80
74
  end
81
75
  end
82
76
  end
@@ -1,13 +1,26 @@
1
- require 'will_paginate/view_helpers/base'
1
+ require 'will_paginate/core_ext'
2
+ require 'will_paginate/view_helpers'
2
3
  require 'will_paginate/view_helpers/link_renderer'
3
4
 
4
- WillPaginate::ViewHelpers::LinkRenderer.class_eval do
5
- protected
5
+ module WillPaginate
6
+ module Merb
7
+ include ViewHelpers
6
8
 
7
- def url(page)
8
- params = @template.request.params.except(:action, :controller).merge(param_name => page)
9
- @template.url(:this, params)
9
+ def will_paginate(collection, options = {}) #:nodoc:
10
+ options = options.merge(:renderer => LinkRenderer) unless options[:renderer]
11
+ super(collection, options)
12
+ end
13
+
14
+ class LinkRenderer < ViewHelpers::LinkRenderer
15
+ protected
16
+
17
+ def url(page)
18
+ params = @template.request.params.except(:action, :controller).merge(param_name => page)
19
+ @template.url(:this, params)
20
+ end
21
+ end
22
+
23
+ ::Merb::AbstractController.send(:include, self)
10
24
  end
11
25
  end
12
26
 
13
- Merb::AbstractController.send(:include, WillPaginate::ViewHelpers::Base)
@@ -0,0 +1,41 @@
1
+ require 'sinatra/base'
2
+ require 'will_paginate/view_helpers'
3
+ require 'will_paginate/view_helpers/link_renderer'
4
+
5
+ module WillPaginate
6
+ module Sinatra
7
+ module Helpers
8
+ include ViewHelpers
9
+
10
+ def will_paginate(collection, options = {}) #:nodoc:
11
+ options = options.merge(:renderer => LinkRenderer) unless options[:renderer]
12
+ super(collection, options)
13
+ end
14
+ end
15
+
16
+ class LinkRenderer < ViewHelpers::LinkRenderer
17
+ protected
18
+
19
+ def url(page)
20
+ str = File.join(request.script_name.to_s, request.path_info)
21
+ params = request.GET.merge(param_name.to_s => page.to_s)
22
+ params.update @options[:params] if @options[:params]
23
+ str << '?' << build_query(params)
24
+ end
25
+
26
+ def request
27
+ @template.request
28
+ end
29
+
30
+ def build_query(params)
31
+ Rack::Utils.build_nested_query params
32
+ end
33
+ end
34
+
35
+ def self.registered(app)
36
+ app.helpers Helpers
37
+ end
38
+
39
+ ::Sinatra.register self
40
+ end
41
+ end
data/spec/ci.rb ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ databases = %w[ sqlite3 mysql mysql2 postgres ]
3
+ databases.delete 'mysql2' if ENV['BUNDLE_GEMFILE'].to_s.include? 'rails3.0'
4
+
5
+ def announce(name, msg)
6
+ puts "\n\e[1;33m[#{name}] #{msg}\e[m\n"
7
+ end
8
+
9
+ def system(*args)
10
+ puts "$ #{args.join(' ')}"
11
+ super
12
+ end
13
+
14
+ if ENV['TRAVIS']
15
+ system "mysql -e 'create database will_paginate;' >/dev/null"
16
+ abort "failed to create mysql database" unless $?.success?
17
+ system "psql -c 'create database will_paginate;' -U postgres >/dev/null"
18
+ abort "failed to create postgres database" unless $?.success?
19
+ end
20
+
21
+ failed = false
22
+
23
+ for db in databases
24
+ announce "DB", db
25
+ ENV['DB'] = db
26
+ failed = true unless system %(rake)
27
+ end
28
+
29
+ exit 1 if failed