Skip to content

Elasticsearch::Model overrides class .search method #924

Closed
skroutz/elasticsearch-rails
#1
@Kukunin

Description

@Kukunin

Hello. I've updated from elasticsearch-model 6.1.0 version to the latest 7.0.0 and got a silly bug. I have the following structure:

class Article
  include Searchable
end

and

class Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
  end

  class_methods do
    def search(term)
      # my logic here
    end
  end
end

When I call Article.search I expect my method to be called, and it worked such before. With the elasticsearch-model 7.0.0 the delegated Article.__elasticsearch__.search is called directly.

I found the reason in the code: https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/lib/elasticsearch/model.rb#L115

    def self.included(base)
      base.class_eval do
        include Elasticsearch::Model::Proxy

        # Delegate common methods to the `__elasticsearch__` ClassMethodsProxy, unless they are defined already
        class << self
          METHODS.each do |method|
            delegate method, to: :__elasticsearch__ unless self.respond_to?(method)
          end
        end
      end

      # Add to the model to the registry if it's a class (and not in intermediate module)
      Registry.add(base) if base.is_a?(Class)
    end

The problem is that self from self.respond_to? is a singleton class of Article class and of course it doesn't respond to search. It's in its public instance methods.

It was changed 8 months ago here f9a87ff#diff-bada22348f9db4f589962054cc9adcb7R115, and I wonder how this was unnoticeable for such a long period. Am I missing something?

The previous implementation was right: delegate method, to: :__elasticsearch__ unless self.public_instance_methods.include?(method)

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