Skip to content

Dependency on the gems loading order #1094

@r7kamura

Description

@r7kamura

elasticsearch-model only loads the corresponding extensions if the kaminari or will_paginate has already been loaded when it is loaded.

case
when defined?(::Kaminari)
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
when defined?(::WillPaginate)
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::WillPaginate
end

In a typical Rails application, gems are usually loaded with Bundler.require in config/application.rb, which means that unless those gems are listed in the Gemfile before elasticsearch-model, it won’t work as intended.

The fact that behavior changes depending on the order of gems makes it very confusing and prone to misunderstandings. I think it would be much better to improve it by using mechanisms like ActiveSupport.on_load hooks so that it no longer depends on the load order.

For example, the Bundler/OrderedGems rule from rubocop-rails, commonly used in Rails development, enforces sorting gems alphabetically in the Gemfile. Following this, elasticsearch-model would appear before kaminari or will_paginate, making this issue a frequent occurrence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions