Skip to content

Commit

Permalink
Merge branch 'drnic'
Browse files Browse the repository at this point in the history
  • Loading branch information
tslocke committed Apr 29, 2008
2 parents 01bdd69 + dd44c58 commit 4d89527
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 4 deletions.
15 changes: 12 additions & 3 deletions hobofields/Manifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
CHANGES.txt
generators/hobo_migration/hobo_migration_generator.rb
generators/hobo_migration/templates/migration.rb
init.rb
lib/hobo_fields/email_address.rb
lib/hobo_fields/enum_string.rb
Expand All @@ -17,9 +15,20 @@ lib/hobo_fields/textile_string.rb
lib/hobo_fields.rb
lib/hobofields.rb
LICENSE.txt
Manifest
rails_generators/hobo_migration/hobo_migration_generator.rb
rails_generators/hobo_migration/templates/migration.rb
rails_generators/hobofield_model/hobofield_model_generator.rb
rails_generators/hobofield_model/templates/fixtures.yml.erb
rails_generators/hobofield_model/templates/model.rb.erb
rails_generators/hobofield_model/templates/test.rb.erb
rails_generators/hobofield_model/USAGE
README.txt
script/destroy
script/generate
test/hobofields.rdoctest
test/hobofields_api.rdoctest
test/migration_generator.rdoctest
test/rich_types.rdoctest
Manifest
test/test_generator_helper.rb
test/test_hobofield_model_generator.rb
4 changes: 3 additions & 1 deletion hobofields/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Echoe.new('hobofields') do |p|
p.summary = "Rich field types and migration generator for Rails"
p.url = "http://hobocentral.net/hobofields"
p.project = "hobo"

p.changelog = "CHANGES.txt"
p.version = "0.7.5"

p.dependencies = ['hobosupport >=0.7.5']
end

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require File.dirname(__FILE__) + '/../../lib/hobofields'
class HoboMigrationGenerator < Rails::Generator::Base

def initialize(runtime_args, runtime_options = {})
Expand Down
29 changes: 29 additions & 0 deletions hobofields/rails_generators/hobofield_model/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Description:
Stubs out a new model. Pass the model name, either CamelCased or
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'.

You don't have to think up every attribute up front, but it helps to
sketch out a few so you can start working with the model immediately.

This generates a model class in app/models, a unit test in test/unit,
a test fixture in test/fixtures/singular_name.yml, and a migration in
db/migrate.

Examples:
`./script/generate hobofield_model account`

creates an Account model, test, fixture, and migration:
Model: app/models/account.rb
Test: test/unit/account_test.rb
Fixtures: test/fixtures/accounts.yml

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

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

After the model is created, and the fields are specified, use hobofield
to create the migrations incrementally.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class HobofieldModelGenerator < Rails::Generator::NamedBase
default_options :skip_timestamps => false, :skip_fixture => false

def manifest
record do |m|
# Check for class naming collisions.
m.class_collisions class_path, class_name, "#{class_name}Test"

# Model, test, and fixture directories.
m.directory File.join('app/models', class_path)
m.directory File.join('test/unit', class_path)
m.directory File.join('test/fixtures', class_path)

# Create stubs
m.template "model.rb.erb", "app/models/#{file_name}.rb"
m.template "test.rb.erb", "test/unit/#{file_name}_test.rb"

unless options[:skip_fixture]
m.template 'fixtures.yml.erb', File.join('test/fixtures', "#{file_name}.yml")
end

end
end

protected
def banner
"Usage: #{$0} #{spec.name} ModelName [field:type, field:type]"
end

def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--skip-timestamps",
"Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
opt.on("--skip-fixture",
"Don't generation a fixture file for this model") { |v| options[:skip_fixture] = v}
end
end
Original file line number Diff line number Diff line change
@@ -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 -%>
10 changes: 10 additions & 0 deletions hobofields/rails_generators/hobofield_model/templates/model.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class <%= class_name %> < ActiveRecord::Base
fields do
<% for attribute in attributes -%>
<%= attribute.name %> :<%= attribute.type %>
<% end -%>
<% unless options[:skip_timestamps] %>
timestamps
<% end -%>
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class <%= class_name %>Test < ActiveSupport::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end
14 changes: 14 additions & 0 deletions hobofields/script/destroy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

begin
require 'rubigen'
rescue LoadError
require 'rubygems'
require 'rubigen'
end
require 'rubigen/scripts/destroy'

ARGV.shift if ['--help', '-h'].include?(ARGV[0])
RubiGen::Base.use_component_sources! [:rubygems, :test_unit]
RubiGen::Scripts::Destroy.new.run(ARGV)
14 changes: 14 additions & 0 deletions hobofields/script/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

begin
require 'rubigen'
rescue LoadError
require 'rubygems'
require 'rubigen'
end
require 'rubigen/scripts/generate'

ARGV.shift if ['--help', '-h'].include?(ARGV[0])
RubiGen::Base.use_component_sources! [:rubygems, :test_unit]
RubiGen::Scripts::Generate.new.run(ARGV)
29 changes: 29 additions & 0 deletions hobofields/test/test_generator_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
begin
require File.dirname(__FILE__) + '/test_helper'
rescue LoadError
require 'test/unit'
end
require 'fileutils'

# Must set before requiring generator libs.
TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
PROJECT_NAME = "myproject" unless defined?(PROJECT_NAME)
app_root = File.join(TMP_ROOT, PROJECT_NAME)
if defined?(APP_ROOT)
APP_ROOT.replace(app_root)
else
APP_ROOT = app_root
end
if defined?(RAILS_ROOT)
RAILS_ROOT.replace(app_root)
else
RAILS_ROOT = app_root
end

begin
require 'rubigen'
rescue LoadError
require 'rubygems'
require 'rubigen'
end
require 'rubigen/helpers/generator_test_helper'
65 changes: 65 additions & 0 deletions hobofields/test/test_hobofield_model_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require File.join(File.dirname(__FILE__), "test_generator_helper.rb")

require 'rails_generator'

class TestHobofieldModelGenerator < Test::Unit::TestCase
include RubiGen::GeneratorTestHelper

def setup
bare_setup
end

def teardown
bare_teardown
end

# Some generator-related assertions:
# assert_generated_file(name, &block) # block passed the file contents
# assert_directory_exists(name)
# assert_generated_class(name, &block)
# assert_generated_module(name, &block)
# assert_generated_test_for(name, &block)
# The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
# assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
#
# Other helper methods are:
# app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
# bare_setup - place this in setup method to create the APP_ROOT folder for each test
# bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test

def test_generator_underscore_name
name = "my_model"
run_generator('hobofield_model', [name], sources)
assert_generated_file("app/models/my_model.rb")
assert_generated_class("app/models/my_model") do |body|
end

assert_generated_file("test/unit/my_model_test.rb")
assert_generated_class("test/unit/my_model_test") do |body|
assert_has_method "test_truth"
end
end

def test_generator_camelcase
name = "MyModel"
run_generator('hobofield_model', [name], sources)
assert_generated_file("app/models/my_model.rb")
assert_generated_class("app/models/my_model") do |body|
end

assert_generated_file("test/unit/my_model_test.rb")
assert_generated_class("test/unit/my_model_test") do |body|
assert_has_method "test_truth"
end
end

private
def sources
[RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"..", generator_path))
]
end

def generator_path
"rails_generators"
end
end

0 comments on commit 4d89527

Please sign in to comment.