Agile Web Development With Rails 6
Agile Web Development With Rails 6
This PDF file contains pages extracted from Agile Web Development with Rails 6,
published by the Pragmatic Bookshelf. For more information or to purchase a
paperback or PDF copy, please visit http://www.pragprog.com.
Note: This extract contains some colored text (particularly in code listing). This
is available only in online versions of the books. The printed versions are black
and white. Pagination might vary between the online and printed versions; the
content is otherwise identical.
Copyright © 2019 The Pragmatic Programmers, LLC.
Sam Ruby
David Bryant Copeland
with Dave Thomas
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,
or otherwise, without the prior consent of the publisher.
ISBN-13: 978-1-68050-670-9
Book version: B1.0—April 24, 2019
In this chapter, you'll see:
• The directory structure of a Rails application
• Naming conventions
• Adding Rake tasks
• Configuration
CHAPTER 18
The chapters in Part III cover all the major subsystems of Rails: Active Record,
Active Resource, Action Pack (including both Action Controller and Action
View), and Active Support. This is followed by an in-depth look at migrations.
Then we are going to delve into the interior of Rails and show how the compo-
nents are put together, how they start up, and how they can be replaced.
Having shown how the parts of Rails can be put together, we’ll complete this
book with a survey of a number of popular replacement parts, many of which
can be used outside of Rails.
We need to set the scene. This chapter covers all the high-level stuff you need
to know to understand the rest: directory structures, configuration, and
environments.
Where Things Go
Rails assumes a certain runtime directory layout and provides application
and scaffold generators, which will create this layout for you. For example, if
we generate my_app using the command rails new my_app, the top-level directory
for our new application appears as shown in the figure on page 4.
Joe asks:
So, Where’s Rails?
One of the interesting aspects of Rails is how componentized it is. From a developer’s
perspective, you spend all your time dealing with high-level modules such as Active
Record and Action View. There is a component called Rails, but it sits below the
other components, silently orchestrating what they do and making them all work
together seamlessly. Without the Rails component, not much would happen. But at
the same time, only a small part of this underlying infrastructure is relevant to
developers in their day-to-day work. We’ll cover the parts that are relevant in the rest
of this chapter.
Let’s start with the text files in the top of the application directory:
1. http://guides.rubyonrails.org/rails_on_rack.html
Technically, this file isn’t used by Rails but rather by your application. You
can find calls to the Bundler2 in the config/application.rb and config/boot.rb files.
• Gemfile.lock records the specific versions for each of your Rails application’s
dependencies. This file is maintained by Bundler and should be checked
into your repository.
• Rakefile defines tasks to run tests, create documentation, extract the current
structure of your schema, and more. Type rake -T at a prompt for the full
list. Type rake -D task to see a more complete description of a specific task.
Let’s look at what goes into each directory (although not necessarily in order).
2. https://github.com/bundler/bundler
The lib directory is also a good place to put code that’s shared among models,
views, or controllers. Maybe you need a library that validates a credit card
number’s checksum, that performs some financial calculation, or that works
out the date of Easter. Anything that isn’t directly a model, view, or controller
should be slotted into lib.
Don’t feel that you have to stick a bunch of files directly into the lib directory.
Feel free to create subdirectories in which you group related functionality
under lib. For example, on the Pragmatic Programmer site, the code that gen-
erates receipts, customs documentation for shipping, and other PDF-formatted
documentation is in the directory lib/pdf_stuff.
In previous versions of Rails, the files in the lib directory were automatically
included in the load path used to resolve require statements. This is now an
option that you need to explicitly enable. To do so, place the following in
config/application.rb:
config.autoload_paths += %W(#{Rails.root}/lib)
Once you have files in the lib directory and the lib added to your autoload
paths, you can use them in the rest of your application. If the files contain
classes or modules and the files are named using the lowercase form of the
class or module name, then Rails will load the file automatically. For example,
we might have a PDF receipt writer in the file receipt.rb in the directory lib/pdf_stuff.
As long as our class is named PdfStuff::Receipt, Rails will be able to find and load
it automatically.
For those times where a library cannot meet these automatic loading condi-
tions, you can use Ruby’s require mechanism. If the file is in the lib directory,
you can require it directly by name. For example, if our Easter calculation
library is in the file lib/easter.rb, we can include it in any model, view, or con-
troller using this:
require "easter"
Rails provides a Rake task to tell you the latest migration that has been per-
formed. But it may be helpful to see a list of all the migrations that have been
performed. We’ll write a Rake task that prints the versions listed in the
schema_migration table. These tasks are Ruby code, but they need to be placed
into files with the extension .rake. We’ll call ours db_schema_migrations.rake:
rails6/depot_u/lib/tasks/db_schema_migrations.rake
namespace :db do
desc "Prints the migrated versions"
task :schema_migrations => :environment do
puts ActiveRecord::Base.connection.select_values(
'select version from schema_migrations order by version' )
end
end
We can run this from the command line just like any other Rake task:
depot> bin/rails db:schema_migrations
(in /Users/rubys/Work/...)
20190425000001
20190425000002
20190425000003
20190425000004
20190425000005
20190425000006
20190425000007