From d01e6366d3eb18e5449643a07f50210f1f7862bf Mon Sep 17 00:00:00 2001 From: Grey Baker Date: Thu, 18 May 2017 22:52:11 +0100 Subject: [PATCH 1/2] Add foreign keys when creating a new transition table --- lib/generators/statesman/generator_helpers.rb | 4 ++++ .../statesman/templates/create_migration.rb.erb | 2 ++ .../active_record_transition_generator_spec.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/lib/generators/statesman/generator_helpers.rb b/lib/generators/statesman/generator_helpers.rb index a0ec18b2..b9ad87e3 100644 --- a/lib/generators/statesman/generator_helpers.rb +++ b/lib/generators/statesman/generator_helpers.rb @@ -20,6 +20,10 @@ def parent_name parent.demodulize.underscore end + def parent_table_name + parent.demodulize.underscore.pluralize + end + def parent_id parent_name + "_id" end diff --git a/lib/generators/statesman/templates/create_migration.rb.erb b/lib/generators/statesman/templates/create_migration.rb.erb index a2ac369b..df45a61b 100644 --- a/lib/generators/statesman/templates/create_migration.rb.erb +++ b/lib/generators/statesman/templates/create_migration.rb.erb @@ -9,6 +9,8 @@ class Create<%= migration_class_name %> < ActiveRecord::Migration t.timestamps null: false end + add_foreign_key :<%= table_name %>, :<%= parent_table_name %> + add_index(:<%= table_name %>, [:<%= parent_id %>, :sort_key], unique: true, diff --git a/spec/generators/statesman/active_record_transition_generator_spec.rb b/spec/generators/statesman/active_record_transition_generator_spec.rb index b4af022b..740d7f63 100644 --- a/spec/generators/statesman/active_record_transition_generator_spec.rb +++ b/spec/generators/statesman/active_record_transition_generator_spec.rb @@ -7,6 +7,20 @@ let(:migration_name) { 'db/migrate/create_bacon_transitions.rb' } end + describe 'creates a migration' do + subject { file("db/migrate/#{time}_create_bacon_transitions.rb") } + + before { allow(Time).to receive(:now).and_return(mock_time) } + before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) } + + let(:mock_time) { double('Time', utc: double('UTCTime', strftime: time)) } + let(:time) { '5678309' } + + it "includes a foreign key" do + expect(subject).to contain("add_foreign_key :bacon_transitions, :bacons") + end + end + describe 'properly adds class names' do before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) } subject { file('app/models/yummy/bacon_transition.rb') } From 9b9f9e9460c1b162dca8e4bd9e90da6111f72862 Mon Sep 17 00:00:00 2001 From: Grey Baker Date: Sat, 20 May 2017 09:01:47 +0100 Subject: [PATCH 2/2] Add comment on foreign keys --- lib/generators/statesman/templates/create_migration.rb.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/generators/statesman/templates/create_migration.rb.erb b/lib/generators/statesman/templates/create_migration.rb.erb index df45a61b..f958871c 100644 --- a/lib/generators/statesman/templates/create_migration.rb.erb +++ b/lib/generators/statesman/templates/create_migration.rb.erb @@ -9,6 +9,7 @@ class Create<%= migration_class_name %> < ActiveRecord::Migration t.timestamps null: false end + # Foreign keys are optional, but highly recommended add_foreign_key :<%= table_name %>, :<%= parent_table_name %> add_index(:<%= table_name %>,