![]() In this case, don’t use a service object-use a concern. Are you trying to share your code in different controllers?.If so, don’t use a service object-your code belongs in the controller. Does your code handle routing, params or do other controller-y things?.This eases one’s use of Ruby, since rules applying to objects apply to all of Ruby. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types. In many languages, numbers and other primitive types are not objects. Exactly what we we wanted! The last thing to do is to rename the method from our TweetCreator class to call, and have the class inherit from ApplicationService: # app/services/tweet_creator.rbĬlass TweetCreator 5.is_a? Object # => true So let’s make our service object behave more like a proc!įirst, because we probably want to reuse this behavior across all our service objects, let’s borrow from the Rails Way and create a class called ApplicationService: # app/services/application_service.rbĭid you see what I did there? I added a class method called call that creates a new instance of the class with the arguments or block you pass to it, and calls call on the instance. Which means, that if TweetCreator were a proc, we could call it with TweetCreator.call(message) and the result would be equivalent to TweetCreator.new(params).call, which looks quite similar to our unwieldy old TweetCreator.new(params).send_tweet. ![]() A proc can be call-ed to execute itself with the given parameters. It returns the value of the last expression evaluated in the block. Proccall invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. TweetCreator is a nice short class name, but the extra cruft around instantiating the object and calling the method is just too long! If only there were precedence in Ruby for calling something and having it execute itself immediately with the given parameters… oh wait, there is! It’s Proc#call. I guess that’s another article for another time. In all seriousness, though, why do people use HTML when HAML is around? Or even Slim. It’s far too verbose with redundant words… much like HTML (ba-dum tiss!). Look, this feels great in theory, but TweetCreator.new(params).send_tweet is just a mouthful. Adding Syntactic Sugar to Make Rails Service Objects Suck Less Want to know more about how autoload works? Read the Autoloading and Reloading Constants Guide. Users/gilani/Sandbox/nazdeeq/app/services If you’ve been using vanilla Rails so far, then you’ve probably done something like this: class TweetController puts ActiveSupport::toload_paths Try this: What do you do when your application needs to tweet the text from params? In this article, I’ll explain when a service object is required how to go about writing clean service objects and grouping them together for contributor sanity the strict rules I impose on my service objects to tie them directly to my business logic and how not to turn your service objects into a dumping ground for all the code you don’t know what to do with.
0 Comments
Leave a Reply. |