0

I'm generating a rather complex mapping using Elasticsearch-Model. I extracted all the search related methods into an ActiveSupport::Concern. The following code was written while using the Tire gem, but I'm upgrading the gem to Elasticsearch-Rails and Elasticsearch-Model, since Tire doesn't support Elasticsearch 1.x . The current implementation looks like this:

require 'active_support/concern'

module SearchBooks
extend ActiveSupport::Concern

included do
   include Elasticsearch::Model
   include Elasticsearch::Model::Callbacks

   mapping do
      locales = %w['de', 'en', 'fr']
         locales.each do |locale|
            class_eval <<-RUBY
               indexes: ... 
            RUBY
      end
   end
 end

But I'm getting this error:

/Users/xxx/.rvm/gems/ruby-2.0.0-p647/gems/activesupport-3.2.22/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval': undefined method `indexes' for #<Class:0x007fea661037ec> (NoMethodError)

I tried a lot of stuff, e.g. usind def included(base) and then base.eval, but I can't get class_eval under the right scope.

Any ideas?

Fei
  • 1,187
  • 1
  • 16
  • 35

1 Answers1

0

The resolution turned out to be very simple:

Just use instance_eval instead of class_eval, since the mapping block scopes everything inside to an instance of Elasticsearch::Model::Indexing::Mappings.

Fei
  • 1,187
  • 1
  • 16
  • 35