h1. 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. h2. Generated code may contain * "Cucumber":http://github.com/aslakhellesoy/cucumber * "Shoulda":http://github.com/thoughtbot/shoulda * "Factory Girl":http://github.com/thoughtbot/factory_girl * "Mocha":http://github.com/jferris/mocha * "Formtastic":http://github.com/justinfrench/formtastic h2. Installation $ script/plugin install git://github.com/dancroak/blitz.git h2. 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 h2. 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 h2. Controller generator $ script/generate controller Posts new create generates... h2. Model generator $ script/generate model User * factory (Factory Girl) * unit test (Shoulda) * migration * model h2. Helper generator $ script/generate helper Navigation * empty helper test file * empty helper module h2. View generator $ script/generate view Posts new generates a Formtastic view which reflects on your current schema...

New post

<% 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...

New post

<% semantic_form_for(@post) do |form| %> <% form.inputs %> <% form.buttons do %> <%= form.commit_button 'Create', :button_html => { :disable_with => 'Please wait...' } %> <% end %> <% end %>
h2. 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
h2. 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
h2. Attribution "Dan Croak":http://github.com/dancroak and "Mike Breen":http://github.com/hardbap Inspired by "shoulda_generator":http://github.com/technicalpickles/shoulda_generator, "model_generator_with_factories":http://github.com/vigetlabs/model_generator_with_factories, and "nifty_generators":http://github.com/ryanb/nifty-generators h2. License The MIT License Copyright (c) 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.