dorianmarie.fr

[email protected]

Engines

Let’s say we have a simple app with only users:

bin/rails generate scaffold user name

Then bin/rails db:migrate

Now you can launch your bin/rails server and go to localhost:3000/users.

Let’s create an engine to handle payments for our app:

bin/rails plugin new payments --mountable

We need to clean and fill a bit the gemspec of the engine (payments/payments.gemspec).

Let’s have something like:

require_relative "lib/payments/version"

Gem::Specification.new do |spec|
  spec.name = "payments"
  spec.version = Payments::VERSION
  spec.authors = ["Dorian Marié"]
  spec.email = ["[email protected]"]
  spec.license = "MIT"
  spec.summary = "handles payments"
  spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

  spec.add_dependency "rails", "~> 6.1.3", ">= 6.1.3.2"
end

We will need to add it to our config/routes.rb

mount Payments::Engine => "/payments"

In the payments/ directory:

bin/rails generate scaffold payment user:references amount:integer

Then the migration needs to be copied to the main app:

rake payments:install:migrations

(this is just an example, amount should probably be in cents and have a currency)

Then bin/rails db:migrate

(I needed to do gem update --system)

Now you need to link the engine css from the main app, in app/assets/stylesheets/application.css:

 *= link payments/application.css

And you can go to localhost:3000/payments/payments