Skip to content

Commit

Permalink
added specs for generators; now handles namespaced classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hrmrebecca committed May 2, 2014
1 parent 77d3036 commit 82e13f0
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.rbc
.bundle
.config
.rspec
.yardoc
Gemfile.lock
InstalledFiles
Expand Down
21 changes: 4 additions & 17 deletions lib/generators/statesman/active_record_transition_generator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
require "rails/generators"
require "generators/statesman/generator_helpers"

module Statesman
class ActiveRecordTransitionGenerator < Rails::Generators::Base
include Statesman::GeneratorHelpers

desc "Create an ActiveRecord-based transition model"\
"with the required attributes"

argument :parent, type: :string, desc: "Your parent model name"
argument :klass, type: :string, desc: "Your transition model name"
argument :klass, type: :string, desc: "Your transition model name"

source_root File.expand_path('../templates', __FILE__)

Expand All @@ -17,26 +20,10 @@ def create_model_file

private

def next_migration_number
Time.now.utc.strftime("%Y%m%d%H%M%S")
end

def migration_file_name
"db/migrate/#{next_migration_number}_create_#{table_name}.rb"
end

def model_file_name
"app/models/#{klass.underscore}.rb"
end

def table_name
klass.underscore.pluralize
end

def parent_id
parent.underscore + "_id"
end

def rails_4?
Rails.version.split(".").map(&:to_i).first >= 4
end
Expand Down
33 changes: 33 additions & 0 deletions lib/generators/statesman/generator_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Statesman
module GeneratorHelpers

def class_name_option
", class_name: '#{parent}'" unless parent.underscore == parent_name
end

def model_file_name
"app/models/#{klass.underscore}.rb"
end

def migration_class_name
klass.gsub(%r[::],'').pluralize
end

def next_migration_number
Time.now.utc.strftime("%Y%m%d%H%M%S")
end

def parent_name
parent.demodulize.underscore
end

def parent_id
parent_name + "_id"
end

def table_name
klass.demodulize.underscore.pluralize
end

end
end
16 changes: 4 additions & 12 deletions lib/generators/statesman/migration_generator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require "rails/generators"
require "generators/statesman/generator_helpers"

# Add statesman attributes to a pre-existing transition class
module Statesman
class MigrationGenerator < Rails::Generators::Base
include Statesman::GeneratorHelpers

desc "Add the required Statesman attributes to your transition model"

argument :parent, type: :string, desc: "Your parent model name"
Expand All @@ -16,20 +19,9 @@ def create_model_file

private

def next_migration_number
Time.now.utc.strftime("%Y%m%d%H%M%S")
end

def file_name
"db/migrate/#{next_migration_number}_add_statesman_to_#{table_name}.rb"
end

def table_name
klass.underscore.pluralize
end

def parent_id
parent.underscore + "_id"
end

end
end
10 changes: 3 additions & 7 deletions lib/generators/statesman/mongoid_transition_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "rails/generators"
require "generators/statesman/generator_helpers"

module Statesman
class MongoidTransitionGenerator < Rails::Generators::Base
include Statesman::GeneratorHelpers

desc "Create a Mongoid-based transition model with the required attributes"

argument :parent, type: :string, desc: "Your parent model name"
Expand All @@ -15,16 +18,9 @@ def create_model_file

private

def model_file_name
"app/models/#{klass.underscore}.rb"
end

def collection_name
klass.underscore.pluralize
end

def parent_id
parent.underscore + "_id"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class <%= klass %> < ActiveRecord::Base
<% unless rails_4? %>
attr_accessible :to_state, :metadata, :sort_key
<% end %>
belongs_to :<%= parent.underscore %>, inverse_of: :<%= table_name %>
belongs_to :<%= parent_name %><%= class_name_option %>, inverse_of: :<%= table_name %>
end
2 changes: 1 addition & 1 deletion lib/generators/statesman/templates/create_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Create<%= klass.pluralize %> < ActiveRecord::Migration
class Create<%= migration_class_name %> < ActiveRecord::Migration
def change
create_table :<%= table_name %> do |t|
t.string :to_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class <%= klass %>

index({ sort_key: 1 })

belongs_to :<%= parent.underscore %>, index: true
belongs_to :<%= parent %><%= class_name_option %>, index: true

end
2 changes: 1 addition & 1 deletion lib/generators/statesman/templates/update_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddStatesmanTo<%= klass.pluralize %> < ActiveRecord::Migration
class AddStatesmanTo<%= migration_class_name %> < ActiveRecord::Migration
def change
add_column :<%= table_name %>, :to_state, :string
add_column :<%= table_name %>, :metadata, :text, default: "{}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "spec_helper"
require "support/generators_shared_examples"
require "generators/statesman/active_record_transition_generator"

describe Statesman::ActiveRecordTransitionGenerator, type: :generator do

it_behaves_like "a generator" do
let(:migration_name) { 'db/migrate/create_bacon_transitions.rb' }
end

describe 'the model contains the correct words' do
before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
subject { file('app/models/yummy/bacon_transition.rb') }

it { should contain(%r[:bacon_transition]) }
it { should_not contain(%r[:yummy/bacon]) }
it { should contain(%r[class_name: 'Yummy::Bacon']) }
end

describe 'the model contains the correct words' do
before { run_generator %w[Bacon BaconTransition] }
subject { file('app/models/bacon_transition.rb') }

it { should_not contain(%r[class_name:]) }
it { should contain(%r[class BaconTransition]) }
end

end
28 changes: 28 additions & 0 deletions spec/generators/statesman/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "spec_helper"
require "support/generators_shared_examples"
require "generators/statesman/migration_generator"

describe Statesman::MigrationGenerator, type: :generator do

it_behaves_like "a generator" do
let(:migration_name) { 'db/migrate/add_statesman_to_bacon_transitions.rb' }
end

describe 'the model contains the correct words' do
let(:migration_number) { '5678309' }
let(:mock_time) { double('Time', utc: double('UTCTime', strftime: migration_number)) }

subject {
file("db/migrate/#{migration_number}_add_statesman_to_bacon_transitions.rb")
}

before {
Time.stub(:now).and_return(mock_time)
run_generator %w[Yummy::Bacon Yummy::BaconTransition]
}

it { should contain(%r[:bacon_transition]) }
it { should_not contain(%r[:yummy/bacon]) }
end

end
23 changes: 23 additions & 0 deletions spec/generators/statesman/mongoid_transition_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "spec_helper"
require "support/generators_shared_examples"
require "generators/statesman/mongoid_transition_generator"

describe Statesman::MongoidTransitionGenerator, type: :generator do

describe 'the model contains the correct words' do
before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
subject { file('app/models/yummy/bacon_transition.rb') }

it { should_not contain(%r[:yummy/bacon]) }
it { should contain(%r[class_name: 'Yummy::Bacon']) }
end

describe 'the model contains the correct words' do
before { run_generator %w[Bacon BaconTransition] }
subject { file('app/models/bacon_transition.rb') }

it { should_not contain(%r[class_name:]) }
it { should_not contain(%r[CreateYummy::Bacon]) }
end

end
33 changes: 33 additions & 0 deletions spec/support/generators_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails/version"
require "ammeter/init"

TMP_GENERATOR_PATH = File.expand_path('../generator-tmp', __FILE__)

shared_examples 'a generator' do
destination TMP_GENERATOR_PATH
before { prepare_destination }
let(:gen) { generator %w[Yummy::Bacon Yummy::BaconTransition] }

it 'invokes create_model_file method' do
expect(gen).to receive(:create_model_file)
capture(:stdout) { gen.invoke_all }
end

describe 'it runs the generator and check things out' do

before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }

describe 'it generates a correctly named file' do
subject { file(migration_name) }
it { should be_a_migration }
end

end

end

RSpec.configure do |config|
config.after :all do
FileUtils.rm_rf(TMP_GENERATOR_PATH)
end
end
15 changes: 8 additions & 7 deletions statesman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 2.14.1"
spec.add_development_dependency "guard-rspec", "~> 3.0.2"
spec.add_development_dependency "rubocop", "~> 0.18.1"
spec.add_development_dependency "rspec", "~> 2.14.1"
spec.add_development_dependency "guard-rspec", "~> 3.0.2"
spec.add_development_dependency "rubocop", "~> 0.18.1"
spec.add_development_dependency "guard-rubocop", "~> 0.2.2"
spec.add_development_dependency "activerecord", "~> 3.2"
spec.add_development_dependency "sqlite3", "~> 1.3.8"
spec.add_development_dependency "mongoid", "~> 3.1.5"
spec.add_development_dependency "sqlite3", "~> 1.3.8"
spec.add_development_dependency "mongoid", "~> 3.1.5"
spec.add_development_dependency "rails", "~> 3.2"
spec.add_development_dependency "ammeter", "~> 1.0.0"
end

0 comments on commit 82e13f0

Please sign in to comment.