Skip to content

Commit

Permalink
Merge branch 'multiple_models' of https://github.com/rselk/closure_tree
Browse files Browse the repository at this point in the history
… into five_two_oh
  • Loading branch information
mceachen committed Nov 2, 2014
2 parents 8ee494b + ecd878d commit 0df24f5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,3 +10,4 @@ tmp/
.yardoc/
.rvmrc
*.lock
tmp/
5 changes: 2 additions & 3 deletions closure_tree.gemspec
Expand Up @@ -20,14 +20,13 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'with_advisory_lock', '>= 3.0.0'

gem.add_development_dependency 'yard'
gem.add_development_dependency 'rspec', '>= 3.0'
gem.add_development_dependency 'rspec-instafail'
# TODO: delete rspec-rails.
gem.add_development_dependency 'rspec-rails' # FIXME: for rspec-rails and rspec fixture support
gem.add_development_dependency 'rspec-rails', '>= 3.1'
gem.add_development_dependency 'uuidtools'
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'
# gem.add_development_dependency 'ruby-prof' # <- don't need this normally.
end
12 changes: 10 additions & 2 deletions lib/generators/closure_tree/migration_generator.rb
@@ -1,11 +1,12 @@
require 'rails/generators/named_base'
require 'rails/generators/active_record/migration'
require 'rails/generators/active_record'
require 'forwardable'

module ClosureTree
module Generators # :nodoc:
class MigrationGenerator < ::Rails::Generators::NamedBase # :nodoc:
include ActiveRecord::Generators::Migration
include ActiveRecord::Generators::Migration if Rails::VERSION::MAJOR == 3
include Rails::Generators::Migration

extend Forwardable
def_delegators :ct, :hierarchy_table_name, :primary_key_type
Expand All @@ -18,13 +19,20 @@ def create_migration_file
migration_template 'create_hierarchies_table.rb.erb', "db/migrate/create_#{ct.hierarchy_table_name}.rb"
end

private

def migration_class_name
"Create#{ct.hierarchy_table_name.camelize}"
end

def ct
@ct ||= class_name.constantize._ct
end

def self.next_migration_number(dirname)
ActiveRecord::Generators::Base.next_migration_number(dirname)
end

end
end
end
Expand Up @@ -8,9 +8,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration

add_index :<%= hierarchy_table_name %>, [:ancestor_id, :descendant_id, :generations],
unique: true,
name: "anc_desc_idx"
name: "<%= file_name %>_anc_desc_idx"

add_index :<%= hierarchy_table_name -%>, [:descendant_id],
name: "desc_idx"
name: "<%= file_name %>_desc_idx"
end
end
3 changes: 3 additions & 0 deletions spec/db/models.rb
Expand Up @@ -90,6 +90,9 @@ class CuisineType < ActiveRecord::Base
end

module Namespace
def self.table_name_prefix
'namespace_'
end
class Type < ActiveRecord::Base
acts_as_tree :dependent => :destroy
attr_accessible :name if _ct.use_attr_accessible?
Expand Down
50 changes: 50 additions & 0 deletions spec/generator_spec.rb
@@ -0,0 +1,50 @@
require 'spec_helper'
require 'ammeter/init'

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

# Note - Tests set to pending due to failures on Travis-ci build.
# Tests pass locally.

RSpec.describe ClosureTree::Generators::MigrationGenerator, :type => :generator do
# Tell generator where to put its output
destination File.expand_path('../tmp', __FILE__)
before { prepare_destination }

xdescribe '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

xdescribe '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

xdescribe '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' do
gen = generator %w(tag)
expect(gen).to receive :create_migration_file
capture(:stdout) { gen.invoke_all }
end
end

0 comments on commit 0df24f5

Please sign in to comment.