Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phoet/active_service
Transactional services for Rails
https://github.com/phoet/active_service
Last synced: 26 days ago
JSON representation
Transactional services for Rails
- Host: GitHub
- URL: https://github.com/phoet/active_service
- Owner: phoet
- Created: 2011-05-30T17:19:29.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-09-17T19:17:55.000Z (about 11 years ago)
- Last Synced: 2024-05-01T22:35:49.763Z (6 months ago)
- Language: Ruby
- Homepage: blog.nofail.de
- Size: 207 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- Changelog: CHANGELOG.rdoc
Awesome Lists containing this project
README
== TODO
* readme is out of date
* add install generator for app/services directory== Common
ActiveService is a Rails extension that aims at
creating reusable, transactional services in
your application to make your code more DRY.== Installation
With bundler:
# Gemfile
gem 'active_service'
$ bundle installor directly
$ (sudo) gem install active_service
== Usage
Start by generating a service:
$ rails generate active_service:service payment
This will generate an empty stub for you in _app/services_:
# app/services/payment_service.rb
class PaymentService < ActiveService::Base
def do_something_important
# your important code that should not fuck up comes here
end
endIf you want to use this service in your controller, just add a makro for it:
# app/controllers/
class PaymentController < ApplicationController
service :payment
def crazy_payment_action
if services(:payment).do_something_important
flash.info 'epic win!'
else
redirect_to :back, :error => 'epic fail!'
end
end
endThis makro is also available in the services themselves, so that you can nest them.
== Good to know
You can always disable the transactional behavior, which might be useful when nesting services,
by using the bang-style method name:service.do_something_important!
ActiveService will wrap all the methods that you define in your service.
If you want to execute them directly you can do this by prefixing them:service.__do_something_important
Nested Transactions are NOT supported!
== Configuration
$ rails generate active_service:config
After running the Rails generator you have a new folder _app/services_
where you can place your services. This folder is added to the rails load-path automagically.If you do not like this folder have a look at the default configuration which has been
generated into an initializer at _config/active_service.yml_:path: "lib/services"
== More on Transactions
Have a look into the API-Docs[http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html]
for more information about Transactions.