0
# A step-by-step project that demonstrates the use of this bundle.
0
-In this demo we'll create a blog; because that's what blogs are for: being
0
+In this demo we'll create a blog; because that's what blogs are for: being
0
demonstrations of web frameworks.
0
The demonstration uses new features of Rails 2.0 and the snippets in this bundle.
0
@@ -14,7 +14,7 @@ The demonstration uses new features of Rails 2.0 and the snippets in this bundle
0
ruby script/generate model Post subject:string body:text
0
This creates a 001_create_post migration with a create_table:
0
create_table :posts do |t|
0
@@ -23,10 +23,10 @@ This creates a 001_create_post migration with a create_table:
0
## Sexy Migration support
0
-If you put the cursor on the line after `t.text :body`, type `t.` and press TAB. Select "Create boolean column" (by pressing 0), and type "published" into the template field. If nothing happened when you pressed TAB, check that when you opened the migrations file you've selected the bundle "Ruby on Rails".
0
+If you put the cursor on the line after `t.text :body`, type `t.` and press TAB. Select "Create boolean column" (by pressing 0), and type "published" into the template field. If nothing happened when you pressed TAB, check that when you opened the migrations file you've selected the bundle "Ruby on Rails".
0
Note that another `t.` was created on the next line! Press TAB and the cursor will be placed after it. You can now press TAB again to create another column, or delete this line.
0
@@ -34,11 +34,11 @@ Here, delete the extraneous `t.` line (Shift-Ctrl-K).
0
Run the migrations, either from the prompt:
0
or directly from the editor by pressing ^-| (Ctrl-Pipeline), and choosing option "Migrate to Current".
0
Update the `test/fixtures/posts.yml` file as:
0
@@ -57,15 +57,15 @@ Note, in Rails 2.0 fixtures no longer have explicit ids. Later on we'll look at
0
Create a controller for our blog, either via the command prompt:
0
- ruby script/generate controller blog
0
+ ruby script/generate controller blog
0
or directly from the editor by pressing ^-|, and choosing option "Call Generate Script", choose "Controller", give it the name "blog", and empty the list of actions.
0
Now open `blog_controller_test.rb`. To find this file quickly press Cmd-T, enter bct, and select the file.
0
-Note how much cleaner functional tests are now via `ActionController::TestCase`.
0
+Note how much cleaner functional tests are now via `ActionController::TestCase`.
0
-Let's do some TDD. First, delete the `test_truth` dummy method.
0
+Let's do some TDD. First, delete the `test_truth` dummy method.
0
To create a test to show a list of blog articles:
0
@@ -91,15 +91,15 @@ Type `index` to replace `action`. Press TAB, and then DELETE to remove the first
0
Now type `asg`, press TAB, and type `posts`, and press TAB again. This creates an instance variable lookup within an assertion:
0
assert(posts = assigns(:posts), "Cannot find @posts")
0
Now, let's assert the HTML format.
0
Type `ass` and press TAB. Type `div#posts`, press TAB and DELETE, then TAB twice to place the cursor within the `assert_select` block:
0
assert_select 'div#posts' do
0
Now we'll check that the `@posts` objects are represented in the `div#posts` element.
0
With the cursor inside the `assert_select`:
0
@@ -139,8 +139,8 @@ Change the `index` action method to:
0
To create/navigate to the view, press Shift+Alt+Cmd+DownArrow and select "View" (like above).
0
As there is no `app/views/blog/index*` files, it will prompt you to create a blank view file. By default it guesses `index.html.erb`, but of course you can change that in the dialog box.
0
-If instead you got the message "blog_controller.rb does not have a view", note that you first need to save the file before hitting Shift-Alt-Cmd-DownArrow and selecting "View".
0
+If instead you got the message "blog_controller.rb does not have a view", note that you first need to save the file before hitting Shift-Alt-Cmd-DownArrow and selecting "View".
0
Press enter to accept `index.html.erb`. You are taken to the new file.
0
@@ -194,7 +194,7 @@ As yet, we have no way for users to leave comments.
0
Create a comment model:
0
ruby script/generate model Comment body:text name:string post:references
0
Note: here `post:references` is effectively the same as `post_id:integer`. Within the generated migration it creates `t.reference :post`. There is also a `t.` snippet for references, as for other standard datatypes, which helps setup polymorphic associations.
0
The generated `create_table` in `002_create_comments.rb` is:
0
@@ -206,7 +206,7 @@ The generated `create_table` in `002_create_comments.rb` is:
0
Now create some comment fixtures so we can look at Foxy Fixtures. Open `text/fixtures/comments.yml`.
0
@@ -216,13 +216,13 @@ By default, the generated `comments.yml` starts like:
0
The `post` fields replace the rails1.2 `post_id` fields. Now, we can specify the `post.yml` labels for a post. From above we have `published` and `unpublished`. It can be hard to remember what fixtures we have, so there is a key-combo helper.
0
Put the cursor after `post: ` and press Alt+Escape. A drop-down box appears with the names of the `posts.yml` fixtures. Select `published` and press return. Repeat for the 2nd fixture. This gives us:
0
@@ -238,7 +238,7 @@ Put the cursor after `post: ` and press Alt+Escape. A drop-down box appears with
0
To enable the Foxy Fixtures, we need to add associations to the model classes.
0
You can now quickly toggle from a fixtures file (we're in comments.yml now) to the
0
@@ -247,8 +247,8 @@ model file with Shift+Alt+Cmd+DownArrow hot key.
0
Within `comment.rb` model, create a new line within the class, and type `bt` and press TAB. Type `post`. This creates a snippet:
0
belongs_to :post, :class_name => "Post", :foreign_key => "post_id"
0
-The class name and foreign key are now generated from the association name. You can change them by TABbing across. But, we only need the default, so we can delete these options.
0
+The class name and foreign key are now generated from the association name. You can change them by TABbing across. But, we only need the default, so we can delete these options.
0
Press TAB and DELETE to remove the `:class_name` and `:foreign_key` options. The `Comment` class is now:
0
@@ -262,10 +262,10 @@ Create a new line within the `Post` class. Type `hm` and press TAB to generate a
0
has_many :comments, :class_name => "Comments", :foreign_key => "class_name_id"
0
-Notice the default `:class_name` is `Comments`, not `Comment`. This is wrong,
0
-and would need to be fixed, so its really just a helper to remind you to
0
-change it. We haven't figured out how to singularize a string using a regular
0
-expression; so if you know how let us know.
0
+Notice the default `:class_name` is `Comments`, not `Comment`. This is wrong,
0
+and would need to be fixed, so its really just a helper to remind you to
0
+change it. We haven't figured out how to singularize a string using a regular
0
+expression; so if you know how let us know.
0
But here, we don't need the options. So press TAB once and then DELETE.
0
@@ -273,7 +273,7 @@ But here, we don't need the options. So press TAB once and then DELETE.
0
-Note, there is now a `has_many :through` snippet. Type `hmt` and TAB to
0
+Note, there is now a `has_many :through` snippet. Type `hmt` and TAB to
0
Finally, we can run our tests since adding the `Comment` model + fixtures.
0
@@ -294,21 +294,21 @@ Change the routes file to:
0
-From the `Post` class (`post.rb`) you can now quickly navigate to a controller
0
-of the same name. It supports either singular or plural controller names, but
0
+From the `Post` class (`post.rb`) you can now quickly navigate to a controller
0
+of the same name. It supports either singular or plural controller names, but
0
will default to the plural name, which is the REST/resources preferred name.
0
To create a `PostsController`, use the 'Go To' hot key (as above) Shift+Alt+Cmd+
0
-DownArrow and select 'Controller'. As there is no `post_controller.rb` nor
0
-`posts_controller.rb` it will create a `posts_controller.rb` controller file;
0
+DownArrow and select 'Controller'. As there is no `post_controller.rb` nor
0
+`posts_controller.rb` it will create a `posts_controller.rb` controller file;
0
which is what we want here.
0
-Note; at this stage you could use the Rails 2.0 `scaffold` generator to create
0
+Note; at this stage you could use the Rails 2.0 `scaffold` generator to create
0
the `posts_controller.rb` (and tests and routes).
0
In the blank file, we need to create a controller class.
0
-Type `cla` and TAB, and select "Create controller class". Type `Posts` and TAB,
0
+Type `cla` and TAB, and select "Create controller class". Type `Posts` and TAB,
0
`post` and TAB, and finally, `Post` and TAB. This leaves the cursor in the middle
0
of the generated class:
0
@@ -327,25 +327,25 @@ of the generated class:
0
Currently there is not a functional test for our `posts_controller.rb`.
0
To create it, use the 'Go To' hot key (Shift+Alt+Cmd+
0
-DownArrow) and select 'Functional Test'. This will create
0
+DownArrow) and select 'Functional Test'. This will create
0
Type `cla` and TAB, and select "Create functional test class".
0
Type `Posts` and TAB. (The functional test class name
0
should match the controller class, with `Test` suffixed to it).
0
-The functional test class snippet gives you a `deft` stub. If you
0
+The functional test class snippet gives you a `deft` stub. If you
0
press TAB now, it creates a generic test method snippet:
0
Instead, we will use the `deftg` (GET request) and `deftp` (POST
0
-Create a test for the `index`, `new` and `edit` actions. For
0
-`index` and `new`, we can delete the `@model = models(:fixture_name)`,
0
+Create a test for the `index`, `new` and `edit` actions. For
0
+`index` and `new`, we can delete the `@model = models(:fixture_name)`,
0
To test for the `create` action, type `deftp` and TAB. Type
0
@@ -360,9 +360,9 @@ say `:subject => 'Test', :body => 'Some body', :published => '1'`
0
On the line after the `assert_response` expression, we'll test
0
-for where we want to be redirected to.
0
+for where we want to be redirected to.
0
-If you type `art` you create an old-style `assert_redirected_to :action => "index"`
0
+If you type `art` you create an old-style `assert_redirected_to :action => "index"`
0
In addition there are now various `assert_redirected_to` snippets that
0
@@ -400,36 +400,36 @@ with `def` snippet:
0
class PostsController < ApplicationController
0
before_filter :find_post
0
@posts = Post.find(:all)
0
@post = Post.find(params[:id]) if params[:id]
0
-Now we need templates for the `index`, `new` and `edit` actions.
0
+Now we need templates for the `index`, `new` and `edit` actions.
0
-Place the cursor inside the `index` method,
0
-and use the 'Go To' hot key (Shift+Alt+Cmd+DownArrow)
0
-and select 'View'. A dialog box will pop up asking for the name of the new
0
-template (as there are no `app/views/posts/index*` files). By default, the
0
+Place the cursor inside the `index` method,
0
+and use the 'Go To' hot key (Shift+Alt+Cmd+DownArrow)
0
+and select 'View'. A dialog box will pop up asking for the name of the new
0
+template (as there are no `app/views/posts/index*` files). By default, the
0
suffix is now `.html.erb` rather than the old `.rhtml`. Press RETURN,
0
to accept `index.html.erb` as your template name.
0
Let's just create a simple table showing the Posts.
0
-Type `table` and Ctrl+< to generate `<table></table>`, and
0
+Type `table` and Ctrl+< to generate `<table></table>`, and
0
press RETURN to put the tags on separate lines.
0
Do the same to create a `<tbody></tbody>` element.
0
@@ -464,11 +464,11 @@ The resulting `index.html.erb` is:
0
-Place the cursor inside the `new` method,
0
-and use the 'Go To' hot key (Shift+Alt+Cmd+DownArrow)
0
+Place the cursor inside the `new` method,
0
+and use the 'Go To' hot key (Shift+Alt+Cmd+DownArrow)
0
and select 'View'. Press RETURN to accept `new.html.erb`.
0
-Inside the blank `new.html.erb` file, type `ffe` and press TAB, and type `post`
0
+Inside the blank `new.html.erb` file, type `ffe` and press TAB, and type `post`
0
<%= error_messages_for :post %>
0
@@ -476,7 +476,7 @@ and press TAB, twice:
0
-`form_for` is the Rails 2.0 preferred helper for managing forms, and
0
+`form_for` is the Rails 2.0 preferred helper for managing forms, and
0
there are now snippets for common form_for helpers. There are `ff` and `ffe`
0
snippets; the former does not have the error messages section.
0
@@ -496,10 +496,10 @@ This gives us:
0
<%= f.text_field :subject %>
0
Now repeat for `body` and `published` fields.
0
-Note, for `published`, you might change the label to `Published yet?` by TABbing
0
+Note, for `published`, you might change the label to `Published yet?` by TABbing
0
into the default string file.
0
Finally, add a "Submit" button using the `f.` snippet tab completion.
0
@@ -526,7 +526,7 @@ The final form is:
0
<%= f.submit "Submit" %>
0
This form is exactly the same as the form required for the `edit.html.erb` template.
0
@@ -563,8 +563,8 @@ There are several `link_to` snippets that support the resources routes:
0
The tab stop points are in useful places.
0
-So, to create our link to the posts page, type `lipp` and TAB, type
0
-`Show all posts`, press TAB and type `post`.
0
+So, to create our link to the posts page, type `lipp` and TAB, type
0
+`Show all posts`, press TAB and type `post`.
0
## Controllers: `respond_to` and `redirect_to`
0
@@ -582,7 +582,7 @@ Within the class, go to the end, and type `def` and TAB, and type
0
Place the cursor in the `true` section of the `if` statement.
0
Type `repp` and TAB to create a `redirect_to` expression. Replace
0
the selected text with `post`.
0
@@ -600,15 +600,15 @@ There are tab stops in useful places.
0
We're going with `redirect_to(posts_path)` here, even though
0
we don't yet have an `index` action yet.
0
-In the `false` section of the `if` expression, we'll demonstrate the
0
+In the `false` section of the `if` expression, we'll demonstrate the
0
`respond_to` block. There are two ways to generate a `respond_to` block.
0
Type `rst` and TAB, and you get a standard empty block you can work with:
0
Inside the block, type `ra` and press TAB, then type `new`. The final block
0
Alternately, there is the "upgrade" hot key. Where you can convert some
0
existing selected code, into a `respond_to` block.
0
-Select the whole line containing the `redirect_to` expression from the
0
+Select the whole line containing the `redirect_to` expression from the
0
`true` section of the `if` statement.
0
Press Shift+Cmd+R (TODO - a better hot key?) and the line is replaced
0
@@ -629,8 +629,8 @@ with:
0
-The `js` is the first tab stop. The point of this hot key is to instantly
0
+The `js` is the first tab stop. The point of this hot key is to instantly
0
refactor your existing html respond code, and support a second response
0
@@ -652,7 +652,7 @@ The completed `create` action is:
0
Yes you'd probably only have one `respond_to` block, but this is a
0
demo so I am taking the scenic route.
0
@@ -660,7 +660,7 @@ demo so I am taking the scenic route.
0
In the browser, we can create posts via
0
[http://localhost:3000/posts/new](http://localhost:3000/posts/new)
0
-and then view them as a blog visitor at
0
+and then view them as a blog visitor at
0
[http://localhost:3000/blog](http://localhost:3000/blog).
0
@@ -686,6 +686,6 @@ and then view them as a blog visitor at
0
* Show text exploding across the screen
0
- * TextMate for Rails 2.0
0
+ * TextMate for Rails 2.0
0
* Coming Soon to PeepCode
0
* Coincides with "Lost" TV show's noise at the end of each show