Elasticsearch:未定义的方法&#39;执行&#39;对于#<indexer ...> [test env] </indexer ...>

时间:2014-12-08 13:42:05

标签: ruby-on-rails rspec elasticsearch

我正在为我的应用程序编写测试(在Rspec中),并且整整一天试图弄清楚我在哪里与Capybara犯了错误。我注意到当调用一个动作(change_status)时,test.log中会出现以下错误:

undefined method `perform' for #<Indexer:0x000000095d9680>

并且更改未保存。我不知道从哪里开始寻找。我猜测它是Elasticsearch的一个问题。这是控制器的整个动作:

def change_status
  product = Product.find_by(id: params[:id])
  product.update_attribute(:status, params[:product][:status])
  if params[:product][:status] == "accepted"
    kind = Kind.where(name: "title").first
    product.author.change_points({points: 2, kind: kind.id})
  elsif params[:product][:status] == "rejected"
    kind = Kind.where(name: "title").first
    product.author.change_points({points: 1, kind: kind.id})
    product.author.change_points({points: -3, kind: kind.id})
  end

  user_id = product.author_id
  products = Product.where(author_id: user_id)
  if products.where(status: "accepted").count == 3
    user = User.find_by(id: user_id)
    user.update_attribute(:approved, true)
    kind = Kind.where(name: "title").first
    user.change_points({points: 1, kind: kind.id})
    redirect_to products_path
  else
    redirect_to action: "pending_products", id: user_id
  end
end

有什么建议吗?除此之外,日志也没用......

编辑:

class Indexer
  include Sidekiq::Worker

  sidekiq_options queue: 'elasticsearch', retry: false, backtrace: true

  Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
  Client = Elasticsearch::Client.new host: (ENV['ELASTICSEARCH_URL'] || 'http://localhost:9200'), logger: Logger

  def perform(operation, klass, record_id, options={})
    logger.debug [operation, "#{klass}##{record_id} #{options.inspect}"]

    case operation.to_s
      when /index|update/
        record = klass.constantize.find(record_id)
        record.__elasticsearch__.client = Client
        record.__elasticsearch__.__send__ "#{operation}_document"
      when /delete/
        Client.delete index: klass.constantize.index_name, type: klass.constantize.document_type, id: record_id
      else raise ArgumentError, "Unknown operation '#{operation}'"
    end
  end
end

0 个答案:

没有答案