All threads / How do I implement Facades Pattern on a already advanced Ruby on Rails project?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I implement Facades Pattern on a already advanced Ruby on Rails project?

Andres Max asked in General

A few days ago read the four rules for developers and the Facade Pattern got my attention, however, find it somehow complicated to implement due to lack of good tutorials or the ones I've read implement it in very different ways.

Any help would be appreciated. Thanks.

The Facade Pattern is a pretty simple one. In Rails the guideline is usually to only have like one instance variable that your controller sets. Reality is that often times you need several variables to render the page, so they create a little Ruby object that's nothing special like their example of Dashboard. It just has a few methods so you interact with the Dashboard instead of several different instance variables in the view.

This pattern doesn't really improve your code all that much. It more hides the complexity behind a facade (hence the name) but it significantly improves the controller code.

You can simply create an app/facades folder for this and as long as the directory loads (you may need to add it to the Rails config, can't remember), then all you'll have to do is create some basic Ruby classes that load the correct records and you're all set.

Since this pattern is simply and helpful but "doesn't really improve your code...", what would you recommend instead?

Thanks a lot!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,665+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.