How best to DRY will_paginate options
Asked Answered
T

2

9

I have 8 controllers using will_paginate to paginate their index pages. I'd like to override the defaults for "Previous" and "Next" on each without having to specify the same options 8 times. Is there a way to override the defaults only once (perhaps by subclassing will_paginate).

Tabling answered 16/8, 2011 at 5:2 Comment(0)
A
11

will_paginate uses I18n so you can just use that. Given you use English as the default locale the following line should be present in application.rb:

config.i18n.default_locale = :en

you can then change the text of the pagination links by adding the following to config/locales/will_paginate.en.yml:

en:
  will_paginate:
    previous_label: "← go back"
    next_label: "go forward →"

Alternatively you can add them to your default translations file: config/locales/en.yml but I have found that it quickly becomes to large to handle.

Note: If you use another locale, for instance es, for this to work you need to at least replace the en: keys within the YAML files to es: and to be concise about naming your files, use either config/locales/will_paginate.es.yml or config/locales/es.yml.

Allegorize answered 16/8, 2011 at 8:56 Comment(4)
So can we consider this question answered?Allegorize
@mislav This made sense, but when I tried it (both yml files), it did not work. I'm using version 3.0.pre2 - could that be why?Tabling
Just to be sure, you are using English as the default rails locale (I believe its the default default locale :-p )? I've updated my answer to include why it might not work if you did not.Allegorize
@jack: that version is too old. Please use 3.0.0 or laterEimile
H
2

I'm assuming you're doing something like this in your controllers:

will_paginate @collection, :previous_label => '< go back', :next_label => 'go forward >'

Your problem is that you want to use these labels everywhere in your application, so it's pointless to repeat them. In that case, you could define a helper like this:

def paginate(collection, options = {})
  defaults = {
    :previous_label => '< go back',
    :next_label     => 'go forward >',
  }
  options = defaults.merge(options)

  will_paginate collection, options
end

After that, calling paginate @collection in your views will use your defaults and still let you override them if necessary.

EDIT: suweller's response is definitely the better way to go in this case, especially considering it's approved by mislav, the plugin creator :). I'd completely forgotten about the translation file option. My solution could probably be useful in the general case, when the helper is not configurable in a similar way.

Hewlett answered 16/8, 2011 at 6:32 Comment(2)
Thanks Andrew, however, your approach would be nice if you want more than one 'default' pagination style on your site,Allegorize
This simple wrapper approach is useful if you want to change the options based on any other dynamic criteria. For example, if you want different pagination options for mobile devices.Climatology

© 2022 - 2024 — McMap. All rights reserved.