250 Form models [2d]

Updated . Posted . Visible to the public.

Reading

Read (or re-read) the following chapters from our book Growing Rails Applications in Practice Show archive.org snapshot (it’s in our library):

  • New rules for Rails
    • Beautiful controllers
    • Relearning ActiveRecord
    • User interactions without a database
  • Creating a system for growth
    • Dealing with fat models
    • A home for interaction-specific code
    • Extracting service objects
    • Organizing large codebases with namespaces

Note

We're no longer using the As naming convention when extending models with ActiveType::Record[User]. Instead just pick whatever substantive best describes the extended class. Note that we prefer the verbose notation of parent namespaces and classes - also for specs:

class User
  class RegistrationForm < ActiveType::Record[User]
    ...
  end
end

Watch Tobias Pfeiffer's talk: Do You Need That Validation? Let Me Call You Back About It Show archive.org snapshot

Talk with your mentor about the motivations behind the use of form models.

Exercises

Go through the repos of Cards and/or MovieDB and apply what you learned:

  • Create a form model backed by a database
    • Have two different forms to create a user: One for public sign up, one in a private admin area
    • The sign up form sends a welcome e-mail and checks for password policy, the admin form doesn't.
      • If you are struggling with this, just have a method send_welcome_email that does nothing. If time allows, try changing this to send a simple e-mail once you are done with everything else in this card.
    • Implement the public sign up form once as an ActiveType::Object and once as an ActiveType::Record[User]. What are the pros and cons of each approach?
  • Create a form model not backed by a database
    • Implement the login form using ActiveType::Object
    • Implement a screen to merge two movies.
      • Flat attributes are copied from the source movie if it is missing on the target movie.
      • The poster is copied from the source movie if the target movie has no poster yet.
      • Lists of actors are merged, but actors found in both lists are only kept once.
      • Showtimes are merged as well (hint: this is easier than merging actors), discarding duplicates.
      • The first movie is destroyed after successfully merging into the second movie.
  • Find and talk about examples where a method should better live in a callback and vice versa
  • Find a composition of classes and move the child classes into the namespace of the container class.
  • Move a group of related classes into a namespace.
Henning Koch
Last edit
Henning Koch
Keywords
form, objects
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-08-04 08:18)