specify model validations declaratively in one place rather than invoking them

Specify model validations declaratively in one place

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

specify model validations declaratively in one place, rather than invoking them explicitly at each join point in the code where we’d want to perform a validity check. A set of join points is collectively called a pointcut , and the code to be inserted at each join point (such as a validation in our example) is called advice . Ruby doesn’t support full AOP, which would allow you to specify arbitrary pointcuts along with what advice applies to each. But Rails uses Ruby’s dynamic language features to define convenient pointcuts such as the AR model lifecycle, which supports validations and other lifecycle callbacks, and join points around controller actions, which support the use of before- and after-filters. A critique of AOP is that the source code can no longer be read in linear order. For example, when a before-filter prevents a controller action from proceeding, the problem can be hard to track down, especially for someone unfamiliar with Rails who doesn’t realize the filter method isn’t even being called explicitly but is an advice method triggered by a particular join point. A response to the critique is that if AOP is applied sparingly and tastefully, and all developers understand and agree on the pointcuts, it can improve DRYness and modularity. Validations and filters are the Rails designers’ attempt to identify this beneficial middle ground. Self-Check 5.1.1. Why didn’t the Rails designers choose to trigger validation when you first instantiate one using Movie#new , rather than waiting until you try to persist the object? As you’re filling in the attributes of the new object, it might be in a temporarily invalid state, so triggering validation at that time might make it difficult to manipulate the object. Persisting the object tells Rails “I believe this object is ready to be saved.” Self-Check 5.1.2. Why can’t we write validate released_1930_or_later , that is, why must the argument to validate be either a symbol or a string? If the argument is just the “bare” name of the method, Ruby will try to evaluate it at the moment it executes validate , which isn’t what we want—we want released_1930_or_later to be called at the time any validation is to occur. 5.2 Single Sign-On and Third-Party Authentication One way to be more DRY and productive is to avoid implementing functionality that you can instead reuse from other services. One example of this today is authentication —the process by which an entity or principal proves that it is who it claims to be. In SaaS, end users and servers are two common types of principals that may need to authenticate themselves. Typically, a user proves her identity by supplying a username and password that (presumably) nobody else knows, and a server proves its identity with a server certificate (discussed in Chapter 12 ) whose provenance can be verified using cryptography.
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