Skip to content

Commit dc464ac

Browse files
authored
Merge pull request #292 from emptyflask/fix/generator
fixed generator specs and added migration version
2 parents 48c3aa9 + 54467cd commit dc464ac

File tree

4 files changed

+59
-53
lines changed

4 files changed

+59
-53
lines changed

closure_tree.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ Gem::Specification.new do |gem|
1919
gem.add_runtime_dependency 'activerecord', '>= 4.1.0'
2020
gem.add_runtime_dependency 'with_advisory_lock', '>= 3.0.0'
2121

22+
gem.add_development_dependency 'appraisal'
23+
gem.add_development_dependency 'database_cleaner'
24+
gem.add_development_dependency 'generator_spec'
25+
gem.add_development_dependency 'parallel'
2226
gem.add_development_dependency 'rspec-instafail'
2327
gem.add_development_dependency 'rspec-rails'
24-
gem.add_development_dependency 'database_cleaner'
25-
gem.add_development_dependency 'appraisal'
2628
gem.add_development_dependency 'timecop'
27-
gem.add_development_dependency 'parallel'
28-
# gem.add_development_dependency 'ammeter', '1.1.2' # See https://github.com/mceachen/closure_tree/issues/181
2929
# gem.add_development_dependency 'byebug'
3030
# gem.add_development_dependency 'ruby-prof' # <- don't need this normally.
3131
end

lib/generators/closure_tree/migration_generator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'closure_tree/active_record_support'
22
require 'forwardable'
3+
require 'rails/generators'
34
require 'rails/generators/active_record'
45
require 'rails/generators/named_base'
56

@@ -41,6 +42,13 @@ def ct
4142
end
4243
end
4344

45+
def migration_version
46+
major = ActiveRecord::VERSION::MAJOR
47+
if major >= 5
48+
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
49+
end
50+
end
51+
4452
def self.next_migration_number(dirname)
4553
ActiveRecord::Generators::Base.next_migration_number(dirname)
4654
end

lib/generators/closure_tree/templates/create_hierarchies_table.rb.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class <%= migration_class_name %> < ActiveRecord::Migration
1+
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
22
def change
33
create_table :<%= migration_name %>, id: false do |t|
44
t.<%= primary_key_type %> :ancestor_id, null: false
Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
1-
# require 'spec_helper'
2-
# require 'ammeter/init'
3-
#
4-
# # Generators are not automatically loaded by Rails
5-
# require 'generators/closure_tree/migration_generator'
6-
#
7-
# RSpec.describe ClosureTree::Generators::MigrationGenerator, type: :generator do
8-
# TMPDIR = Dir.mktmpdir
9-
# # Tell generator where to put its output
10-
# destination TMPDIR
11-
# before { prepare_destination }
12-
#
13-
# describe 'generator output' do
14-
# before { run_generator %w(tag) }
15-
# subject { migration_file('db/migrate/create_tag_hierarchies.rb') }
16-
# it { is_expected.to be_a_migration }
17-
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
18-
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
19-
# it { is_expected.to contain(/t.integer :generations, null: false/) }
20-
# it { is_expected.to contain(/add_index :tag_hierarchies/) }
21-
# end
22-
#
23-
# describe 'generator output with namespaced model' do
24-
# before { run_generator %w(Namespace::Type) }
25-
# subject { migration_file('db/migrate/create_namespace_type_hierarchies.rb') }
26-
# it { is_expected.to be_a_migration }
27-
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
28-
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
29-
# it { is_expected.to contain(/t.integer :generations, null: false/) }
30-
# it { is_expected.to contain(/add_index :namespace_type_hierarchies/) }
31-
# end
32-
#
33-
# describe 'generator output with namespaced model with /' do
34-
# before { run_generator %w(namespace/type) }
35-
# subject { migration_file('db/migrate/create_namespace_type_hierarchies.rb') }
36-
# it { is_expected.to be_a_migration }
37-
# it { is_expected.to contain(/t.integer :ancestor_id, null: false/) }
38-
# it { is_expected.to contain(/t.integer :descendant_id, null: false/) }
39-
# it { is_expected.to contain(/t.integer :generations, null: false/) }
40-
# it { is_expected.to contain(/add_index :namespace_type_hierarchies/) }
41-
# end
42-
#
43-
# it 'should run all tasks in generator without errors' do
44-
# gen = generator %w(tag)
45-
# expect(gen).to receive :create_migration_file
46-
# capture(:stdout) { gen.invoke_all }
47-
# end
48-
# end
1+
require 'spec_helper'
2+
require "generator_spec/test_case"
3+
4+
# Generators are not automatically loaded by Rails
5+
require 'generators/closure_tree/migration_generator'
6+
7+
RSpec.describe ClosureTree::Generators::MigrationGenerator, type: :generator do
8+
include GeneratorSpec::TestCase
9+
10+
# Tell generator where to put its output
11+
destination Dir.mktmpdir
12+
before { prepare_destination }
13+
14+
describe 'generator output' do
15+
before { run_generator %w(tag) }
16+
subject { File.read migration_file_name('db/migrate/create_tag_hierarchies.rb') }
17+
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
18+
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
19+
it { is_expected.to match(/t.integer :generations, null: false/) }
20+
it { is_expected.to match(/add_index :tag_hierarchies/) }
21+
end
22+
23+
describe 'generator output with namespaced model' do
24+
before { run_generator %w(Namespace::Type) }
25+
subject { File.read migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') }
26+
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
27+
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
28+
it { is_expected.to match(/t.integer :generations, null: false/) }
29+
it { is_expected.to match(/add_index :namespace_type_hierarchies/) }
30+
end
31+
32+
describe 'generator output with namespaced model with /' do
33+
before { run_generator %w(namespace/type) }
34+
subject { File.read migration_file_name('db/migrate/create_namespace_type_hierarchies.rb') }
35+
it { is_expected.to match(/t.integer :ancestor_id, null: false/) }
36+
it { is_expected.to match(/t.integer :descendant_id, null: false/) }
37+
it { is_expected.to match(/t.integer :generations, null: false/) }
38+
it { is_expected.to match(/add_index :namespace_type_hierarchies/) }
39+
end
40+
41+
it 'should run all tasks in generator without errors' do
42+
gen = generator %w(tag)
43+
expect(gen).to receive :create_migration_file
44+
capture(:stdout) { gen.invoke_all }
45+
end
46+
end

0 commit comments

Comments
 (0)