Easels App with Test Driven Development: Part 2
Master Test Driven Development with Ruby on Rails
This tutorial builds directly on Exercise 13A. If you haven't completed the previous exercise, follow the setup instructions to get your environment ready.
Key Learning Objectives
User-Specific Data Access
Learn to implement user authentication and ensure users only see their own to-do items through proper database associations.
Test-Driven Development
Continue building features using TDD methodology, writing tests first and implementing functionality to pass those tests.
Database Migrations
Add new fields to existing database tables using Rails migrations and update your models accordingly.
Setup Checklist for New Users
Locate your Rails class directory structure
Use cd command and drag folder to Terminal for correct path
Clean slate ensures no conflicts with previous incomplete work
Download the complete project with all previous exercise solutions
Sets your codebase to the exact state needed for this tutorial
Ensures all Ruby gems and JavaScript packages are properly installed
Copying test steps from previous features saves development time and ensures consistency. The user_creates_todo_spec.rb file contains valuable patterns we can reuse for testing user-specific functionality.
Database Migration Process
Generate Migration File
Use rails g migration AddEmailToTodo email:string to create a new migration that adds an email field
Run Database Migration
Execute rails db:migrate to apply the changes to your database schema
Verify Schema Changes
Check db/schema.rb to confirm the new email field was added to the todos table
The test creates two to-dos with different email addresses to verify that users can only see their own tasks. This is crucial for data privacy and security in multi-user applications.
Key Implementation Components
Sessions Controller
Handles user sign-in functionality and manages session state. Creates new and create actions for login form processing.
Database Associations
Links to-dos with user email addresses through the Todo model. Filters displayed tasks based on current session.
Route Configuration
Adds session routes and updates application flow to require login before accessing to-do functionality.
We replaced Todo.create with Todo.new because create creates the new Todo and saves it all in one step, but now we need to modify it before saving.
After implementing all required components (migration, controller, views, and session management), the test passes and users can only see their own to-dos while background tasks remain hidden.
Key Takeaways