-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added specs for generators; now handles namespaced classes
- Loading branch information
1 parent
77d3036
commit 82e13f0
Showing
14 changed files
with
169 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
*.rbc | ||
.bundle | ||
.config | ||
.rspec | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
spec/generators/statesman/active_record_transition_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
spec/generators/statesman/mongoid_transition_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters