Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor Rails 3 generators to be namspaced in modules.

Stop supporting Rails 2 generators.
  • Loading branch information...
commit eca2ad08a6c394bb091326a00c7175452d2851ca 1 parent 84e9c4a
@midas authored
Showing with 270 additions and 265 deletions.
  1. +2 −0  .gitignore
  2. +1 −1  Guardfile
  3. +1 −0  genesis.gemspec
  4. +30 −0 lib/generators/genesis/contexts/contexts_generator.rb
  5. +0 −33 lib/generators/genesis/genesis_generator.rb
  6. +30 −0 lib/generators/genesis/install/install_generator.rb
  7. 0  {rails_generators/prepare_seeding → lib/generators/genesis/install}/templates/genesis.rake
  8. +3 −1 lib/generators/{prepare_genesis → genesis/install}/templates/genesis_callbacks.rb
  9. +41 −0 lib/generators/genesis/seed/seed_generator.rb
  10. +7 −5 lib/generators/genesis/{ → seed}/templates/migration.erb
  11. +0 −13 lib/generators/genesis_contexts/genesis_contexts_generator.rb
  12. +0 −15 lib/generators/prepare_genesis/prepare_genesis_generator.rb
  13. 0  lib/{generators/prepare_genesis/templates → tasks}/genesis.rake
  14. 0  rails_generators/genesis/USAGE
  15. +0 −50 rails_generators/genesis/genesis_generator.rb
  16. 0  rails_generators/genesis/templates/genesis_override.rake
  17. +0 −9 rails_generators/genesis/templates/migration.rb
  18. 0  rails_generators/genesis_contexts/USAGE
  19. +0 −44 rails_generators/genesis_contexts/genesis_contexts_generator.rb
  20. +0 −1  rails_generators/prepare_seeding/USAGE
  21. +0 −52 rails_generators/prepare_seeding/prepare_seeding_generator.rb
  22. +0 −13 rails_generators/prepare_seeding/templates/genesis_callbacks.rb
  23. +37 −0 spec/generators/genesis/contexts/contexts_generator_spec.rb
  24. +80 −0 spec/generators/genesis/install/install_generator_spec.rb
  25. +34 −0 spec/generators/genesis/seed/seed_generator_spec.rb
  26. +0 −26 spec/generators/genesis_generator_spec.rb
  27. +4 −2 spec/spec_helper.rb
View
2  .gitignore
@@ -3,3 +3,5 @@
.bundle
Gemfile.lock
pkg/*
+tmp/*
+db/*
View
2  Guardfile
@@ -1,5 +1,5 @@
guard 'rspec', :version => 2, :all_on_start => true, :all_after_pass => false, :cli => '--color --format doc' do
watch( %r{^spec/.+_spec\.rb$} )
- watch( %r{^lib/(.+)\.rb$} ) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch( %r{^lib/(.+)\.rb$} ) { |m| "spec/#{m[1]}_spec.rb" }
watch( 'spec/spec_helper.rb' ) { "spec" }
end
View
1  genesis.gemspec
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
growl
guard-rspec
rails
+ genspec
).each do |development_dependency|
s.add_development_dependency development_dependency
end
View
30 lib/generators/genesis/contexts/contexts_generator.rb
@@ -0,0 +1,30 @@
+require 'rails/generators'
+
+module Genesis
+
+ class ContextsGenerator < Rails::Generators::Base
+
+ desc "Description:\n Generate one or more context folders."
+
+ argument :contexts, :type => :string, :required => true
+
+ def self.source_root
+ File.join File.dirname(__FILE__),
+ 'templates'
+ end
+
+ def install_contexts
+ normalized_contexts.each do |context|
+ empty_directory File.join( Genesis::SEEDS_ROOT, 'contexts', context )
+ end
+ end
+
+ def normalized_contexts
+ return contexts if contexts.is_a?( Array )
+
+ contexts.split ','
+ end
+
+ end
+
+end
View
33 lib/generators/genesis/genesis_generator.rb
@@ -1,33 +0,0 @@
-require 'rails/generators'
-
-class GenesisGenerator < Rails::Generators::Base
- #source_root File.join( File.dirname(__FILE__), 'templates' )
- argument :seed_name, :type => :string
- argument :env, :type => :string, :default => ''
-
- def self.source_root
- File.join( File.dirname(__FILE__), 'templates' )
- end
-
- def install_seed
- template "migration.erb", "db/seeds/#{env_str}#{timestamp}_#{file_name}.rb"
- end
-
-private
-
- def file_name
- seed_name.underscore
- end
-
- def class_name
- seed_name.camelcase
- end
-
- def timestamp
- DateTime.now.strftime( "%Y%m%d%H%M%S" )
- end
-
- def env_str
- return env.blank? ? '' : "#{env}/"
- end
-end
View
30 lib/generators/genesis/install/install_generator.rb
@@ -0,0 +1,30 @@
+require 'rails/generators'
+
+module Genesis
+
+ class InstallGenerator < Rails::Generators::Base
+
+ desc "Description:\n Installs the genesis assets necessary to create and execute seeds."
+
+ argument :environments, :type => :string, :default => %w(development production)
+
+ def self.source_root
+ File.join File.dirname(__FILE__),
+ 'templates'
+ end
+
+ def install_seeding
+ envs.each { |env| empty_directory "db/seeds/#{env}" }
+ copy_file 'genesis.rake', 'lib/tasks/genesis.rake'
+ copy_file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
+ end
+
+ def envs
+ return environments if environments.is_a?( Array )
+
+ environments.split ','
+ end
+
+ end
+
+end
View
0  ...generators/prepare_seeding/templates/genesis.rake → ...generators/genesis/install/templates/genesis.rake
File renamed without changes
View
4 ...rs/prepare_genesis/templates/genesis_callbacks.rb → ...rs/genesis/install/templates/genesis_callbacks.rb
@@ -1,4 +1,5 @@
class GenesisCallbacks
+
def self.before_up
end
@@ -10,4 +11,5 @@ def self.before_down
def self.after_down
end
-end
+
+end
View
41 lib/generators/genesis/seed/seed_generator.rb
@@ -0,0 +1,41 @@
+require 'rails/generators'
+
+module Genesis
+
+ class SeedGenerator < Rails::Generators::Base
+
+ desc "Description:\n Creates the specified seed file (optionally within a specificied environment)."
+
+ argument :seed_name, :type => :string, :required => true
+ argument :environment, :type => :string, :default => ''
+
+ def self.source_root
+ File.join File.dirname(__FILE__),
+ 'templates'
+ end
+
+ def install_seed
+ template "migration.erb", "db/seeds/#{environment_part}#{timestamp}_#{file_name}.rb"
+ end
+
+ private
+
+ def file_name
+ seed_name.underscore
+ end
+
+ def class_name
+ seed_name.camelcase
+ end
+
+ def timestamp
+ @teimstamp ||= DateTime.now.strftime( "%Y%m%d%H%M%S" )
+ end
+
+ def environment_part
+ environment.blank? ? '' : "#{environment}/"
+ end
+
+ end
+
+end
View
12 lib/generators/genesis/templates/migration.erb → lib/generators/genesis/seed/templates/migration.erb
@@ -1,9 +1,11 @@
-class <%= class_name %>
+class <%= class_name %>
+
def self.up
-
+
end
-
+
def self.down
-
+
end
-end
+
+end
View
13 lib/generators/genesis_contexts/genesis_contexts_generator.rb
@@ -1,13 +0,0 @@
-require 'rails/generators'
-
-class GenesisContextsGenerator < Rails::Generators::Base
- argument :contexts, :type => :string, :default => []
-
- def self.source_root
- File.join( File.dirname(__FILE__), 'templates' )
- end
-
- def install_contexts
- @contexts.each { |context| empty_directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
- end
-end
View
15 lib/generators/prepare_genesis/prepare_genesis_generator.rb
@@ -1,15 +0,0 @@
-require 'rails/generators'
-
-class PrepareGenesisGenerator < Rails::Generators::Base
- argument :envs, :type => :string, :default => %w(development production)
-
- def self.source_root
- File.join( File.dirname(__FILE__), 'templates' )
- end
-
- def install_seeding
- envs.each { |env| empty_directory "db/seeds/#{env}" }
- copy_file 'genesis.rake', 'lib/tasks/genesis.rake'
- copy_file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
- end
-end
View
0  ...generators/prepare_genesis/templates/genesis.rake → lib/tasks/genesis.rake
File renamed without changes
View
0  rails_generators/genesis/USAGE
No changes.
View
50 rails_generators/genesis/genesis_generator.rb
@@ -1,50 +0,0 @@
-class GenesisGenerator < Rails::Generator::NamedBase
- attr_accessor :opts
- attr_accessor :environment
- attr_accessor :env_dirs
- attr_accessor :stamp
-
- def initialize( runtime_args, runtime_options={} )
- super
- @opts = {}
- @env_dirs = []
- @stamp = DateTime.now.strftime( "%Y%m%d%H%M%S" )
- parse_args( args )
- end
-
- def manifest
- env_str = @environment.nil? || @environment.empty? ? '' : "#{@environment}/"
-
- record do |m|
- m.directory "db/seeds"
- @env_dirs.each { |env_dir| m.directory "db/seeds/#{env_dirs}" }
- m.template "migration.rb", "db/seeds/#{env_str}#{@stamp}_#{file_name.underscore}.rb"
- end
- end
-
- private
-
- def parse_args( arguments )
- arguments.each do |arg|
- arg_parts = arg.split( ':' )
- if arg_parts.size > 1
- process_keyed_arg( arg_parts )
- else
- handle_env_arg( arg )
- end
- end
- end
-
- def process_keyed_arg( arg_parts )
- if arg_parts[0] == 'env'
- handle_env_arg( arg_parts[1] )
- else
- opts[arg_parts[0].to_sym] = arg_parts[1]
- end
- end
-
- def handle_env_arg( val )
- @environment = val
- @env_dirs = val
- end
-end
View
0  rails_generators/genesis/templates/genesis_override.rake
No changes.
View
9 rails_generators/genesis/templates/migration.rb
@@ -1,9 +0,0 @@
-class <%= file_name.camelcase %>
- def self.up
-
- end
-
- def self.down
-
- end
-end
View
0  rails_generators/genesis_contexts/USAGE
No changes.
View
44 rails_generators/genesis_contexts/genesis_contexts_generator.rb
@@ -1,44 +0,0 @@
-class GenesisContextsGenerator < Rails::Generator::Base
- attr_accessor :opts
- attr_accessor :environments
-
- def initialize( runtime_args, runtime_options={} )
- super
- @opts = {}
- @contexts = []
- parse_args( args )
- end
-
- def manifest
- record do |m|
- m.directory Genesis::SEEDS_ROOT
- @contexts.each { |context| m.directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
- end
- end
-
- private
-
- def parse_args( arguments )
- arguments.each do |arg|
- arg_parts = arg.split( ':' )
- if arg_parts[0] == 'contexts'
- handle_env_arg( arg_parts[1] )
- else
- opts[arg_parts[0].to_sym] = arg_parts[1]
- end
- end
- end
-
- def handle_contexts_arg( val )
- if val.include?( '[' ) && val.include?( ']')
- val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
- val.split( ',' ).each { |v| @contexts << v.trim.gsub( /,/, '' ) }
- elsif val.include?( '[' ) || val.include?( ']' )
- raise 'Error The contexts option must be formatted without any spaces in the array. ie. contexts:[accounts,users]'
- elsif val.include?( ',' )
- raise 'Error The contexts option must be formatted with braces at the beginning and end of the list. ie. contexts:[accounts,users]'
- else
- @contexts << val
- end
- end
-end
View
1  rails_generators/prepare_seeding/USAGE
@@ -1 +0,0 @@
-script/generate seed create_users OR script/generate seed create_users production
View
52 rails_generators/prepare_seeding/prepare_seeding_generator.rb
@@ -1,52 +0,0 @@
-class PrepareSeedingGenerator < Rails::Generator::Base
- attr_accessor :opts
- attr_accessor :environments
-
- def initialize( runtime_args, runtime_options = {} )
- super
- @opts = {}
- @environments = []
- parse_args( args )
- end
-
- def manifest
- record do |m|
- m.directory 'db/seeds'
- @environments.each { |env| m.directory "db/seeds/#{env}" }
- m.file 'genesis.rake', 'lib/tasks/genesis.rake'
- m.file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
- end
- end
-
- private
-
- def parse_args( arguments )
- arguments.each do |arg|
- arg_parts = arg.split( ':' )
- if arg_parts[0] == 'env'
- handle_env_arg( arg_parts[1] )
- else
- opts[arg_parts[0].to_sym] = arg_parts[1]
- end
- end
-
- validate_env_args
- end
-
- def handle_env_arg( val )
- if val.include?( '[' ) && val.include?( ']')
- val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
- val.split( ',' ).each { |v| @environments << v.trim.gsub( /,/, '' ) }
- elsif val.include?( '[' ) || val.include?( ']' )
- raise 'Error The env option must be formatted without any spaces in the array. ie. env:[development,production]'
- elsif val.include?( ',' )
- raise 'Error The env option must be formatted with braces at the beginning and end of the list. ie. env:[development,production]'
- else
- @environments << val
- end
- end
-
- def validate_env_args
- @environments += %w(development production) if @environments.empty?
- end
-end
View
13 rails_generators/prepare_seeding/templates/genesis_callbacks.rb
@@ -1,13 +0,0 @@
-class GenesisCallbacks
- def self.before_up
- end
-
- def self.after_up
- end
-
- def self.before_down
- end
-
- def self.after_down
- end
-end
View
37 spec/generators/genesis/contexts/contexts_generator_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+require 'generators/genesis/contexts/contexts_generator'
+
+describe Genesis::ContextsGenerator do
+
+ with_args 'test1' do
+
+ it "should generate the db/seeds/test1 folder" do
+ subject.should generate( 'db/seeds/contexts/test1' )
+ end
+
+ end
+
+ with_args 'test1,test2' do
+
+ %w(
+ db/seeds/contexts/test1
+ db/seeds/contexts/test2
+ ).each do |folder_path|
+
+ it "should generate the #{folder_path} folder" do
+ subject.should generate( folder_path )
+ end
+
+ end
+
+ end
+
+ with_args '--help' do
+
+ it "should ouput the correct description in the help message" do
+ subject.should output( "Description:\n Generate one or more context folders." )
+ end
+
+ end
+
+end
View
80 spec/generators/genesis/install/install_generator_spec.rb
@@ -0,0 +1,80 @@
+require 'spec_helper'
+require 'generators/genesis/install/install_generator'
+
+shared_examples_for 'a genesis installation generator' do
+
+ {
+ 'genesis.rake', 'lib/tasks/genesis.rake',
+ 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
+ }.each do |src, dest|
+
+ it "should generate the #{dest} file with the correct contents" do
+ subject.should generate( dest ) { |content|
+ content.should == File.read( File.join( source_root, src ) )
+ }
+ end
+
+ end
+
+end
+
+describe Genesis::InstallGenerator do
+
+ let :source_root do
+ described_class.source_root
+ end
+
+ context "with no arguments or options" do
+
+ it_should_behave_like 'a genesis installation generator'
+
+ %w(
+ db/seeds/production
+ db/seeds/development
+ ).each do |folder_path|
+
+ it "should generate the #{folder_path} folder" do
+ subject.should generate( folder_path )
+ end
+
+ end
+
+ end
+
+
+ with_args 'test1' do
+
+ it_should_behave_like 'a genesis installation generator'
+
+ it "should generate the db/seeds/test1 folder" do
+ subject.should generate( 'db/seeds/test1' )
+ end
+
+ end
+
+ with_args 'test1,test2' do
+
+ it_should_behave_like 'a genesis installation generator'
+
+ %w(
+ db/seeds/test1
+ db/seeds/test2
+ ).each do |folder_path|
+
+ it "should generate the #{folder_path} folder" do
+ subject.should generate( folder_path )
+ end
+
+ end
+
+ end
+
+ with_args '--help' do
+
+ it "should ouput the correct description in the help message" do
+ subject.should output( "Description:\n Installs the genesis assets necessary to create and execute seeds." )
+ end
+
+ end
+
+end
View
34 spec/generators/genesis/seed/seed_generator_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+require 'generators/genesis/seed/seed_generator'
+
+describe Genesis::SeedGenerator do
+
+ with_args 'create_users' do
+
+ xit "should generate the correct seed file" do
+ # subject.should generate( // )# { |content|
+ # content.should == File.read( File.join( source_root, src ) )
+ # }
+ end
+
+ end
+
+ with_args 'create_users production' do
+
+ xit "should generate the correct seed file" do
+ # subject.should generate( // )# { |content|
+ # content.should == File.read( File.join( source_root, src ) )
+ # }
+ end
+
+ end
+
+ with_args '--help' do
+
+ it "should ouput the correct description in the help message" do
+ subject.should output( "Description:\n Creates the specified seed file (optionally within a specificied environment)." )
+ end
+
+ end
+
+end
View
26 spec/generators/genesis_generator_spec.rb
@@ -1,26 +0,0 @@
-require 'spec_helper'
-require 'generators/genesis/genesis_generator'
-
-describe GenesisGenerator do
-
- before :each do
- @destination = File.join 'tmp', 'test_app'
- @source = GenesisGenerator.source_root
- GenesisGenerator.start '', :destination_root => @destination
- end
-
- after :each do
- FileUtils.rm_rf @destination
- end
-
- {}.each do |file, path|
- it "should copy '#{file}' to '#{path}'" do
- File.exists?( File.join( @destination, path ) ).should be_true
- end
-
- # it "should agree that the contents of '#{file}' are identical to '#{path}'" do
- # File.read( File.join( @source, file ) ).should eql File.read( File.join( @destination, path ) )
- # end
- end
-
-end
View
6 spec/spec_helper.rb
@@ -1,7 +1,9 @@
require 'rubygems'
-# require 'active_record'
-# require 'rails'
+require 'active_record'
+require 'rails'
+require 'rails/generators'
require 'bundler/setup'
+require 'genspec'
require 'genesis'
Please sign in to comment.
Something went wrong with that request. Please try again.