GitHub license GitHub release Build Status Code Climate Test Coverage Dependency Status

Make your models acts as UseCase, easy ;)

UseCase are enhanced Service objects :

You can then call success? on the UseCase object and a bunch of other methods :

Installation

gem 'active_use_case', '~> 1.1.0', git: 'https://github.com/jbox-web/active_use_case.git', tag: '1.1.0'

then bundle install.

Usage

class Post < ApplicationRecord
  add_use_cases [:send_email, :send_sms], namespace: self.name.pluralize
end

In app/use_cases/posts/send_email :

module Posts
  class SendEmail
    def execute(email)
      if job success
        # Do what you want
      else
        # Store the error
        error_message('Error while sending email!')
      end
    end
  end
end

In app/use_cases/posts/send_sms :

module Posts
  class SendSms
    def execute(phone_number)
      if job success
        # Do what you want
      else
        # Store the error
        error_message('Error while sending sms!')
      end
    end
  end
end

Wherever you want :

task = post.send_email!('foo@bar.com')
puts task.success?
puts task.message_on_success?
puts task.message_on_errors?
puts task.message_on_start?
puts task.errors (=> ['Error while sending email!'])

task = post.send_sms!('0123456789')
puts task.success?
puts task.message_on_success?
puts task.message_on_errors?
puts task.message_on_start?
puts task.errors (=> ['Error while sending sms!'])

You can pass any parameters you want and even blocks :

module Posts
  class DoSomething
    def execute(arg1, arg2, opts = {}, &block)
      if job success
        # Do what you want
      else
        # Store the error
        error_message('Error while doing something!')
      end
    end
  end
end
task = post.do_something!('foo@bar.com', 'baz', force: true) do
  puts 'Foo!'
end

puts task.success?
puts task.message_on_success?
puts task.message_on_errors?
puts task.message_on_start?
puts task.errors (=> ['Error while doing something!'])

Contributors

A big thank to them for their contribution!

Contribute

You can contribute to this plugin in many ways such as :