Flexible Rails
Total Page:16
File Type:pdf, Size:1020Kb
Flexible Rails Flexible Rails FLEX 3 ON RAILS 2 PETER ARMSTRONG MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] Copyright © 2006-2007 by Peter Armstrong Version 2007-12-29 All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means, including information storage and retrieval systems, without permission in writing from Peter Armstrong, except by a reviewer who may quote brief passages in a review. Rails, Ruby on Rails, and the Rails logo are trademarks of David Heinemeier Hansson. Flash, Flex, and Flex Builder are trademarks of Adobe Systems Incorporated. Furthermore, many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peter Armstrong was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. Much effort went into the preparation of this book. However, the publisher and author assume no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. The opinions expressed within are solely the personal opinions of Peter Armstrong. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine. Manning Publications Co. Copyeditor: Tiffany Taylor Sound View Court 3B Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-50-9 Printed in the United States of America 12345678910–MAL –13121110090807 For Caroline and Evan brief contents PART 1GETTING STARTED ....................................................... 1 1 ■ Why are we here? Where are we going? 3 2 ■ Hello World 14 3 ■ Getting started 52 PART 2BUILDING THE APPLICATION..................................... 103 4 ■ Creating the main Flex UI 105 5 ■ Expanding the Rails code, RESTfully 118 6 ■ Flex on Rails 186 7 ■ Validation 261 PART 3REFACTORING.......................................................... 293 8 ■ Refactoring to Cairngorm 295 9 ■ Holding state on the client properly 369 PART 4FINISHING UP........................................................... 419 10 ■ Finishing the application 421 11 ■ Refactoring to RubyAMF 468 12 ■ Rails on AIR (Adobe Integrated Runtime) 512 vii contents foreword xvii preface xix acknowledgments xxii about this book xxiv about the cover illustration xxxi PART 1GETTING STARTED......................................... 1 Why are we here? Where are we going? 3 1 1.1 Overview of the features and strengths of Flex 3 and Rails 2 5 Overview of Flex 3 5 ■ Overview of Rails 2 5 ■ Overview of using Flex 3 and Rails 2 together 7 1.2 Flash 9? Are you kidding me? 8 1.3 History 9 1.4 A preview of the book 12 1.5 Summary 13 ix x CONTENTS Hello World 14 2 2.1 Installing everything 16 Installing Ruby 16 ■ Installing Rails 17 ■ Installing MySQL 18 ■ Installing Flex 18 ■ Installing a text editor or IDE (SDK users only) 21 ■ Creating the Rails project 22 ■ How to read the rest of this iteration 24 2.2 Windows or Mac OS X + Flex Builder 3 25 Creating the Flex project 25 ■ “Hello World” from Flex 26 ■ “Hello World” from Rails 28 ■ “Hello World” from Flex and Rails 31 2.3 Windows + Flex SDK 32 Creating the Aptana RadRails project 32 ■ “Hello World” from Flex 34 ■ “Hello World” from Rails 38 ■ “Hello World” from Flex and Rails 41 2.4 Mac OS X (or Linux) + Flex SDK 43 Creating the TextMate project (or launch Emacs or vi) 43 ■ “Hello World” from Flex 43 ■ “Hello World” from Rails 46 ■ “Hello World” from Flex and Rails 49 2.5 Summary 51 Getting started 52 3 3.1 If you’re starting here 53 3.2 Freezing the Rails version 55 3.3 Disabling browser navigation integration 56 3.4 Adding login functionality to Rails 57 Installing and running restful_authentication 58 ■ Editing and running the CreateUsers migration, and checking the result 59 ■ Adding RESTful routes 62 ■ Modifying the includes and before_filter as instructed by the comments 63 ■ Testing account creation from HTML 67 ■ Testing login from HTML 69 3.5 Adding login functionality to Flex 69 “Hello World,” this time with meaning! 69 ■ Binding? What the…? 71 ■ This MXML looks strange 72 ■ Flex 3 documentation? Where? 73 ■ Stubbing out an account-creation and login UI in Flex 74 ■ Making account create and login functional 80 CONTENTS xi 3.6 Adding data to the test fixtures 95 3.7 Checking the tests 98 3.8 Configuring Flex Builder to run and debug pomodo 99 3.9 Summary 101 PART 2BUILDING THE APPLICATION ...................... 103 Creating the main Flex UI 105 4 4.1 Requirements 106 4.2 Design 106 4.3 Code 108 4.4 Summary 117 Expanding the Rails code, RESTfully 118 5 5.1 A brief note about REST 119 Disclaimer: doing REST wrong 119 ■ What is REST? 120 ■ Why use a RESTful approach? 122 5.2 Calling the user by name 123 Adding a primitive debug console to Flex 124 ■ The case of the missing first name 128 ■ Fixing to_xml temporarily 130 ■ Fixing to_xml permanently 132 5.3 Creating the new resources (including migrations, models, and controllers) 135 Creating the Task, Project, and Location resources 135 Adding the associations to the model 137 ■ A tour of the TasksController 142 ■ Understanding how routing works to set the requested format 146 ■ Making the UsersController and SessionsController RESTful 149 ■ Editing and running the migrations 153 ■ Ad hoc testing with the HTML views 157 5.4 Security 161 Ensuring destructive actions are done by POST 162 ■ Requiring login 162 ■ Access control 165 5.5 Expanding our fixtures and keeping our tests passing 178 5.6 Summary 185 xii CONTENTS Flex on Rails 186 6 6.1 Setup 187 6.2 Listing tasks in Flex 187 Should we use to_xml with :include? 192 6.3 Creating tasks in Flex 195 6.4 Creating and listing projects and locations in Flex 200 6.5 Making the Projects and Locations ComboBoxes work in the TaskCreateBox 208 Refactoring the list data location 208 ■ Making the projects and locations show up 214 ■ Using a ComboBox prompt 217 ■ Adding a None object to the ComboBox dataProvider 219 ■ Saving the project and location choices 222 6.6 About that None project and location 225 6.7 Updating and deleting tasks, projects, and locations 228 Adding update_xml and destroy_xml methods to the Rails controllers 228 ■ Getting ComboBox itemRenderers to work in the TasksListBox 231 ■ Adding Delete buttons 243 Adding Completed CheckBoxes to the TasksListBox 245 Editing the task name and notes in the TasksListBox 248 Adding Delete buttons and Completed check boxes to the ProjectsListBox and LocationsListBox 251 6.8 Keeping our tests passing 259 6.9 Summary 259 6.10 Exercises for the reader 259 Validation 261 7 7.1 Revisiting the HTML account signup screen 262 7.2 Rails and Flex validation—should you stay DRY? 266 7.3 Understanding Rails validation, and building custom XML for errors 267 7.4 A Quick look at validation in Flex 3 274 7.5 Integrating Rails validation with Flex 3 validation 276 7.6 Flex validators revisited 284 7.7 Keeping our tests passing 290 CONTENTS xiii 7.8 Summary 290 7.9 Exercises for the reader 291 PART 3REFACTORING ........................................... 293 Refactoring to Cairngorm 295 8 8.1 Background and setup 296 Cairngorm history 297 ■ Do you need to use Cairngorm? 298 ■ Downloading Cairngorm 2.2.1 299 ■ Importing the Cairngorm sources into Flex Builder 299 ■ Getting and running the ModifiedCairngormStore 300 ■ Adding Cairngorm to pomodo 302 ■ Creating the standard directories 303 8.2 Cairngorm event sequence overview 304 8.3 Creating com.pomodo.model. PomodoModelLocator 305 8.4 Creating com.pomodo.control.* 309 EventNames.as 309 ■ PomodoController.as 310 8.5 Adding CairngormUtils and ServiceUtils to com.pomodo.util.* 312 CairngormUtils.as 312 ■ ServiceUtils.as 313 8.6 Creating com.pomodo.command.* 315 CreateTaskCommand.as 315 ■ CreateProject- Command.as 316 ■ CreateLocationCommand.as 318 CreateSessionCommand.as 318 ■ CreateUser- Command.as 320 ■ UpdateTaskCommand.as 321 UpdateProjectCommand.as 322 ■ UpdateLocation- Command.as 324 ■ DestroyTaskCommand.as 325 DestroyProjectCommand.as 326 ■ DestroyLocation- Command.as 327 ■ ListTasksCommand.as 328 ListProjectsCommand.as 329 ListLocationsCommand.as 330 8.7 Creating com.pomodo.business.* 331 TaskDelegate.as 331 ■ ProjectDelegate.as 332 LocationDelegate.as 334 ■ UserDelegate.as 335 SessionDelegate.as 336 xiv CONTENTS 8.8 Deleting the com.pomodo.events package 336 8.9 Modifying the com.pomodo.components.* 337 TaskCreateBox.mxml 337 ■ ProjectCreateBox.mxml 339 LocationCreateBox.mxml 340 ■ TasksListBox.mxml 342 ProjectsListBox.mxml 347 ■ LocationsListBox.mxml 350 AccountCreateBox.mxml 352 ■ LoginBox.mxml 354 MainBox.mxml 355 ■ SplashBox.mxml 358 8.10 Modifying Pomodo.mxml 359 8.11 Running pomodo 361 8.12 HTTPService Gotchas 362 8.13 Summary 368 8.14 Exercise for the reader 368 Holding state on the client properly 369 9 9.1 Refactoring, samurai coder style