README.md in service_it-1.0.0 vs README.md in service_it-1.1.0

- old
+ new

@@ -1,71 +1,112 @@ -[![Gem Version](https://badge.fury.io/rb/service_it.svg)](https://badge.fury.io/rb/service_it) [![Build Status](https://travis-ci.org/iago-silva/service_it.svg?branch=master)](https://travis-ci.org/iago-silva/service_it) [![Code Climate](https://codeclimate.com/github/iago-silva/service_it.png)](https://codeclimate.com/github/iago-silva/service_it) - # ServiceIt +[![Gem Version](https://badge.fury.io/rb/service_it.svg)](https://badge.fury.io/rb/service_it) [![Build Status](https://travis-ci.org/iago-silva/service_it.svg?branch=master)](https://travis-ci.org/iago-silva/service_it) [![Code Climate](https://codeclimate.com/github/iago-silva/service_it.png)](https://codeclimate.com/github/iago-silva/service_it) [![Test Coverage](https://api.codeclimate.com/v1/badges/fcc8375ebe8fa5412381/test_coverage)](https://codeclimate.com/github/iago-silva/service_it/test_coverage) -[Service objects are a holy grail to keep your controllers and models slim and readable](https://medium.com/selleo/essential-rubyonrails-patterns-part-1-service-objects-1af9f9573ca1) +- [ServiceIt](#serviceit) + - [Installation](#installation) + - [With Bundler](#with-bundler) + - [Rails Generator](#rails-generator) + - [Usage](#usage) + - [Example](#example) -<br/> - ## Installation $ gem install service_it - + ## With Bundler Add this line to your `Gemfile`: - gem 'service_it', '~> 0.2.0' + gem 'service_it', '~> 1.0.0' And then execute: $ bundle - -## With Rails +## Rails Generator + You can use Rails generator to create a `Service` $ rails g service NAME +This will create: + +``` +├── app + ├── services +    └── name.rb +``` + ## Usage ```ruby class Foo < ServiceIt::Base def perform - # here you can use params that became instance variables + # put your logic here + # you can use params that became variables end end ``` Call your service from anywhere + ```ruby -Foo.call(params) -``` +Foo.call(foo: foo, bar: bar) +``` -## Example +## Example -Simple example _using Rails_ to change status of a _transaction_ to complete +Simple example to release a _POST_ - $ rails g service CompleteTransaction +* Before ```ruby -# app/services/complete_transaction.rb -class CompleteTransaction < ServiceIt::Base - def perform - @transaction.update(:status, :complete) +# 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 + ```ruby -if CompleteTransaction.call(transaction: transaction) - puts 'Transaction complete!' -end -``` +# app/controllers/post_releases_controller.rb +class PostReleasesController < ApplicationController -## Contributing + # [...] -Bug reports and pull requests are welcome on GitHub at https://github.com/iago-silva/service_it. + def update + if ReleasePost.call(post: @post) # <-- + # [...] + else + # [...] + end + end -## License + # [...] -The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). +end +``` + +```ruby +# app/services/release_post.rb +class ReleasePost < ServiceIt::Base + def perform + post.prepare_to_release + post.update(released_at: Date.current) + end +end +```