ServiceIt

Gem Version Build Status Code Climate Test Coverage

Its benefit is to facilitate the creation of Service Objects, providing you the basic and enough to have a complete one and letting you free to use on your own way.

Installation

$ gem install service_it

With Bundler

Add this line to your Gemfile:

gem 'service_it', '~> 1.2.0'

And then execute:

$ bundle

Rails Generator

You can use Rails generator to create a Service

$ rails g service NAME

This will create:

├── app
    ├── services
        └── name.rb

Usage

class Foo < ServiceIt::Base
  def perform
    # put your logic here
    # you can use params that became variables
  end
end

Call your service from anywhere

Foo.call(foo: foo, bar: bar)

Example

Simple example to release a POST

  • Before
# app/controllers/post_releases_controller.rb
class PostReleasesController < ApplicationController

  # [...]

  def update
    @post.prepare_to_release                      # <--
    if @post.update(released_at: Date.current)    # <--
      # [...]
    else
      # [...]
    end
  end

  # [...]

end

  • After
# app/controllers/post_releases_controller.rb
class PostReleasesController < ApplicationController

  # [...]

  def update
    if ReleasePost.call(post: @post)    # <--
      # [...]
    else
      # [...]
    end
  end

  # [...]

end
# app/services/release_post.rb
class ReleasePost < ServiceIt::Base
  def perform
    post.prepare_to_release
    post.update(released_at: Date.current)
  end
end