ServiceIt
Service objects are a holy grail to keep your controllers and models slim and readable
Installation
$ gem install service_it
With Bundler
Add this line to your Gemfile:
gem 'service_it', '~> 0.2.0'
And then execute:
$ bundle
With Rails
You can use Rails generator to create a Service
$ rails g service NAME
Usage
class Foo < ServiceIt::Base
def perform
# here you can use params that became instance variables
end
end
Call your service from anywhere
Foo.call(params)
Example
Simple example using Rails to change status of a transaction to complete
$ rails g service CompleteTransaction
# app/services/complete_transaction.rb
class CompleteTransaction < ServiceIt::Base
def perform
@transaction.update(:status, :complete)
end
end
if CompleteTransaction.call(transaction: transaction)
puts 'Transaction complete!'
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/iago-silva/service_it.
License
The gem is available as open source under the terms of the MIT License.
