26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 ·...

21
831 Symbols & Numerics # (pound sign) delimiter, 310 - (minus sign) delimiter, 310 307 redirects, 42 A abstract base model classes, 291-292 acceptance tests, 588-589 AccountController class, Acts_as_Authenticated method, 496-498 Action Cache plugin, 337-338 action caching, 328-330 ActionMailer framework, setting up, 531 actions (Selenium), 589 ActionView, 308 Active Record framework, 140-142, 158 attributes default values, 160-162 serialized, 162 configuring, 196-197 CRUD, 163-178 database connections, 189-196 database locking, 179 optimistic locking, 180-182 pessimistic locking, 182 database schema, naming conventions, 159 finders conditions, 183-185 options, 187-189 parameters, 186 results, ordering, 186 macro-style methods, 155 convention over configuration, 156 pluralization, 157-158 relationship declarations, 155-156 Migration API, 146-148 migrations, 142-145 columns, defining, 149-155 query cache, 172-174 ActiveRecord abstract base model classes, 291-292 callback registration, 272-273 before after/callbacks, 274 callbacks, 271 after_find callback, 278-279 after_initialize callback, 278 classes, 279-282 halting execution, 274 usage examples, 275-278 classes, modifying at runtime, 301-304 common behavior, reusing, 296-301 observers, 282 naming conventions, 283 registration, 283 Index

Transcript of 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 ·...

Page 1: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

831

Symbols & Numerics

# (pound sign) delimiter, 310- (minus sign) delimiter, 310

307 redirects, 42

A

abstract base model classes, 291-292acceptance tests, 588-589AccountController class, Acts_as_Authenticated

method, 496-498Action Cache plugin, 337-338action caching, 328-330ActionMailer framework, setting up, 531actions (Selenium), 589ActionView, 308Active Record framework, 140-142, 158

attributesdefault values, 160-162serialized, 162

configuring, 196-197CRUD, 163-178database connections, 189-196database locking, 179

optimistic locking, 180-182pessimistic locking, 182

database schema, naming conventions, 159finders

conditions, 183-185options, 187-189parameters, 186results, ordering, 186

macro-style methods, 155convention over configuration, 156pluralization, 157-158relationship declarations, 155-156

Migration API, 146-148migrations, 142-145

columns, defining, 149-155query cache, 172-174

ActiveRecordabstract base model classes, 291-292callback registration, 272-273

before after/callbacks, 274callbacks, 271

after_find callback, 278-279after_initialize callback, 278classes, 279-282halting execution, 274usage examples, 275-278

classes, modifying at runtime, 301-304common behavior, reusing, 296-301observers, 282

naming conventions, 283registration, 283

Index

26_5619_index.qxd 10/25/07 10:28 AM Page 831

Page 2: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

polymorphic has_many relationships, 292-296STI, 284-291

inheritance, mapping to database, 286-287ActiveRecord models, creating forms, 362-369ActiveRecord SessionStore, 473-474ActiveRecordHelper module, 342-345, 347-348ActiveResource, 519

Create method, 522-523customizing, 527-528Delete method, 524-525Find methods, 519-521headers, 526Update method, 524

ActiveSupport library, 726-739, 759Array class, 724Class object, 729-730default log formatter, 764-765dependencies, 740

module attributes, 740-741public instance methods, 742-743

Deprecation module, 744-745Enumerable module, 747-748Exception module, 748FalseClass module, 749File module, 749Hash module, 750-757Inflections class, 757-759Module class, 765-769NilClass, 772-773Numeric class, 773-776Object class, 776-777, 779Proc class, 781-782Range class, 782String class, 783-790Symbol class, 790Time class, 792-798

active_record_defaults plugin, 807acts-as-authenticated plugin

AccountController class, 496-498authenticate method, 494before-save callback, 493current user, 499-501installing, 486-487login from cookie, 498-499remember token, 495

User model, 487-492validation rules, 492

Adam, James, 650adding

classes to mocks folder, 548-549load paths, 10plugin sources, 630

after_find callback, 278-279after_initialize callback, 278Ajax, Prototype, 420-421

Class object, 424Enumerable object, 437-443Firebug, 421Hash class, 443Object class, 425-426Prototype object, 445Responders object, 437top-level functions, 422-424

alert() method (RJS), 457aliases, 806analyzing log files, 22-23ante-default route, 69-70

respond to method, 70APIs

integration test API, 585-586Migration, 146-148Prototype, 421

Class object, 424Object class, 425-426top-level functions, 422-424

RSelenese, 592partial test cases, 592-593

application consoleroute objects, 129-131routes

dumping, 128-129manual recognition and generation, 132-134named routes, executing, 134-135

application.rhtml layout template, 312-313arbitrary predicates, 599-601arguments

for named routes, 84-85hashes, syntax, 136

around filters, 48Array class

832 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 832

Page 3: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

ActiveSupport library, 724JavaScript extensions, 426-428to_xml method, 510-513

assertions, 553, 562-566assert_block, 563for functional tests, 572-576one-assertion guideline, 566-567

assert_block assertion, 563AssetTagHelper module, 348-352assigning instance variables to view specs, 622associated objects, validating presence of, 261AssociationProxy class, 249-250associations, 199

belongs_to, 207-208options, 209-215

class hierarchy, 199-201extensions, 247-248has_many, 215

options, 216-224proxy methods, 224-225

many-to-many relationships, 225has_and_belongs_to_many method, 225-

232through association, 233-241

objects, adding to collection, 203-207one-to-many relationships, 201-203one-to-one relationships, 241

has_one relationship, 241-246unsaved, 246

Astels, Dave, 566attribute-based finders, 170attributes

default values, 160-162serialized, 162

attributes method, 168authenticate method, acts as authenticated, 494AuthenticatedTestHelper module, 501-502auto-loading classes and modules, 8autocompleters, 466-467automatic class reloading, 15Autotest project, 624auto_discovery_link_tag method, 348auto_link method, 393

B

BackgounDRb, adding background processingto applications, 713-718

background processingwith BackgounDRb, 713-718with daemons, 719-722with DRb, 710-712with script/runner, 708-710

Bates, Ryan, 810BDD (Behavior-Driven Development), 611before save callback, Acts as Authenticated, 493before/after callbacks, 274behaviors, 598, 603-604

shared behaviors, 604-607belongs_to association, 207-208

options, 209-215benchmark method, 353BenchmarkHelper module, 353bidirectional relationships, 227-228Bilkovich, Wilson, 249BNL (Business Natural Languages), 303boot script, initializer.rb file, 6bootstrapping, 3

auto-loading classes and modules, 8initializer.rb file, 6

default load paths, 6-7mode override, 2Rails gem version, 2RubyGems, 5

bound parameters for routes, 60-61breadcrumbs help method, writing, 409-410Buck, Jamis, 108, 131, 808Builder API, 513-515building production stack, required compo-

nents, 655application tier, 656database tier, 656monitoring tools, 657web tier, 656

builtins, 8button_to method, 400

Index 833

26_5619_index.qxd 10/25/07 10:28 AM Page 833

Page 4: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

C

caching, 327Action Cache plugin, 337-338action caching, 328, 330cache logging, 337content expiration, 333-334

fragments, 334Sweeper class, 335-336

fragment caching, 330global fragments, 332-333named fragments, 331-332storage, 338-340

page caching, 328call() method (RJS), 457callback registration, 272-273

before/after callbacks, 274callbacks, 271

after_find, 278-279after_initialize, 278classes, 279-282halting execution, 274usage examples, 275-278

capify command, 685-686Capistrano, 681

capify command, 685-686configuring, 670database schema, loading, 696database.yml, storing, 693-695deploying, 687-688, 691

via :copy, 692deployment maching, setting up, 689

symbolic links, 690gateway setting, 705installing, 661, 684multiserver deployments, 702-703preinitialization, 698, 700recipes, managing Mongrel clusters, 701-702remote user accounts, 691requirements for using, 682-684SCM System, customizing, 692spawner script, 688transactions, 703-705variables, 696-698

CaptureHelper module, 354

check_box_tag method, 378class loader, 15-16Class object (Prototype), 424classes

abstract base model classes, 291-292adding to mocks folder, 548-549AssociationProxy, 249-250auto-loading code, 8callback classes, 279-282extending, 644external filter classes, 46

cleaning up sessions, 481collections

associated objects, adding, 203-207proxy collection objects, 202rendering, 325-327

color gem, 807columns

adding to has_and_belongs_to_many_jointables, 232

defining, 149-155commands

capify (Capistrano), 685-686discover, 631erb, 308install, 632list, 629-630piston lock, 639piston unlock, 639redirect to, 41remove, 633render, 33script/plugin, 629source, 630sources, 630unsource, 631

concat method, 393conditional callbacks, 493conditional output, 311conditional validation, 264

when to use, 265-266configuring

BackgrounDRb, 714Capistrano, 670Mongrel, init script, 672-673Mongrel Cluster, 662

834 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 834

Page 5: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

Monit, 667-669init script, 673-675

Nginx, 663-667init script, 670-672

SMTP servers, 543constants, JSON, 760content_tag_for method, 390controller-only resources, 113-115controllers, 28-31

filters, 43-44around filters, 48conditions, 50external filter classes, 46filter chain halting, 50filter chain ordering, 47-48filter chain skipping, 49halting the filter chain, 46inheritance, 44-46inline filter method, 47

functional testing, 570-571assertions, 572-576methods, 571

instance variables, 42-43namespaced controllers, 107post-backs, 112requests, redirecting, 39-42REST actions, 96-98

explicitly specifying, 105session class method, 471specs, 617-619streaming, 51

send data() method, 51-52send file() method, 52-55

templates, rendering, 33-36view templates, 31

convention over configuration, 32converting

numeric data to formatted strings,NumberHelper module, 383-385

vendor libraries to Piston, 638XML to Ruby hashes, 515-516

cookies, 481reading, 482writing, 482

CookieStore session storage, 476-478create actions, 123-124

Create method (ActiveResource), 522-523creating

mailer model, 532-533migrations, 143-144named routes, 81-83

route helper methods, 82via with options mapping method, 86-87

sortable lists, 465-466CRUD (Create Read Update Delete),

92, 168-172creating, 163deleting, 178reading, 164-168updating, 174-178

CSV fixtures, 555current user, Acts as Authenticated, 499-501current user id, storing in session hash, 470current_page? method, 401custom actions (REST), syntax, 112-113custom expectation matchers, 601-603custom parameters, receptors, 73-74custom routes, writing, 72custom SQL queries, 171-172custom validation, 268-269customizing

ActiveResource, 527-528environments, 18SCM System (Capistrano), 692to_xml method output, 505-506validation error behavior, 347-348

cycle method, 394

D

daemons, adding background processing toapplications, 719-722

database locking, 179optimistic locking, 180-182pessimistic locking, 182

database schemaActive Record framework, 140-142, 158

attributes, 160-162configuring, 196-197connections, 189-196

Index 835

26_5619_index.qxd 10/25/07 10:28 AM Page 835

Page 6: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

CRUD, 163-178database locking, 179-182finders, 183-189macro-style methods, 155-158Migration API, 146-148migrations, 142-145, 149-155naming conventions, 159query cache, 172-174

database.yml, storing, 693-695DateHelper module, 355-361Davis, Ryan, 624DebugHelper module, 361debug_view_helper plugin, 808deep nesting, 108-109default load paths (initializer.rb file), 6-7default log formatter, 764-765default routes (routes.rb file), 65-66

ante-default route, 69-70resond to method, 70

generating, 67-68id field, 66, 77-78modifying, 68

delay() method (RJS), 458Delete method (ActiveResource), 524-525DELETE requests, handling with REST, 98delimiters, 308, 310

blank lines, removing, 310deploying

Capistrano, 687-688, 691multiserver deployments, 702-703via :copy, 692

production environmentCapistrano installation procedures, 661Mongrel installation procedures, 659Monit installation procedures, 661MySQL installation procedures, 660Nginx installation procedures, 659predeployment concerns, 676-677Ruby installation procedures, 658RubyGems installation procedures, 658Subversion installation procedures, 660

deployment machine (Capistrano), setting up,689-690

destroy actions, 121-122destroy method, 179

development mode, 14automatic class reloading, 15caching, 328class loader, 15-16

development mode:fixtures, 557disabling sessions for robots, 472discover command, 631dispatcher, 29-31distance_of_time_in_words method, 360div_for method, 391dom_class method, 389dom_id method, 389draggable() method (RJS), 458dragging and dropping, 463-464DRb (Distributed Ruby)

adding background processing to applications,710-712

session storage, 475drop_receiving() method (RJS), 458DSL (Domain-Specific Language), 155dumping

routes, 128-129schema, 11

dynamic fixture content, 556-557

E

edge rails, 5, 805-806applications, freezing/unfreezing, 4

edit actions, 124-125edit/update operations (REST), 99-100email

file attachmentsreceiving, 543sending, 539-540

receiving, 541-543sending, 540SMTP servers, configuring, 543TMail methods, 542

empty routes, 71enabling sessions, 473end_form_tag method, 378enforcing uniqueness of join models, 262Enumerable object (prototype), 437-443

836 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 836

Page 7: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

environment.rb filelog-level, overriding, 10overriding, 9-11production mode, 2-4TZ environment variable, 12-13

environments, customizing, 18ERb (Embedded Ruby), 308

delimiters, 310erb command, 308errors, 266

conditional validation, 264when to use, 265-266

custom validation, 268-269Errors collection, manipulating, 267finding, 254skipping validation, 269-270validation methods

allow_nil option, 263message option, 263on option, 264RecordInvalid, 263validates_acceptance_of, 254validates_associated, 255-256validates_confirmation_of, 256validates_each, 256validates_exclusion_of, 257validates_existence_of, 257validates_format_of, 258-259validates_inclusion_of, 257validates_length_of, 259-260validates_numericality_of, 260validates_presence_of, 261validates_uniqueness_of, 261-262

error_messages_for method, 342-343error_message_on method, 342-343Event class (JavaScript), extensions, 428-430examples of callback usage, 275-278exception_logger plugin, 808exception_notification plugin, 808excerpt method, 395executing, named routes in application console,

134-135expectations, 599

custom expectation matchers, 601-603

expiring cached content, 333-334fragments, 334Sweeper class, 335-336

explicitly specifying RESTful controllers, 105extending classes, 644extensions to associations, 247-248external filter classes, 46externals, 635

F

faux accessors, 369-370Fielding, Roy T., 89Fields, Jay, 566file attachments

receiving, 543sending, 539-540

file_field_tag method, 378filters, 43-44

around filters, 48conditions, 50external filter classes, 46filter chain halting, 50filter chain ordering, 47-48filter chain skipping, 49halting the filter chain, 46inheritance, 44-46inline filter method, 47

Find methods (ActiveResource), 519-521finders

attribute-based, 170conditions, 183-185options, 187-189parameters, 186results, ordering, 186

finding errors, 254FireBug, 421fixtures, 554

accessing from tests, 556CSV fixtures, 555disadvantages of, 560-561dynamic content, 556-557generating from development data, 558-559in development mode, 557options, 559

Index 837

26_5619_index.qxd 10/25/07 10:28 AM Page 837

Page 8: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

forcing plugin reinstallation, 633form method, 344-345form tags (HTML), generating, 378-381formatted named routes, 117FormHelper module, 362-370

faux accessors, 370FormOptionsHelper module, 371-377FormTagHelper module, 378-381form_tag method, 379Fowler, Martin, 139fragment caching, 330

global fragments, 332-333named fragments, 331-332storage, 338-340

frameworks, skipping, 9freezing/unfreezing applications, 4Function class (JavaScript), extensions, 430-431functional tests, 570-571, 576-580

assertions, 572-576equality parameter, 580-581methods, 571RJS behavior, testing, 581-582routing rules, testing, 582-584selection methods, 582

functions (prototype), 422-424

G

gateway capability (Capistrano), 705gems

color gem, 807Rails versions, 2

generatingfixtures from development data, 558-559HTML tags, 378-381

TagHelper module, 391-392routes, 132-134

generating default route, 67-68generators, 614globs

key value pairs, globbing, 80-81route globbing, 79-80

green bars, 553Grosenbach, Geoffrey, 806

H

h method, 319halting

callback execution, 274filter chain, 46

Hash class (prototype), 443hashes, syntax, 136has_and_belongs_to_many method, 225-228

columns, adding to join tables, 232custom SQL options, 229-231

has_finder plugin, 808has_many association, 215

options, 216-224proxy methods, 224-225

has_many_polymorphs plugin, 809has_one relationship, 241-244

options, 244-246HEAD element, yielding additional content,

315headers (ActiveResource), setting, 526Helmkamp, Bryan, 808helper methods, 311

breadcrumbs, writing, 409-410photo_for, writing, 408stubbing, 623tiles, writing, 410-416title helper, writing, 407-408

helper modulesActiveRecordHelper, 342-348AssetTagHelper, 348-352BenchmarkHelper, 353CaptureHelper, 354DateHelper, 355-361DebugHelper, 361FormHelper, 362-369

faux accessors, 370FormOptionsHelper, 371-377FormTagHelper, 378-381JavaScriptHelper, 381-383NumberHelper, 383, 385PaginationHelper, 386-388PrototypeHelper

link_to_remote method, 445-449observe_field method, 451-453

838 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 838

Page 9: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

observe_form method, 453periodically_call_remote method, 451remote_form_for method, 449-451

RecordIdentificationHelper, 388-389RecordTagHelper, 390-391TagHelper, 391-392TextHelper, 393-400UrlHelper, 400-406writing, 407

helper specs, 623hide() method (RJS), 458highlight method, 395Hodel, Eric, 624hook code, 641HTML

adding content to HEAD, 348form tags, generating, 378-381input tags, generating, 378-381messages, sending, 536-537select helper methods, 371-373select tags, creating for calendar data, 355-359TagHelper module, 391-392tags, generating, 391-392

HTTP requests, processing using REST, 98HTTP status codes, 37

for redirection, 41-42HTTPS, securing sessions, 473Hyett, PJ, 807

I

image_path method, 350image_tag method, 350implicit multipart messages, 539importing vendor libraries into Piston, 637in-place editors, 467index actions, 118-121inflections, 157-158Inflections class (ActiveSupport Library), 757-

759Inflector class, 511inheritance, 44-46

STI, 284-291mapping to the database, 286-287

init scripts, configuringMongrel, 672-673Monit, 673-675Nginx, 670-672

init.rb file, 640initializer.rb file, 6

default load paths, 6-7injection attacks, preventing, 319inline filter method, 47inline rendering, 35input method, 346input tags (HTML), generating, 378-381insert_html() method (RJS), 458install command, 632install.rb file, 645-647installing

Capistrano, 661, 684Mongrel, 659Mongrel Cluster, 659Monit, 661MySQL, 660Nginx, 659Piston, 636plugins, 629

Acts as Authenticated, 486-487Routing Navigator, 136

RSpec, 613Ruby, 658RubyGems, 658Selenium on Rails, 591Subversion, 660

instance variables, 42-43, 315-319assigning to view specs, 622

integration mode (RSpec on Rails), 619integration tests, 550-551, 584-586

sessions, 586invalid?() method, 267isolation mode (RSpec on Rails), 619

J-K

JavaScriptArray class extensions, 426-428Event class extensions, 428-430Function class extensions, 430-431JSON, 461-462

Index 839

26_5619_index.qxd 10/25/07 10:28 AM Page 839

Page 10: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

Number class extensions, 432-433RJS, 453-455

alert() method, 457call() method, 457delay() method, 458draggable() method, 458drop_receiving() method, 458hide() method, 458insert_html() method, 458literal() method, 459redirect_to() method, 459remove() method, 459replace() method, 460replace_html() method, 460select() method, 460show() method, 460sortable() method, 460templates, 455-456toggle() method, 461visual_effect() method, 461

String class extensions, 433-436JavaScriptHelper module, 381-383javascript_include_tag method, 351javascript_path method, 352join models, enforcing uniqueness, 262JSON (JavaScript Object Notation),

420, 461-462, 760class methods, 761-762constants, 760rendering, 36

Kernel classpublic instance methods, 762-764

key-value pairs, globbing, 80-81keywords, yield, 313Koziarski, Michael, 158

L

lambdas, 415layouts, application.rhtml layout template,

312-313legacy naming schemes, 159link_to method, 401-402link_to_remote method, 445-449

list command, 629-630listing available plugins, 629-630literal() method (RJS), 459load paths, 15

adding, 10class loader, 15-16

locators (Selenium), 590locking, 179

optimistic locking, 180-182pessimistic locking, 182

locking down plugin versions, 636locking Piston revisions, 639log files, 20-22

analyzing, 22-23log level, overriding, 10logging, 18

log files, 20-22analyzing, 22-23

severity levels, 19Syslog, 24

logging partials, 327login from cookie, Acts as Authenticated,

498-499

M

macro-style methods, 43, 155convention over configuration, 156pluralization, 157-158relationship declarations, 155-156

mailer methods, 533email

receiving, 541-543sending, 540

file attachments, sending, 539-540HTML messages, sending, 536-537multipart messages, sending, 537-539options, 534-536

mailer models, 531creating, 532-533

mail_to method, 404-405manipulating Errors collection, 267manual route recognition and generation,

132-134

840 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 840

Page 11: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

many-to-many relationships, 225has_and_belongs_to_many method, 225-228columns, adding to join tables, 232custom SQL options, 229-231

through association, 233-238options, 238-241

markdown method, 396Marklund, Peter, 798memcache session storage, 475-476messages

error messages, 266validating, 267

metaprogramming, 155methods

assertion methods, 562-565assert_block, 563

attributes, 168auto_discovery_link_tag method, 348auto_link, 393benchmark, 353button_to, 400check_box_tag, 378concat, 393content_tag_for, 390Create, 522-523current_page?, 401cycle, 394delete, 524-525destroy, 179distance_of_time_in_words, 360div_for, 391dom_class, 389dom_id, 389end_form_tag, 378error_messages_for, 342-343error_message_on, 342-343excerpt, 395file_field_tag, 378Find, 519-521for functional tests, 571form, 344-345form_tag, 379h, 319has_and_belongs_to_many method, 225-228helper methods, 311

writing, 407-416

highlight, 395image_path, 350image_tag, 350inline filter method, 47input, 346javascript_include_tag, 351javascript_path, 352lambdas, 415link_to, 401-402macro-style, 43, 155

convention over configuration, 156pluralization, 157-158relationship declarations, 155-156

mailer methods, 533email, receiving, 541-543email, sending, 540file attachments, sending, 539-540HTML messages, sending, 536-537multipart messages, sending, 537-539options, 534-536

mail_to, 404-405markdown, 396mock_model, 617partial_path, 389pluralize, 396public instance methods, 726reload, 169render

:content type option, 37:layout option, 37:status option, 37-39

reset_cycle, 397respond_to, 69-70

resource representations, 116route helper methods, 82sanitize, 397select helper methods, 371-373send data(), 51-52send file(), 52-55setup methods, 553simple_format, 398strip_links, 398strip_tags, 398stylesheet_link_tag, 352textilize, 399

Index 841

26_5619_index.qxd 10/25/07 10:28 AM Page 841

Page 12: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

textilize_without_paragraph, 399TMail, 542to_xml, 503-504

include parameter, 507-508methods parameter, 508-509output, customizing, 505-506overriding, 510procs parameter, 509-510

truncate, 399update, 524url_for, 76-77, 405-406validation errors

skipping validations, 269-270validation methods

:allow_nil option, 263:message option, 263:on option, 264conditional validation, 264-266custom validation, 268-269RecordInvalid, 263validates_acceptance_of, 254validates_associated, 255-256validates_confirmation_of, 256validates_each, 256validates_exclusion_of, 257validates_existence_of, 257validates_format_of, 258-259validates_inclusion_of, 257validates_length_of, 259-260validates_numericality_of, 260validates_presence_of, 261validates_uniqueness_of, 261-262

word_wrap, 400methods:invalid?(), 267methods:on(), 267Migration API, 146-148migrations, 142

caveats, 145columns, creating, 149-155creating, 143-144naming, 144

MIT-LICENSE files, 644Mocha library, 549

stubbing, 549-550mocking

mock objects, 607-608

partial mocking, 609-610mocks folder, adding classes, 548-549mock_model method, 617model classes, associations, 199

belongs_to, 207-215class hierarchy, 199-201extensions, 247-248has_many, 215-225many_to_many relationships, 225-232objects, adding to collection, 203-207one_to_many relationships, 201-203one_to_one relationships, 241-246model specs, 614-616

models, unit testing, 568-569modifying ActiveRecord classes at runtime,

301-304modifying default route, 68Module class, 765-769modules

AuthenticatedTestHelper, 501-502auto-loading code, 8

Mongrel, 652init script, configuring, 672-673installing, 659

Mongrel Clusterconfiguring, 662installing, 659

Monitconfiguring, 667, 669init script, configuring, 673-675installing, 661

multipart messagesimplicit multipart messages, 539sending, 537-539

multiserver deployments (Capistrano), 702-703MySQL, installing, 660

N

named routes, 81, 94-95arguments, 84-85creating, 81-87

route helper methods, 82executing in application console, 134-135

namespaced controllers, 107

842 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 842

Page 13: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

naming conventionsfor database schema, 159for observers, 283for migrations, 144for routes, 83-84

nested resources, 101-103, 106-108:name_prefix option, 103-105:path_prefix option, 103

deep nesting, 108-109new actions, 123-124new/create operations (REST), 99-100Nginx

configuring, 663-667init script, configuring, 670-672installing, 659

Nilclass, 772-773null objects, 608Number class (JavaScript), extensions, 432-433NumberHelper module, 383-385Numeric class (ActiveSupport library), 773-776

O

Object class (ActiveSupport library), 776-779Object class (Prototype), 425-426objects

adding to collection, 203-207errors, finding, 254storing in session hash, 470

observers, 11, 282naming conventions, 283registration, 283

observe_field method, 451-453observe_form method, 453Olson, Rick, 136, 486on() method, 267one-to-many relationships, 201-203one-to-one relationships, 241

has_one relationship, 241-244options, 244-246

optimistic locking, 180-182options

for belongs_to associations, 209-215for has_many associations, 216-224

for has_one relationships, 244-246for mailer methods, 534-536for through associations, 238-241

ORM frameworks, ActiveRecordabstract base model classes, 291-292before/after callbacks, 274callbacks, 271, 275-282

halting execution, 274registration, 272-273

classes, modifying at runtime, 301-304common behavior, reusing, 296-301observers, 282-283polymorphic has_many relationships, 292-296STI, 284-291

overridingenvironment.rb file, 9-11

TZ code, 12-13to_xml method, 510

P

page caching, 328PaginationHelper module, 386-388parameters, 60-61

receptors, 73-74wildcard parameters for routes, 61-62

parsing XML, 515-518XmlSimple library, 516-517

partial mocking, 609-610partial stubbing, 609-610partial templates

inline rendering, 35rendering, 35

partial test cases, 592-593partials, 320-322

collections, rendering, 325-327logging, 327passing variables to, 324-325reusing, 322shared, 323-324

partial_path method, 389passing variables to partials, 324-325patterns (Selenium), 590PeepCode screencast, 810

Index 843

26_5619_index.qxd 10/25/07 10:28 AM Page 843

Page 14: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

performance of applications, analyzing, 22periodically_call_remote method, 451pessimistic locking, 182photo_for method, writing, 408Piston

installing, 636properties, 639revisions, locking and unlocking, 639vendor libraries,

converting, 638importing, 637updating, 638

piston lock command, 639plugins, 628

Action Cache plugin, 337-338active_record_defaults, 807Acts as Authenticated

AccountController class, 496-498authenticate method, 494before save callback, 493current user, 499-501installing, 486-487login from cookie, 498-499remember token, 495User model, 487-492validation rules, 492

checking out via Subversion, 634debug_view_helper, 808exception_logger, 808exception_notification, 808has_finder, 808has_many_polymorphs, 809installing, 629-632listing, 629-630locking down, 636Piston, properties, 639query_trace, 809Rake tasks, 647

Rakefiles, 648-649reinstalling, 633removing, 633Routing Navigator, 137

installing, 136RSpec on Rails, 613

generators, 614model specs, 614-616

session timeout, 478-479sources, adding, 630sources, removing, 631Subversion externals, 635testing, 649updating, 634will_paginate, 386writing, 640, 644-647

Rake tasks, 647Rakefiles, 649

plural REST routes, 98-99pluralization, 157-158pluralize method, 396polymorphic associations, 293-296polymorphic has_many relationships, 292-296post-backs, 112predicates, 599-601preinitializing Capistrano, 698-700prerequisites for production environment,

652-654Proc class (ActiveSupport library), 781-782processing order of routes, 75-76production environment, 652

caching, 676components of, 655

application tier, 656database tier, 656monitoring tools, 657web tier, 656

performance issues, 676-677predeployment concerns, 676-677prerequisites, 652-654redundancy, 676scalability issues, 676-677

production mode, 2-4, 17Prototype, 420-421

Ajax object, 436Class object, 424Enumerable object, 437-443Firebug, 421Hash class, 443Object class, 425-426Prototype object, 445Responders object, 437top-level functions, 422-424

844 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 844

Page 15: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

Prototype object, 445PrototypeHelper module

observe_field method, 451-453observe_form method, 453

link_to_remote method, 445-449periodically_call_remote method, 451remote_form_for method, 449-451

proxy collection objects, 202proxy methods for has_many associations,

224-225PStore files, 474public instance methods, 726PUT requests, handling with REST, 98

Q-R

query cache, 172-174query_trace plugin, 809

Railcasts, 810Rake tasks, 647

freezing/unfreezing, 4Rakefiles, 648-649

rake tests, 587for SVN, 811-812

Rakefiles, 648-649Range class (ActiveSupport library), 782RCov, 624reading cookies, 482README files, 644receiving email, 541-543

file attachments, 543TMail methods, 542

receptors, 61-62, 73-74recipes (Capistrano), managing Mongrel clus-

ters, 701-702RecordIdentificationHelper module, 388-389RecordInvalid method, 263RecordTagHelper module, 390-391red bars, 553redirect to command, 41redirecting

HTTP status codes, 41-42 requests, 39-42

redirect_to() method (RJS), 459

Redpath, Luke, 478registration of observers, 283regular expressions in routes, 76reinstalling plugins, 633reload method, 169remember token, acts as authenticated, 495remote user accounts (Capistrano), 691remote_form_for method, 449-451remove command, 633remove() method (RJS), 459removing

blank lines from output, 310plugin sources, 631plugins, 633

render command, 33render method. See also rendering

:content type option, 37:layout option, 37:status option, 37-39

renderingcollections, 325-327templates, 33-34

inline rendering, 35partial templates, 35structured data, 36text rendering, 35

view templates, 31replace() method (RJS), 460replace_html() method (RJS), 460representations of resources, 115request handling, 28requests

dispatcher, 29-31redirecting, 39-42

requirements for using Capistrano, 682-684reset_cycle method, 397resources (REST), 93

controller-only, 113-115nested, 101-108

deep nesting, 108-109name_prefix option, 103-105path_prefix option, 103

representations of, 115respond to method, 69-70Responders object (Prototype), 437

Index 845

26_5619_index.qxd 10/25/07 10:28 AM Page 845

Page 16: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

respond_to method, resource representations,116

REST, 91controller actions, 96-98controllers

explicitly specifying, 105post-backs, 112

create action, 123-124custom actions, syntax, 112-113destroy action, 121-122edit action, 124-125edit/update operations, 99-100formatted named routes, 117HTTP requests, 96-98index action, 118-121named routes, 95new action, 123-124new/create operations, 99-100plural routes, 98-99resources, 93

controller-only, 113-115nested, 101-109representations of, 115

route customizationsextra collection routes, 111extra member routes, 110-111

show action, 121singular resource routes, 100-101singular routes, 98-99syntactic vinegar, 113update action, 124-125

REST (Representational State Transfer), 57, 89reusing

common behavior, 296-301code, 628partials, 322

REXML, 515RJS (Ruby JavaScript), 453-456

alert() method, 457call() method, 457delay() method, 458draggable() method, 458drop_receiving() method, 458hide() method, 458insert_html() method, 458

literal() method, 459redirect_to() method, 459remove() method, 459replace() method, 460replace_html() method, 460select() method, 460show() method, 460sortable() method, 460templates, 455-456toggle() method, 461visual_effect() method, 461

RJS behavior, testing, 581-582robots, disabling sessions, 472route customizations (REST)

extra collection routes, 111extra member routes, 110-111

route globbing, 79-80globbing key-value pairs, 80-81

route objects, 129-131routes

bound parameters, 59-61custom routes, writing, 72default routes, :id value, 77-78dumping, 128-129empty routes, 71literal URLs, 79manual recognition and generation in applica-

tion console, 132-134named routes, 81

arguments, 84-85creating, 81-83creating via with options mapping method,

86-87executing in application console, 134-135

naming, 83-84processing order, 75-76regular expressions, 76routes.rb file, 63-64

ante-default route, 69-70default route, 65-68

static strings, 62-63, 72-73syntax, 59testing, 135url for method, 76-77wildcard parameters, 61-62

846 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 846

Page 17: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

routes.rb file, 63-64ante-default route, 69-70

respond to method, 70custom routes, writing, 72default route, 65-66

:id field, 66generating, 67-68modifying, 68

empty routes, 71routing, 59

receptors, 73-74Routing Navigator plugin, 137

installing, 136routing rules, testing, 582-584RSelenese, 592

partial test cases, 592-593RSpec, 546

arbitrary predicates, 599-601Autotest project, 624behaviors, 603-604

shared behaviors, 604-607controller specs, 617-619expectations, custom expectation matchers,

601-603installing, 613mock objects, 607-608null objects, 608partial mocking, 609-610partial stubbing, 609-610RCov, 624running specs, 610scaffolding, 623specdoc format, 611-612specs, helper specs, 623stub objects, 608view specs, 621

helper methods, stubbing, 623instance variables, assigning, 622

RSpec on Rails, 613errors, specifying, 620generators, 614model specs, 614-616integration mode, 619isolation mode, 619routes, specifying, 620

RSpec scripts, 597expectations, 599

Ruby, installing, 658

RubyGems, 5installing, 658

running edge rails, 4running specs, 610

S

sanitize method, 397scaffolding, 623schema dumping, 11SCM System (Capistrano), customizing, 692scoping Capistrano variables, 696-698screencasts, 810script/plugin command, 629script/runner, adding background processing to

applications, 708-710Scriptaculous

dragging and dropping, 463-464in-place editors, 467sortable lists, creating, 465-466

scriptsRSpec, 597

expectation matchers, 599spawner script, 688SpiderTester, 809

securing sessions, 480select helper methods, 371-373select tags (HTML), creating for calendar data,

355-359select() method (RJS), 460Selenium, 589

actions, 589assertions, 590locators, 590patterns, 590RSelenese, partial test cases, 592-593

Selenium on Rails, 591self-referential relationships, 226send data() method, 51-52send file() method, 52-55sending

email, 540file attachments, 539-540HTML messages, 536-537multipart messages, 537-539

serialized attributes, 162

Index 847

26_5619_index.qxd 10/25/07 10:28 AM Page 847

Page 18: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

session hashcurrent user id, storing, 470objects, storing, 470

session id, 469sessions, 469, 586

cleaning up, 481enabling, 473for robots, disabling, 472options, 471securing, 480securing with HTTPS, 473storing

DRb session storage, 475in ActiveRecord, 473-474in CookieStore, 476-478in memcache, 475-476in PStore files, 474

timing out, 478-479tracking, 479-480

setting up ActionMailer framework, 531setup methods, 553shared behaviors, 604-607shared partials, 323-324shared secrets, 495Shaw, Zed, 652show actions, 121show() method (RJS), 460simple_format method, 398single responsibility principle, 282singular resource routes, 100-101singular REST routes, 98-99skipping

error validation, 269-270frameworks, 9

SMTP server, configuring, 543sortable lists, creating, 465-466sortable() method (RJS), 460sources command, 630spec method, specdoc format, 611-612specdoc format (spec method), 611-612specs, 597

controller specs, 617-619expectations, 599helper specs, 623model specs, 614-616running specs, 610

view specs, 621helper methods, stubbing, 623instance variables, assigning, 622

SpiderTester script, 809SQL

custom options, 229-231custom queries, 171-172

static strings, 62-63, 72-73Stephenson, Sam, 420STI (single-table inheritance), 284-291

inheritance, mapping to the database, 286-287storing

database.yml, 693-695sessions

ActiveRecord, 473-474CookieStore, 476-478current user id, 470DRb session storage, 475memcache, 475-476objects, 470PStore files, 474

streaming, 51send data() method, 51-52send file() method, 52-55

String class (ActiveSupport library), 783-790String class (JavaScript), extensions, 433-436strings, typecasting, 518strip_links method, 398strip_tags method, 398structured date, rendering, 36stub objects, 608

partial stubbing, 609-610stubbing, 549-550stylesheet_link_tag method, 352stylesheet_path method, 352Subversion, 810-811

externals, 635installing, 660plugins, updating, 634rake tests, 811-812website, 633

suffixes for templates, 312Susser, Josh, 112, 233, 369SVN (Subversion). See SubversionSweeper class, 335-336Symbol class (ActiveSupport library), 790

848 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 848

Page 19: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

syntactic sugar, 113syntactic vinegar, 113syntax

for custom REST actions, 112-113for hashes in arguments, 136

Syslog, 24

T

TagHelper module, 391-392templates

application.rhtml layout template, 312-313delimiters, 308-310partials, 320-322

collections, rendering, 325-327logging, 327passing variables to, 324-325reusing, 322shared partials, 323-324

rendering, 33-35RJS, 455-456structured data, rendering, 36suffixes, 312text, rendering, 35variables, 315

instance variables, 315-319test mode, 17test suites, 553testing, 553

acceptance tests, 588-589assertions, 562-566

assert, 563one-assertion guideline, 566-567

errors, 553failures, 553fixtures, 554-556

CSV fixtures, 555disadvantages of, 560-561dynamic content, 556-557generating from development data, 558-559in development mode, 557options, 559

functional tests, 570-571, 576-580assertions, 572-576equality parameter, 580-581

methods, 571routing rules, testing, 582-584selection methods, 582testing RJS behavior, 581-582

integration tests, 550-551, 584-586plugins, 649Selenium, 589

actions, 589assertions, 590locators, 590patterns, 590

routes, 135unit tests, 568-569with Rake, 587XUnit framework, 552

text, rendering, 35TextHelper module, 393-400textilize method, 399textilize_without_paragraph method, 399tiles help method, writing, 410-416time and date information, creating HTML

select tags, 355-359Time class (ActiveSupport library), 792-798time zones, 798-802timing out sessions, 478-479TMail, 540

file attachments, receiving, 543methods, 542

toggle() method (RJS), 461to_xml method, 503-504

Array class, 510-513:include parameter, 507-508:methods parameter, 508-509:procs parameter, 509-510output, customizing, 505-506overriding, 510

tracking sessions, 479-480transactional fixtures, 559transactions (Capistrano), 703-705truncate method, 399typecasting strings, 518TZ (time zone) code, overriding in environ-

ment.rb file, 12-13

Index 849

26_5619_index.qxd 10/25/07 10:28 AM Page 849

Page 20: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

U

unfreezing edge rails applications, 4uninstall.rb file, 645-647uniqueness of join models, enforcing, 262unit tests, 568-569unlocking Piston revisions, 639unsaved associations, 246unsource command, 631update actions, 124-125Update method (ActiveResource), 524updating plugins, 634url for method, 76-77UrlHelper module, 400-406URLs, generating, 133-134url_for method, 405-406User model, Acts as Authenticated, 487-492user-submitted content, preventing injection

attacks, 319

V

validates_acceptance_of method, 254validates_associated method, 255-256validates_confirmation_of method, 256validates_each method, 256validates_exclusion_of method, 257validates_existence-of method, 257validates_format_of method, 258-259validates_inclusion_of method, 257validates_length_of method, 259-260validates_numericality_of method, 260validates_presence_of method, 261validates_uniqueness_of method, 261-262validation errors

reporting, 342-343skipping validation, 269-270

validation methodsallow_nil option, 263conditional validation, 264

when to use, 265-266custom validation, 268-269

message option, 263on option, 264

validation rules, Acts as Authenticated, 492variables, 315

instance variables, 315-319passing to partials, 324-325

variables (Capistrano), 696-698vendor libraries

converting to Piston, 638importing into Piston, 637

view specs, 621helper methods, stubbing, 623instance variables, assigning, 622

view templates, rendering, 31viewing generated routes, 132views

functional testing, 576-580equality parameter, 580-581routing rules, testing, 582-584selection methods, 582

instance variables, 42-43visual_effect() method (RJS), 461

W

Wanstrath, Chris, 807websites, Subversion project, 633whiny nil, 772wildcard parameters for routes, 61-62Williams, Bruce, 387will_paginate plugin, 386word_wrap method, 400writing

cookies, 482custom routes, 72helper methods, 407-408

breadcrumbs, 409-410photo_for, 408tiles, 410-416

helper modules, 407writing plugins, 640, 644-647

Rake tasks, 647Rakefiles, 649

850 Index

26_5619_index.qxd 10/25/07 10:28 AM Page 850

Page 21: 26 5619 index.qxd 10/25/07 10:28 AM Page 831 › images › 9780321445612 › ... · 2009-06-09 · deployment machine (Capistrano), setting up, 689-690 destroy actions, 121-122 destroy

X-Y-Z

XMLBuilder API, 513-515parsing, 516-518parsing into Ruby hashes, 515-517rendering, 36to_xml method, 503-504

include parameter, 507-508methods parameter, 508-509output, customizing, 505-506procs parameter, 509-510

XmlSimple library, 516-517XUnit framework, 552

YAML (Yet Another Markup Language), 554yield keyword, 313yielding additional HEAD element content, 315

Index 851

26_5619_index.qxd 10/25/07 10:28 AM Page 851