Skip to content

Commit

Permalink
Merge pull request #292 from emptyflask/fix/generator
Browse files Browse the repository at this point in the history
fixed generator specs and added migration version
  • Loading branch information
seuros authored Dec 6, 2017
2 parents 48c3aa9 + 54467cd commit dc464ac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
8 changes: 4 additions & 4 deletions closure_tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'activerecord', '>= 4.1.0'
gem.add_runtime_dependency 'with_advisory_lock', '>= 3.0.0'

gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'database_cleaner'
gem.add_development_dependency 'generator_spec'
gem.add_development_dependency 'parallel'
gem.add_development_dependency 'rspec-instafail'
gem.add_development_dependency 'rspec-rails'
gem.add_development_dependency 'database_cleaner'
gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'timecop'
gem.add_development_dependency 'parallel'
# gem.add_development_dependency 'ammeter', '1.1.2' # See https://github.com/mceachen/closure_tree/issues/181
# gem.add_development_dependency 'byebug'
# gem.add_development_dependency 'ruby-prof' # <- don't need this normally.
end
8 changes: 8 additions & 0 deletions lib/generators/closure_tree/migration_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'closure_tree/active_record_support'
require 'forwardable'
require 'rails/generators'
require 'rails/generators/active_record'
require 'rails/generators/named_base'

Expand Down Expand Up @@ -41,6 +42,13 @@ def ct
end
end

def migration_version
major = ActiveRecord::VERSION::MAJOR
if major >= 5
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
end
end

def self.next_migration_number(dirname)
ActiveRecord::Generators::Base.next_migration_number(dirname)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class <%= migration_class_name %> < ActiveRecord::Migration
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table :<%= migration_name %>, id: false do |t|
t.<%= primary_key_type %> :ancestor_id, null: false
Expand Down
94 changes: 46 additions & 48 deletions spec/generators/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
# require 'spec_helper'
# require 'ammeter/init'
#
# # Generators are not automatically loaded by Rails
# require 'generators/closure_tree/migration_generator'
#
# RSpec.describe ClosureTree::Generators::MigrationGenerator, type: :generator do
# TMPDIR = Dir.mktmpdir
# # Tell generator where to put its output
# destination TMPDIR
# before { prepare_destination }
#
# describe 'generator output' do
# before { run_generator %w(tag) }
# subject { migration_file('db/migrate/create_tag_hierarchies.rb') }
# it { is_expected.to be_a_migration }
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
# it { is_expected.to contain(/t.integer :generations, null: false/) }
# it { is_expected.to contain(/add_index :tag_hierarchies/) }
# end
#
# describe 'generator output with namespaced model' do
# before { run_generator %w(Namespace::Type) }
# subject { migration_file('db/migrate/create_namespace_type_hierarchies.rb') }
# it { is_expected.to be_a_migration }
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
# it { is_expected.to contain(/t.integer :generations, null: false/) }
# it { is_expected.to contain(/add_index :namespace_type_hierarchies/) }
# end
#
# describe 'generator output with namespaced model with /' do
# before { run_generator %w(namespace/type) }
# subject { migration_file('db/migrate/create_namespace_type_hierarchies.rb') }
# it { is_expected.to be_a_migration }
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
# it { is_expected.to contain(/t.integer :generations, null: false/) }
# it { is_expected.to contain(/add_index :namespace_type_hierarchies/) }
# end
#
# it 'should run all tasks in generator without errors' do
# gen = generator %w(tag)
# expect(gen).to receive :create_migration_file
# capture(:stdout) { gen.invoke_all }
# end
# end
require 'spec_helper'
require "generator_spec/test_case"

# Generators are not automatically loaded by Rails
require 'generators/closure_tree/migration_generator'

RSpec.describe ClosureTree::Generators::MigrationGenerator, type: :generator do
include GeneratorSpec::TestCase

# Tell generator where to put its output
destination Dir.mktmpdir
before { prepare_destination }

describe 'generator output' do
before { run_generator %w(tag) }
subject { File.read migration_file_name('db/migrate/create_tag_hierarchies.rb') }
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
it { is_expected.to match(/t.integer :generations, null: false/) }
it { is_expected.to match(/add_index :tag_hierarchies/) }
end

describe 'generator output with namespaced model' do
before { run_generator %w(Namespace::Type) }
subject { File.read migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') }
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
it { is_expected.to match(/t.integer :generations, null: false/) }
it { is_expected.to match(/add_index :namespace_type_hierarchies/) }
end

describe 'generator output with namespaced model with /' do
before { run_generator %w(namespace/type) }
subject { File.read migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') }
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
it { is_expected.to match(/t.integer :generations, null: false/) }
it { is_expected.to match(/add_index :namespace_type_hierarchies/) }
end

it 'should run all tasks in generator without errors' do
gen = generator %w(tag)
expect(gen).to receive :create_migration_file
capture(:stdout) { gen.invoke_all }
end
end

0 comments on commit dc464ac

Please sign in to comment.