5 ms·
I've been looking into incorporating this pattern into my larger Rails apps as well. Another benefits of interactions is DRYing up your code for use in APIs. So
by Axsuul 13y ago
I've been looking into incorporating this pattern into my larger Rails apps as well. Another benefits of interactions is DRYing up your code for use in APIs. Some of the more popular interactor gems:
https://github.com/orgsync/active_interaction https://github.com/orgsync/active_interaction
https://github.com/cypriss/mutations https://github.com/cypriss/mutations
- taylorfausak 13y agoThanks for the shout out! I'm one of the developers of ActiveInteraction. Since most of the interactor gems are pretty similar, here's why you might prefer ActiveInteraction over Mutations, Interactor, et al.: - Built-in ActiveModel validations. - Designed to be used with form objects. - Easier and more flexible composition. - Translatable with I18n. - Quacks like an ActiveModel. That being said, Mutations is a great library for PORO interactors and ActiveInteraction wouldn't exist without it.
- rubiquity 13y agoIf you need a gem to implement some of these complementary patterns, you're doing it wrong. Draper, Naught, DisplayCase and all of the other gems of patterns are over abstraction 99% of the time. Implement the pattern yourself. Most of these Presenter and Service object patterns are different variations of the Decorator pattern. Ruby happens to ship with 3 great ways to implement decorators: SimpleDelegator (my personal favorite)[0], Delegator[1] and Forwardable[2]. 0 - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/delegate/rdoc/SimpleDelegator.html http://www.ruby-doc.org/stdlib-1.9.3/libdoc/delegate/rdoc/Si... 1 - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/delegate/rdoc/Delegator.html http://www.ruby-doc.org/stdlib-1.9.3/libdoc/delegate/rdoc/De... 2 - http://www.ruby-doc.org/stdlib-2.0/libdoc/forwardable/rdoc/Forwardable.html http://www.ruby-doc.org/stdlib-2.0/libdoc/forwardable/rdoc/F...
- ryanbrunner 13y agoThe biggest pain points that gems like Draper solve are allowing view helpers in your decorators (which is by no means straightforward when you get into routing helpers), and dealing with some of the strangeness that you get into by wrapping ActiveRecord models (JSON serialization was a biggie for us). It's less about what Ruby gives you, and more about navigating around some of the darker, messier corners of Rails.
- rubiquity 13y agoThey just wrapped the view_context with a method_missing proxy (another form of a Decorator) that the controller instance gives you. You can accomplish the same very easily using dependency injection in your controller action: MassivelyUpVotedPostPresenter.new(@post, self.view_context) Or just inject the whole controller instance and you have your router helpers too! MassivelyUpVotedPostPresenter.new(@post, self)
- happyshadows 13y agoMutations is a great gem. I've used in my latest project and it has been an exceptionally useful tool for managing complexity.