I am trying to make a Rails Engine that can be plugged into my applications and manage friendships between users. To do that, all the logic dealing with friend requests, acceptance, etc, is going to live in a Rails Engine.
When I create my Friendship
model, it needs a belongs_to
relation for two Users
(two friends). But, I don't want to tie a user to this engine. I want this engine to work generically with any User
an application has established.
What technique does one use to create a dummy User
that is never to be included in the host application? (I want to avoid a migration of the engine pulling in this dummy User
.)
Update:
I removed the second question, pertaining to how to then override the engine's User
with the host app's User
. I found the answer to that in the guides (http://edgeguides.rubyonrails.org/engines.html#configuring-an-engine).
rails g model User
rake db:migrate
I came here looking for an answer, but seeing how there were no answers, I will share how I ended up solving this.
When creating a mountable Rails engine (via something like rails plugin new . --mountable --dummy-path=spec/dummy
), Rails will generate a "dummy" app for your engine that will serve as the main app an engine would normally be required into.
Us RSpec users use the "--dummy-path" directive and put the dummy app in /spec
folder, but for you it may be elsewhere. Find where it is and cd into that dummy app's root.
Once inside the dummy app, call rails g model User
to generate a User model for the dummy app. Run rake db:migrate
to add the table.
Now you have a placeholder User
model that should function acceptably in tests as an association.
You may want to define some mock methods on User
, do so in the model definition file located in /path/to/dummy/app/models/user.rb
Naturally, you can make a full-fledged model with associations and logic instead of just a placeholder right there in dummy.
Migrations and code in dummy app are not used when an engine is included in an app,as rake railties:install:migrations
will show.