Components in a Rails App

43 views
Skip to first unread message

jk

unread,
Aug 23, 2014, 6:44:15 PM8/23/14
to clean-code...@googlegroups.com
Anyone have experience with Component level design involving Rails? I've been successful with clean architecture in Rails, but when focusing on the component level-- I've run into many questions.

Isolating the Database implementations seems doable -- as the ActiveRecord/Mongoid gem can be standalone. 

But what about a View component or a Controller component?  As expressed in episode 18 (Component Case Study), The view/view model/presenter of a family would be its own component/gem. In Rails, it seems that the 'Controller' plays the role as BOTH the Controller and Presenter. Is it possible to split these 2 concepts into separate components?

Thoughts? experiences?

Thanks!








Jakob Holderbaum

unread,
Aug 24, 2014, 1:12:40 AM8/24/14
to clean-code...@googlegroups.com
Hi jk,

I always use presenters in rails. I let them include ActiveModel, so
they play well with forms and such.

You can extend the load path of a rails application, of course. There
are two possibilities:

Adding just a gem to the Gemfile, so you would have to require the
presenters in the controller (I find this the cleanest approach).

Making the gem a rails engine with a directory app/presenters. By this
you leverage the ActiveSupport AutoLoad (black)magic. If you remove
isolate_namespace from the engine, your sharing namespace and all the
constant lookups in the app will also use the engines app/ and lib/
directories.

I don't really now if you want to extract all the controllers out of
your rails application. Normally, they just instantiate and call
interactors and assign the results to some presenters. Nothing more!

But still, you can build up gems as engines. That means, you can add
Controllers and Views to the current rails application namespace. With
shared namespace you can manipulate the routes of the app and add
controllers/views to it.

Does this shed some light upon your question or did I miss the point?

Cheers
Jakob
--
Jakob Holderbaum, M.Sc.
Systems Engineer

0176 637 297 71
http://jakob.io
h...@jakob.io
#hldrbm

jk

unread,
Aug 24, 2014, 8:33:58 AM8/24/14
to clean-code...@googlegroups.com
First I want to point out that what the rails community calls a presenter and what clean architecture calls a presenter are not quite the same.

Rails folks have seperate terms for the 2 concepts you mentioned:
-A)The active record included, form assisting class you called a presenter is what I know as a "form object". It's a stand in for use when calling the 'form_for' helper.
-B)What I've heard called a presenter in rails is a class that acts as a stand in for the model in the view that moves formatting/display logic out of the view template.

A is input to a view template. B is input to a view template.

I think what I'm seeking is something else. The clean architecture presenter, from what I understand, plays a responding role for the interactor. It accepts the response model and populates a view model which gets picked up by the view. It ALMOST sounds like 'B'... But in this example 'B' is playing the role as a (clean) presenter AND the view model.

I suppose my original post can be boiled down to--- can I have 2 seperate gems -- one with rails/controllers, and one with presenters, views models, and views. Since a (rails) controller plays the role of a controller (duh), renders the view template, and uses itself as the view model... It seems very difficult to seperate.

Reply all
Reply to author
Forward
0 new messages