1 class 2

This preview shows page 177 - 180 out of 517 pages.

1 class ApplicationController < ActionController::Base 2 before_filter :set_current_user
Image of page 177
3 protected # prevents method from being invoked by a route 4 def set_current_user 5 # we exploit the fact that find_by_id(nil) returns nil 6 @current_user ||= Moviegoer.find_by_id(session[:user_id]) 7 redirect_to login_path and return unless @current_user 8 end 9 end Figure 5.7: If there is a logged-in user, the redirect will not occur, and the controller instance variable @current_user will be available to the action and views. Otherwise, a redirect will occur to login_path , which is assumed to correspond to a route that takes the user to a login page, about which more in Section 5.2 . ( and is just like && but has lower precedence, thus ((redirect_to login_path) and (return)) unless... ) Filters normally apply to all actions in the controller, but :only can be used to specify that the filter only guards certain actions, while :except can be used to specify that some actions are exempt. Each takes an array of action names. You can define multiple filters: they are run in the order in which they are declared. You can also define after-filters, which run after certain actions are completed, and around- filters, which specify actions to run before and after, as you might do for auditing or timing. Around- filters use yield to actually do the controller action: 1 # somewhat contrived example of an around-filter 2 around_filter :only => [’withdraw_money’, ’transfer_money’] do 3 # log who is trying to move money around 4 start = Time.now 5 yield # do the action 6 # note how long it took 7 logger.info params 8 logger.info (Time.now - start) 9 end Summary of DRYing out MVC in Rails: Partials allow you to reuse chunks of views across different templates, collecting common view elements in a single place. Validations let you collect constraints on a model in a single place. Validations are checked anytime the database is about to be modified; failing validation is one of the ways that non- dangerous save and update_attributes can fail. The errors field of a model, an ActiveRecord::Errors object, records errors that occurred during validation. View form helpers can use this information to apply special CSS styles to fields whose values failed validation, cleanly separating the responsibility for detecting an error from the responsibility for displaying that error to the user. Controller filters let you collect conditions affecting many controller actions in a single place by defining a method that always runs before those actions. A filter declared in a controller affects all actions in that controller, and a filter declared in ApplicationController affects all actions in all controllers, unless :only or :except are specified.
Image of page 178
ELABORATION: Aspect-oriented programming Aspect-oriented programming (AOP) is a programming methodology for DRYing out code by separating crosscutting concerns such as model validations and controller filters from the main code of the actions to which the concerns apply. In our case, we
Image of page 179
Image of page 180

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors