Skip to content
Skinnying up controllers
Ruby JavaScript CoffeeScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
app
config
db
features
lib
log
presentation
public
script
spec
vendor
.gitignore
Gemfile
Gemfile.lock
README.md
README.textile
Rakefile
config.ru

README.md

Skinny Controllers

Rails forces an individual to adhere to the Model-View-Controller (MVC) conventions, but it is still incredibly easy for a developer to take the project off "the rails" by not trying to keep the cleanest and DRY.

One area that usually receives the most punishment are our poor controllers. As they sit between the models and the views, they have access to a lot of data and thus conventions can get lost and your controllers can start getting bloated.

Exercise

This week is a sample Rails application that allows a user to login and authenticate with various services. After a user has authorized the various services when the user creates a post, they can have it appear on the various services.

The objective of the exercise is to skinny up the Posts Controller in the sample code.

app/controller/posts_controller.rb#create is controller action that you should be addressing.

You should generate a test account for Twitter and/or Facebook to ensure you do not annoy your friends as you create sample posts.

Goal

Understand the code.

Move the code that publishes content to Twitter and Facebook out of controller and move it into model callbacks or into an observer.

Exercise Retrospective

Imagine adding a new service

  • How maintainable would logic within the controller become?

  • model callbacks?

  • a single 'PostObserver'?

  • an observer for each service?

Imagine removing a service

  • How maintainable would logic within the controller become?

  • model callbacks?

  • a single 'PostObserver'?

  • an observer for each service?

Handling service specific error messages

  • How maintainable would logic within the controller become?

  • model callbacks?

  • a single 'PostObserver'?

  • an observer for each service?

Responsiveness

  • What happens to the user's request when a service hangs or takes a long time to respond? Do any of these implementation address this issue?

Reading

Further Exercise

This project boilerplate allows you to authenticate and post to additional services. This is a common pattern with applications to add these services.

  • Attempt to add a new authentication service

    Google, Github, etc ...

This is the first part of an exercise. The second part of the exercise we are going to look at moving the work that is being performed here in an offline job through Resque and Redis.

  • Install Redis and Resque

    Start to explore Redis and see if you can interact with the server through Resque gem or through another Redis gem.

    Redis supports many different variable types to store information. Try and use some of these commands to store and retrieve data.

Something went wrong with that request. Please try again.