Service objects helps to keep your controllers and models slim and readable
Simple example
- Before
# app/controllers/post_releases_controller.rb class PostReleasesController < ApplicationController # [...] def update @post.prepare_to_release # <-- if @post.update(released_at: Date.current) # <-- redirect_to root_path else render :edit end end # [...] end
- After
# app/controllers/post_releases_controller.rb class PostReleasesController < ApplicationController # [...] def update if ReleasePost.call(post: @post) # <-- redirect_to root_path else render :edit 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
Feedbacks are most welcome!
Here's its Github: https://github.com/iago-silva/service_it
Top comments (0)