adapt impressionist gem to mongoid

2019-08-04 16:44发布

问题:

I'm trying adapt this gem, https://github.com/charlotte-ruby/impressionist to mongoid odm.

I have been seeing the commit for mongo_mapper in https://github.com/tute/impressionist/commit/1c3f809da8ebd3ced8cb9bda8caee89012091a10.

This is impression.rb model for mongoid:

class Impression
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  field :impressionable_type, :type => String
  field :impressionable_id, :type => String
  field :user_id, :type => String
  field :controller_name, :type => String
  field :action_name, :type => String
  field :view_name, :type => String
  field :request_hash, :type => String
  field :ip_address, :type => String
  field :session_hash, :type => String
  field :message, :type => String
  field :referrer, :type => String
end

After generate the file for mongoid fine:

rails g impressionist --orm mongo_id
      invoke  mongo_id
      create  config/initializers/impression.rb

I try run the server and I get this error:

uninitialized constant Impressionist::Engine::MongoId (NameError)
    from /home/ubuntu/.rvm/gems/ruby-1.9.3-p125@rails32/gems/impressionist-1.1.1/lib/impressionist/engine.rb:11:in `block in <class:Engine>'

In engine.rb file I have:

require "impressionist"
require "rails"

module Impressionist
  class Engine < Rails::Engine
    initializer 'impressionist.model' do |app|
      require "#{root}/app/models/impressionist/impressionable.rb"
      if Impressionist.orm == :mongo_id
        require "impressionist/models/mongo_id/impression.rb"
        require "impressionist/models/mongo_id/impressionist/impressionable.rb"
        MongoId::Document.plugin Impressionist::Impressionable
      end
    end

    initializer 'impressionist.controller' do
      ActiveSupport.on_load(:action_controller) do
        include ImpressionistController::InstanceMethods
        extend ImpressionistController::ClassMethods
      end
    end
  end
end

The line 11 is MongoId::Document.plugin Impressionist::Impressionable

can anyone help out? Thank you

回答1:

In line 11

MongoId::Document.plugin Impressionist::Impressionable

Should be

Mongoid::Document.plugin Impressionist::Impressionable

Note the capitalization of Mongoid.