Rails integration for Alki! Allows easily using Alki in your Rails project. Alki is a dependency injection and program organization framework.
Why use Alki with Rails?
While Rails is a fantastic web framework, it doesn’t offer a lot of tools to manage your core business logic. Common idioms like "Fat Model" or "Fat Controller" work ok for a while, but most large Rails projects get to a point where their business logic grows beyond Rails' basic MVC paradigm.
Using Alki to manage your business logic allows your Rails code to focus on what it does best: serve web pages. With Alki::Rails, Alki integrates seamlessly with Rails, while also keeping your core code encapsulated.
What about Rails Services?
Recent versions of Rails have added an app/services directory as a place to put
dedicated service objects containing your business logic.
The key difference between these Rails services and Alki services is that Rails services are classes or modules, referenced directly by consumers when needed, while Alki services can be any time of object, but are typically instances of a service class.
By using instances, Alki allows services to be configured and dependency injected before being used.
Installation
Add this line to your Rails application’s Gemfile:
gem 'alki-rails'
And then execute:
$ bundle
Finally, run the generator to create an empty config/assembly.rb and to make your
assembly elements accessible in your controllers and in the console.
$ bin/rails generate alki
Usage
Elements like services and application settings are defined in your Assembly definition
(config/assembly.rb). These elements are accessible by name directly within controllers
and the rails console.
See here for more documentation on how to use Alki.
Alki do
group :orders do
service :manager do
require 'order_manager'
OrderManager.new model, payment_processor
end
service :model do
Order
end
service :payment_processor do
require 'stripe_processor'
StripeProcessor.new
end
end
end
class OrdersController < ApplicationController
def post
@order = orders.manager.place_order params[:order]
end
end
class OrderManager
def initialize(model,payment_processor)
@model = model
@payment_processor = payment_processor
end
...
end
class StripeProcessor
...
end
Elements can also be accessed anywhere in your Rails application, via Alki::Rails:
Alki do
group :settings do
set :msg, 'Hello World'
end
end
2.3.2 :001 > settings.msg
=> 'Hello World'
2.3.2 :001 > Alki::Rails.settings.msg
=> 'Hello World'
Alki will automatically add your projects lib directory to the ruby load path, so you can require files from there directly. It also will handle auto-reloading files in lib.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/alki-project/alki-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.