1

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?

a.barbieri
  • 2,398
  • 3
  • 30
  • 58
  • I was trying the method explained [here](https://stackoverflow.com/a/45252778/1498118) but I still get the error. – a.barbieri Aug 29 '17 at 09:14
  • Hmm I'm not quite sure I understand the question. You can't `translate` an attribute on a class using the (default) `key_value` backend unless the class inherits from either `ActiveRecord::Base` or `Sequel::Model`, otherwise Mobility doesn't know how to setup methods correctly. If you want to do something more flexible, you could use the `Mobility::Attributes` module builder directly, which you can create without knowing the class it will be included in right away. If you can provide more details I can explain how to do this. – Chris Salzberg Sep 03 '17 at 03:16
  • Can you not have `class Foo < ApplicationRecord` in your plugin? That should fix the issue. – Chris Salzberg Sep 03 '17 at 03:19

1 Answers1

0

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

a.barbieri
  • 2,398
  • 3
  • 30
  • 58