Repository navigation
Pagy method overrides #905
Answered
by
yudin-s
pontflorian
asked this question in
Q&A
|
Hi, I'm trying to override the paginate method in the Pagy::Offset module, but it's not working. I've followed the steps to define a custom module and prepend it, but the changes don't seem to take effect. What could be going wrong? Here is my require 'pagy/toolbox/paginators/offset' # path to the overridden method file
module PagyWithI18nKey
def paginate(collection, options) # Edit or define your method with the identical name
model_name = collection.instance_of?(Array) ? collection.first.model_name : collection.model_name
i18n_key = options.delete(:i18n_key) || "activerecord.models.#{model_name.i18n_key}"
super(collection, **options.merge(i18n_key:)) # Call super with the modified options
end
end
# prepend your module to the overridden module
Pagy::OffsetPaginator.prepend PagyWithI18nKeyThanks |
Answered by
yudin-s
May 21, 2026
Replies: 1 comment 1 reply
|
The likely issue is that In that shape, prepending into the module itself changes the instance method side, but the method Pagy calls is the module's singleton method. Try prepending into the singleton class instead: require "pagy/toolbox/paginators/offset"
module PagyWithI18nKey
def paginate(collection, options)
model_name =
if collection.respond_to?(:model_name)
collection.model_name
elsif collection.respond_to?(:klass)
collection.klass.model_name
elsif collection.is_a?(Array) && collection.first
collection.first.model_name
end
i18n_key = options.delete(:i18n_key)
i18n_key ||= "activerecord.models.#{model_name.i18n_key}" if model_name
super(collection, options.merge(i18n_key: i18n_key))
end
end
Pagy::OffsetPaginator.singleton_class.prepend(PagyWithI18nKey)Two other details:
You can verify the override is active with: Pagy::OffsetPaginator.method(:paginate).ownerIt should show your prepended module. If this solves it, please mark this comment as the answer so other people can find it faster. |
1 reply
Answer selected by
pontflorian
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The likely issue is that
Pagy::OffsetPaginatordefinespaginateas a module function.In that shape, prepending into the module itself changes the instance method side, but the method Pagy calls is the module's singleton method. Try prepending into the singleton class instead: