ruby-on-railsrubyinheritanceactiverecordmobility

How to extend in my app an AR model of my engine so Mobility doesn't complain?


In my plugin I'm inheriting few models from the engine and adding Mobility settings.

# my enginge
module MyEngine
  class Foo < ApplicationRecord
    def self.say_hi
      return "hi"
    end
  end
end
# my plugin
require 'my-engine'
module MyEngine
  class Foo
    include Mobility
    translates :name, type: :string
  end
end

Now if I look for the model in Rails console:

MyEngine::Foo.say_hi
ArgumentError: KeyValue backend can only be used by ActiveRecord or Sequel models

If I comment out Mobility settings everything works:

MyEngine::Foo.say_hi
=> "hi"

How to extend in my app an AR model MyEngine::Foo so Mobility doesn't complain?


Solution

  • This answer does help with extending an engine model from a plugin, but doesn't help with mobility.

    To use modify MyEngine::Foo to use mobilty you need to create an initializer config/initializers/mobility_models_extension.rb (make sure the file name is alphabetically after mobility.rb file).

    :: MyEngine::Foo(:include, Mobility)
    :: MyEngine::Foo.send(:translates, :name, type: :string)
    

    See send documentation and initializers documentation