Skip to content

Commit

Permalink
Use named base as a template.
Browse files Browse the repository at this point in the history
This reduces the amount of customization that power enum adds to its
generator and allows for the migration_template helper to do its job of
detecting and removing an existing template for the same migration.
  • Loading branch information
zbelzer committed Jan 20, 2014
1 parent 59d2993 commit 6ba0077
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
50 changes: 11 additions & 39 deletions lib/generators/enum/enum_generator.rb
Expand Up @@ -2,12 +2,12 @@
# Released under the MIT license. See LICENSE for details.

# Generator for PowerEnum
class EnumGenerator < Rails::Generators::Base
class EnumGenerator < Rails::Generators::NamedBase
require File.expand_path('../enum_generator_helpers/migration_number', __FILE__)
include EnumGeneratorHelpers::MigrationNumber
include Rails::Generators::Migration
extend EnumGeneratorHelpers::MigrationNumber

source_root File.expand_path('../templates', __FILE__)
argument :enum_name, :type => :string
class_option :migration, :type => :boolean, :default => true, :desc => 'Generate migration for the enum'
class_option :fixture, :type => :boolean, :default => false, :desc => 'Generate fixture for the enum'

Expand All @@ -18,45 +18,17 @@ def generate_model

# Generates the migration to create the enum table.
def generate_migration
template migration_template, "db/migrate/#{migration_file_name}.rb" if options.migration?
migration_template 'rails31_migration.rb.erb', "db/migrate/create_enum_#{table_name}.rb" if options.migration?
end

hook_for :test_framework, :as => :model do |enum_generator_instance, test_generator_class|
# Need to do this because I'm changing the default value of the 'fixture' option.
enum_generator_instance.invoke( test_generator_class, [enum_generator_instance.enum_name], { 'fixture' => enum_generator_instance.options.fixture? } )
# Do not pluralize enumeration names
def pluralize_table_names?
false
end

no_tasks do

# Returns the file name of the enum model without the .rb extension.
def file_name
enum_name.underscore
end

# Returns the class name of the enum.
def enum_class_name
file_name.camelize
end

# Derives the name for the migration, something like 'create_enum_fruit'
def migration_name
"create_enum_#{file_name}"
end

# Returns the class name of our migration
def migration_class_name
migration_name.camelize
end

# Generates and returns the filename of our migration
def migration_file_name
"#{next_migration_number}_#{migration_name}"
end

# Returns the name of the template file for the migration.
def migration_template
'rails31_migration.rb.erb'
end

hook_for :test_framework, :as => :model do |enum_generator_instance, test_generator_class|
# Need to do this because I'm changing the default value of the 'fixture' option.
table_name = enum_generator_instance.send(:table_name)
enum_generator_instance.invoke( test_generator_class, [table_name], { 'fixture' => enum_generator_instance.options.fixture? } )
end
end
12 changes: 1 addition & 11 deletions lib/generators/enum/enum_generator_helpers/migration_number.rb
Expand Up @@ -2,20 +2,10 @@
module EnumGeneratorHelpers
# Helper methods to figure out the migration number.
module MigrationNumber

# Returns the number of the last migration.
# @return [Fixnum]
def current_migration_number(dirname = nil)
dirname ||= "#{Rails.root}/db/migrate/[0-9]*_*.rb"
Dir.glob(dirname).collect do |file|
File.basename(file).split("_").first.to_i
end.max.to_i
end

# Returns the next upcoming migration number. Sadly, Rails has no API for
# this, so we're reduced to copying from ActiveRecord::Generators::Migration
# @return [Fixnum]
def next_migration_number(dirname = nil)
def next_migration_number(dirname)
# Lifted directly from ActiveRecord::Generators::Migration
# Unfortunately, no API is provided by Rails at this time.
next_migration_number = current_migration_number(dirname) + 1
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/enum/templates/model.rb.erb
@@ -1,3 +1,3 @@
class <%= enum_class_name %> < ActiveRecord::Base
class <%= class_name %> < ActiveRecord::Base
acts_as_enumerated
end
4 changes: 2 additions & 2 deletions spec/generators/enum_gen_spec.rb
Expand Up @@ -8,7 +8,7 @@ def generate_migration(enum_name, &block)

context 'no arguments or options' do
it 'should generate an error message' do
subject.should output("No value provided for required arguments 'enum_name'")
subject.should output("No value provided for required arguments 'name'")
end
end

Expand Down Expand Up @@ -41,4 +41,4 @@ def change
}
end
end
end
end

0 comments on commit 6ba0077

Please sign in to comment.