Skip to content

Loading…

Respect pagination_options[:renderer] #176

Closed
wants to merge 4 commits into from

4 participants

@ghost

Hey,

This is related to the pull request I made earlier, it respects pagination_options[:renderer] for all frameworks(Sinatra, Merb, Rails). I added tests for the ActionView changes, and I couldn't find any tests relating to Sinatra/Merb. Since the changes are identical, i think it should be okay to rely on the ActionView tests, but let me know if you'd like me to add tests for Sinatra/Merb.

Cheers,
Rob

@ghost

I noticed a problem with these specs, gonna update them now.

robgleeson Rewrite specs. 5eb19d3
@ghost

I rewrote the specs. I'm really unfamiliar with Mocha, but I think I got it setup properly now.
Appreciate if you can have a look :)

@phene

+1 Looks good.

@mislav
Owner

Hey @robgleeson,

There are few things wrong with your patch. First, you're not guarding yourself from editing options objects passed in from outside to the will_paginate helpers. You're writing the :renderer key to them in Merb in Sinatra. Second, the tests are too brittle. I know you have gone through much trouble to mock this up with Mocha but there easier ways of doing this without mocking, and the tests could also check if the correct renderer was really used for the HTML output (which your tests don't do).

But, since the last time I've talked I have thought a lot about this and related issues and I realized I can't encourage people to set a global link renderer. This is because of Rack apps mounted inside of each another, like Sinatra inside Rails. If a Rails user set a global link renderer that works with Rails url helpers, that link renderer would implicitly be used in Sinatra and break. The proper way of setting a default link renderer for your application would be wrapping the will_paginate helper with your own and setting the renderer there.

So, I'm going to change the code to deprecate the global option and close this. Sorry about wasting your time, but even I didn't predict these problems.

@mislav mislav closed this
@mislav mislav added a commit that referenced this pull request
@mislav remove :renderer from global pagination_options to discourage using it
Because lately it's popular to mount different web frameworks in the
same Ruby process (e.g. Sinatra apps beside Rails), a global link
renderer option is useless since it would affect all frameworks but it's
hardly possible that a single renderer will work in all of these
frameworks.

See resolution for #176
3c12251
@ghost

No problem!

Yeah, I thought a global renderer might cause conflicts within Rails with mounted Rack applications, too.
Thanks for the response.

  • Rob
@korny

The proper way of setting a default link renderer for your application would be wrapping the will_paginate helper with your own and setting the renderer there.

I agree! This should be mentioned in the warning.

@banesto

Maybe someone could provide an example how to override will_paginate function in helper?

@banesto

@mislav Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 6, 2011
  1. Respect pagination_options[:renderer].

    robgleeson committed
  2. Fix specs.

    robgleeson committed
  3. Rewrite specs.

    robgleeson committed
View
2 lib/will_paginate/view_helpers/action_view.rb
@@ -28,7 +28,7 @@ def will_paginate(collection = nil, options = {}) #:nodoc:
collection ||= infer_collection_from_controller
options = options.symbolize_keys
- options[:renderer] ||= LinkRenderer
+ options[:renderer] ||= ViewHelpers.pagination_options[:renderer] || LinkRenderer
super(collection, options).try(:html_safe)
end
View
2 lib/will_paginate/view_helpers/merb.rb
@@ -7,7 +7,7 @@ module Merb
include ViewHelpers
def will_paginate(collection, options = {}) #:nodoc:
- options = options.merge(:renderer => LinkRenderer) unless options[:renderer]
+ options[:renderer] ||= WillPaginate::ViewHelpers.pagination_options[:renderer] || LinkRenderer
super(collection, options)
end
View
2 lib/will_paginate/view_helpers/sinatra.rb
@@ -8,7 +8,7 @@ module Helpers
include ViewHelpers
def will_paginate(collection, options = {}) #:nodoc:
- options = options.merge(:renderer => LinkRenderer) unless options[:renderer]
+ options[:renderer] ||= WillPaginate::ViewHelpers.pagination_options[:renderer] || LinkRenderer
super(collection, options)
end
end
View
37 spec/view_helpers/action_view_spec.rb
@@ -41,6 +41,43 @@ def render(locals)
@view.render(:inline => @template, :locals => locals)
end
+
+ describe "Renderer" do
+ before(:all) do
+ @collection = WillPaginate::Collection.new(1,2,4)
+ @options = WillPaginate::ViewHelpers.pagination_options
+ end
+
+ after(:all) do
+ @options.unstub(:merge)
+ end
+
+ it "should default to LinkRenderer when pagination_options[:renderer] is nil" do
+ expectation = { :renderer => WillPaginate::ActionView::LinkRenderer }
+ ret = @options.merge(expectation)
+
+ @options.stubs(:merge).with(expectation).returns(ret)
+ render(:collection => @collection, :options => {})
+ end
+
+ it "should default to pagination_options[:renderer] when it is not nil" do
+ @options.merge!({ :renderer => Class.new(WillPaginate::ActionView::LinkRenderer)})
+
+ @options.stubs(:merge).with(:renderer => @options[:renderer]).returns(@options)
+ render(:collection => @collection, :options => {})
+
+ @options.delete :renderer
+ end
+
+ it "should use the :renderer supplied as an option if present." do
+ expectation = { :renderer => Class.new(WillPaginate::ActionView::LinkRenderer) }
+ ret = @options.merge(expectation)
+
+ @options.stubs(:merge).with(expectation).returns(ret)
+ render(:collection => @collection, :options => expectation)
+ end
+ end
+
## basic pagination ##
it "should render" do
Something went wrong with that request. Please try again.