Skip to content

Commit

Permalink
Updated factory_girl_on_rails.
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Sep 5, 2008
1 parent b79fd2f commit 4cb7812
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 3 deletions.
16 changes: 13 additions & 3 deletions vendor/plugins/factory_girl_on_rails/README
Expand Up @@ -4,18 +4,28 @@ factory_girl_on_rails
factory_girl_on_rails provides some minor creature comforts for using factory_girl on a Rails project:

* Automatically load factories in RAILS_ROOT/test/factories
* Coming soon: A generator for putting new factories in RAILS_ROOT/test/factories
* A generator for putting new factories in RAILS_ROOT/test/factories


Installing
==========

script/plugin install .....
script/plugin install git://github.com/technicalpickles/factory_girl_on_rails.git

Example
=======

Coming soon...
After installing this plugin, you just start using factories in RAILS_ROOT/test/factories.

As a convenience, a factory generator is provided:

`./script/generate factory account`

creates an Account factory: test/factories/account_factory.rb

`./script/generate factory post title:string body:text published:boolean`

creates a Post factory with a string title, text body, and published flag.


Copyright
Expand Down
@@ -0,0 +1,18 @@
Description:
Stubs out a new factory. Pass the factory name name, under_scored,
and an optional list of attribute pairs as arguments.

Attribute pairs are column_name:sql_type arguments specifying the
model's attributes. Timestamps are added by default, so you don't have to
specify them by hand as 'created_at:datetime updated_at:datetime'.

This generates a file in test/factories.

Examples:
`./script/generate factory account`

creates an Account factory: test/factories/account_factory.rb

`./script/generate factory post title:string body:text published:boolean`

creates a Post factory with a string title, text body, and published flag.
@@ -0,0 +1,17 @@
class FactoryGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
m.directory File.join('test/factories', class_path)
m.template 'factory.rb', File.join('test/factories', class_path, "#{file_name}_factory.rb")
end
end

def factory_line(attribute)
"#{file_name}.#{attribute.name} '#{attribute.default}'"
end

protected
def banner
"Usage: #{$0} #{spec.name} FactoryName [field:type, field:type]"
end
end
@@ -0,0 +1,5 @@
Factory.define :<%= file_name %> do |<%= file_name %>|
<% for attribute in attributes -%>
<%= factory_line(attribute) %>
<% end -%>
end
@@ -0,0 +1,19 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

<% unless attributes.empty? -%>
one:
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>

two:
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>
<% else -%>
# one:
# column: value
#
# two:
# column: value
<% end -%>
@@ -0,0 +1,16 @@
class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
<% for attribute in attributes -%>
t.<%= attribute.type %> :<%= attribute.name %>
<% end -%>
<% unless options[:skip_timestamps] %>
t.timestamps
<% end -%>
end
end
def self.down
drop_table :<%= table_name %>
end
end
@@ -0,0 +1,2 @@
class <%= class_name %> < ActiveRecord::Base
end
@@ -0,0 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'

class <%= class_name %>Test < ActiveSupport::TestCase
<% for attribute in attributes -%>
should_have_db_column :<%= attribute.name %>
<% end -%>
end

0 comments on commit 4cb7812

Please sign in to comment.