ebook img

Rails for Java Developers PDF

323 Pages·2007·2.01 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Rails for Java Developers

What readers are saying about Rails for Java Developers Every Java developer should be exposed to ideas from different lan- guages, and Ruby/Rails is a wellspring of good ideas. Read this book—it will help you learn Ruby and Rails, and give you new ideas transferableto Java. David Bock Technical Director, Federal and Commercial Division, FGM Inc. Stuart and Justin have pulled off what I once thought was an impos- sible feat: consolidating all the knowledge that a Java developer needs to understand Ruby on Rails. Until this book, you would have to read at least three books (and thousands of pages) to get the same under- standing encapsulated in this excellent text. They clearly understand both sides of the equation (RoR and Java), which allows them to cut through irrelevancies and hone in on the important topics. This book should be required reading for more than just the people learning Rails: every Java developer will benefit from learning the important lessons that Rails teaches. Neal Ford Application Architect/Developer, Thoughtworks If you are a Java developer and you want to explore Ruby on Rails, this is the book to get. Justin and Stu do a masterful job of revealing the intricacies of Ruby and Ruby on Rails from a Java developer’s per- spective. Not only that, this book is extremely well written,and is a pleasure to read. David Geary Author of Graphic Java Swing and co-author of Core JavaServer Faces Stu and Justin offer the Java developer the unique opportunity to “get” Rails by presenting the Rails stack from a perspective that’s familiar and comfortable. In doing so, they prove that Rails and Java don’t have to be mutually exclusive. Ted Neward Author of Effective Enterprise Java If you are a Java developer trying to learn Rails, this book is the place to start. There is no better resource for quickly coming up to speed with Rails, Ruby, Rake, and ActiveRecord. Mark Richards Senior IT Architect, IBM To hear some tell it, there’s tension and rivalry between the Java and Rails web development camps, but that’s hard to see from whereI stand. Most of the happy Rails developers I know have a long history as Java programmers, and while we love Java for what it does well, web development in Java leaves a lot to be desired. Rails is a delight- ful breath of fresh air, and I’m confident this book will open the eyes of a lot of other Java developers who are looking for a nicer way to build web applications. Glenn Vanderburg Independent Ruby and Java consultant Rails for Java Developers Stuart Halloway Justin Gehtland The Pragmatic Bookshelf Raleigh,NorthCarolina Dallas,Texas Many of the designations used by manufacturers and sellers to distinguish their prod- uctsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andThe Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The PragmaticProgrammer,PragmaticProgramming,PragmaticBookshelfandthelinkingg devicearetrademarksofThePragmaticProgrammers,LLC. Every precaution was taken in the preparation of this book. However, the publisher assumesnoresponsibility for errorsor omissions,or for damagesthatmay result from theuseofinformation(includingprogramlistings)containedherein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatictitles,pleasevisitusat http://www.pragmaticprogrammer.com Copyright©2007ThePragmaticProgrammersLLC. Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmit- ted, in any form, or by any means, electronic,mechanical,photocopying, recording, or otherwise,withoutthepriorconsentofthepublisher. PrintedintheUnitedStatesofAmerica. ISBN-10:0-9776166-9-X ISBN-13:978-0-9776166-9-5 Printedonacid-freepaperwith85%recycled,30%post-consumercontent. Firstprinting,February,2007 Version:2007-2-12 Contents Foreword 11 Preface 13 1 Getting Started with Rails 20 1.1 Setting Up Ruby and Rails . . . . . . . . . . . . . . . . . 20 1.2 Rails App in Fifteen Minutes . . . . . . . . . . . . . . . . 21 1.3 The Rails Development Cycle . . . . . . . . . . . . . . . . 25 1.4 Finding Information in Online Documentation . . . . . . 28 1.5 Editors and IDEs . . . . . . . . . . . . . . . . . . . . . . . 29 1.6 Running the Samples . . . . . . . . . . . . . . . . . . . . 30 1.7 Rails Environments . . . . . . . . . . . . . . . . . . . . . 32 1.8 How Rails Connects to Databases . . . . . . . . . . . . . 35 1.9 Rails Support Scripts . . . . . . . . . . . . . . . . . . . . 36 2 Programming Ruby 38 2.1 Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . 38 2.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.3 Objects and Methods . . . . . . . . . . . . . . . . . . . . 44 2.4 Collections and Iteration . . . . . . . . . . . . . . . . . . 47 2.5 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.6 Defining Classes . . . . . . . . . . . . . . . . . . . . . . . 57 2.7 Identity and Equality . . . . . . . . . . . . . . . . . . . . 63 2.8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 65 2.9 Controlling Access with Access Specifiers . . . . . . . . 67 2.10 Raising and Handling Exceptions . . . . . . . . . . . . . 69 3 Ruby Eye for the Java Guy 72 3.1 Extending Core Classes . . . . . . . . . . . . . . . . . . . 72 3.2 Mutable and Immutable Objects . . . . . . . . . . . . . . 74 3.3 Packages and Namespaces . . . . . . . . . . . . . . . . . 76 3.4 Deploying Code . . . . . . . . . . . . . . . . . . . . . . . . 79 3.5 Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 CONTENTS 8 3.6 Polymorphism and Interfaces. . . . . . . . . . . . . . . . 85 3.7 Duck Typing . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.8 Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 3.9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 4 Accessing Data with ActiveRecord 96 4.1 Getting Connected . . . . . . . . . . . . . . . . . . . . . . 97 4.2 Managing Schema Versions with Migrations . . . . . . . 100 4.3 Mapping Data to Classes . . . . . . . . . . . . . . . . . . 103 4.4 Create, Read, Update, and Delete: Access Patterns . . . 106 4.5 Validating Data Values . . . . . . . . . . . . . . . . . . . 113 4.6 Lifecycle Callbacks . . . . . . . . . . . . . . . . . . . . . . 116 4.7 Associations and Inheritance . . . . . . . . . . . . . . . . 119 4.8 Transactions, Concurrency, and Performance . . . . . . 125 4.9 Conserving Resources with Connection Pooling . . . . . 131 4.10 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5 Coordinating Activities with ActionController 133 5.1 Routing Basics: From URL to Controller+Method . . . . 134 5.2 List and Show Actions: The R in CRUD . . . . . . . . . . 136 5.3 Create, Update, and Delete Actions . . . . . . . . . . . . 140 5.4 Tracking User State with Sessions . . . . . . . . . . . . . 144 5.5 Managing Cross-Cutting Concerns with Filtersand Verify147 5.6 Routing in Depth . . . . . . . . . . . . . . . . . . . . . . . 151 5.7 Logging, Debugging, and Benchmarking . . . . . . . . . 153 5.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 6 Rendering Output with ActionView 167 6.1 Creating Basic .rhtml Files . . . . . . . . . . . . . . . . . 168 6.2 Minimizing View Code with View Helpers . . . . . . . . . 169 6.3 WritingCustom Helpers . . . . . . . . . . . . . . . . . . . 170 6.4 Reuse with Layouts and Partials . . . . . . . . . . . . . . 172 6.5 Building HTML Forms . . . . . . . . . . . . . . . . . . . . 174 6.6 Building HTML with Markaby . . . . . . . . . . . . . . . 178 6.7 Caching Pages, Actions, and Fragments . . . . . . . . . 180 6.8 Creating Dynamic Pages with Ajax. . . . . . . . . . . . . 184 6.9 Rendering JavaScript with RJS . . . . . . . . . . . . . . 191 6.10 Black-Box Testing with Selenium . . . . . . . . . . . . . 192 6.11 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . 196 6.12 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 CONTENTS 9 7 Testing 198 7.1 Getting Started with Test::Unit . . . . . . . . . . . . . . . 199 7.2 Rails Testing Conventions. . . . . . . . . . . . . . . . . . 206 7.3 Rails Extensions to Test::Unit . . . . . . . . . . . . . . . 212 7.4 Integration Testing . . . . . . . . . . . . . . . . . . . . . . 216 7.5 Rails Testing Examples . . . . . . . . . . . . . . . . . . . 218 7.6 Measuring Code Coverage with rcov . . . . . . . . . . . . 222 7.7 Testing Interactions with Mock Objects . . . . . . . . . . 225 7.8 Reducing Dependencies with Stub Objects . . . . . . . . 229 7.9 Advanced Considerations . . . . . . . . . . . . . . . . . . 230 7.10 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 8 Automating the Development Process 233 8.1 Rake Basics . . . . . . . . . . . . . . . . . . . . . . . . . . 234 8.2 Setting Rake Options: It’s Just Ruby . . . . . . . . . . . 236 8.3 Custom Rake Tasks: It’s Just Ruby . . . . . . . . . . . . 238 8.4 Using Rake in Rails Applications . . . . . . . . . . . . . . 240 8.5 Continuous Integration with Cerberus . . . . . . . . . . 243 8.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 9 Creating and Invoking Web Services 247 9.1 RESTful Web Services . . . . . . . . . . . . . . . . . . . . 248 9.2 SOAP Web Services. . . . . . . . . . . . . . . . . . . . . . 255 9.3 YAML and XML Compared . . . . . . . . . . . . . . . . . 261 9.4 JSON and Rails . . . . . . . . . . . . . . . . . . . . . . . . 265 9.5 XML Parsing . . . . . . . . . . . . . . . . . . . . . . . . . 266 9.6 Ruby XML Output . . . . . . . . . . . . . . . . . . . . . . 275 9.7 Creating XML with Builder . . . . . . . . . . . . . . . . . 276 9.8 Curing Your Data Headache . . . . . . . . . . . . . . . . 278 9.9 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 10 Security 282 10.1 Authentication with the acts_as_authenticated Plugin . 283 10.2 Authorization with the Authorization Plugin . . . . . . . 285 10.3 Testing Authentication and Authorization . . . . . . . . 290 10.4 Preventing the Top-Ten Web Security Flaws . . . . . . . 293 10.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 A Java to Ruby Dictionary 303 CONTENTS 10 B Bibliography 306 C Structure of a Rails Project 307 Index 309

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.