Name Last Update
..
features Loading commit data...
generators Loading commit data...
.gitignore Loading commit data...
CHANGELOG.textile Loading commit data...
README.textile Loading commit data...
Rakefile Loading commit data...

README.textile

Blitz generators

A Rails plugin for feature, view, controller, model, & helper generators. It is meant to be used as part of an “Outside-In” Test-Driven Development cycle.

Generated code may contain

Installation

$ script/plugin install git://github.com/dancroak/blitz.git

Feature generator

$ script/generate feature Posts new create

generates…

  Scenario: Create a new post
    Given I am on the new post page
    When I create a 'post' named 'A new post'
    Then I should see 'A new post'

Now run it:

$ cucumber features/posts.feature

The failure will be:

undefined method `new_post_path’

To get past this error, we’ll create just the route we need:

map.resources :posts, :only => [:new]

Running the test again, we have a new failure:

uninitialized constant PostsController

Controller test generator

We’ve reached a point in our Outside-In cycle where we want to spiral down into a tighter feedback loop to unit test the non-existant controller.

$ script/generate controller_test Posts new create

Controller generator

$ script/generate controller Posts new create

generates…

Model generator

$ script/generate model User
  • factory (Factory Girl)
  • unit test (Shoulda)
  • migration
  • model

Helper generator

$ script/generate helper Navigation
  • empty helper test file
  • empty helper module

View generator

$ script/generate view Posts new

generates a Formtastic view which reflects on your current schema…

<h1>New post</h1>

<% semantic_form_for(@post) do |form| %>
  <% form.inputs do %>
    <%= form.input :title %>
    <%= form.input :body %>
    <%= form.input :user %>
  <% end %>
  <% form.buttons do %>
    <%= form.commit_button 'Create',
          :button_html => { :disable_with => 'Please wait...' } %>
  <% end %>
<% end %>

If no ActiveRecord is defined named Post, it will generate…

<h1>New post</h1>

<% semantic_form_for(@post) do |form| %>
  <% form.inputs %>
  <% form.buttons do %>
    <%= form.commit_button 'Create',
          :button_html => { :disable_with => 'Please wait...' } %>
  <% end %>
<% end %>

Model generator: belongs_to

$ script/generate model post user:belongs_to

generates…

class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.belongs_to :user
      t.timestamps
    end

    add_index :posts, :user_id
  end

  def self.down
    remove_index :posts, :user_id
    drop_table :posts
  end
end

class Post < ActiveRecord::Base
  belongs_to :user
end

class PostTest < ActiveSupport::TestCase
  should_belong_to :user
  should_have_index :user_id
end

Factory.define :post do |post|
  post.association(:user)
end

Model generator: paperclip

$ script/generate model design image:paperclip
  • all the necessary columns in the migration
  • “has_attached_file” in the model
  • “should_have_attached_file” in unit test
class CreateDesigns < ActiveRecord::Migration
  def self.up
    create_table :designs do |t|
      t.string :image_file_name
      t.string :image_content_type
      t.integer :image_file_size
      t.datetime :image_updated_at
      t.timestamps
    end
  end

  def self.down
    drop_table :designs
  end
end

class Design < ActiveRecord::Base
  has_attached_file :image
end

class DesignTest < ActiveSupport::TestCase
  should "be valid with factory" do
    assert_valid Factory.build(:design)
  end
  should_have_attached_file :image
end

Attribution

Dan Croak and Mike Breen

Inspired by shoulda_generator, model_generator_with_factories, and nifty_generators

License

The MIT License

Copyright © 2008, 2009 Dan Croak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.