Skip to content

Commit

Permalink
Set the default value of metadata as empty hash string
Browse files Browse the repository at this point in the history
  • Loading branch information
kymmt90 committed Apr 19, 2018
1 parent 0a05a0b commit 7115a10
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 6 deletions.
4 changes: 0 additions & 4 deletions lib/generators/statesman/generator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,5 @@ def mysql?
def database_supports_partial_indexes?
Statesman::Adapters::ActiveRecord.database_supports_partial_indexes?
end

def metadata_default_value
Utils.rails_5_or_higher? ? "{}" : "{}".inspect
end
end
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
Expand Up @@ -2,7 +2,7 @@ class Create<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveR
def change
create_table :<%= table_name %> do |t|
t.string :to_state, null: false
t.text :metadata<%= ", default: #{metadata_default_value}" unless mysql? %>
t.text :metadata<%= ", default: \"{}\"" unless mysql? %>
t.integer :sort_key, null: false
t.integer :<%= parent_id %>, null: false
t.boolean :most_recent<%= ", null: false" if database_supports_partial_indexes? %>
Expand Down
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,7 +1,7 @@
class AddStatesmanTo<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %>
def change
add_column :<%= table_name %>, :to_state, :string, null: false
add_column :<%= table_name %>, :metadata, :text<%= ", default: #{metadata_default_value}" unless mysql? %>
add_column :<%= table_name %>, :metadata, :text<%= ", default: \"{}\"" unless mysql? %>
add_column :<%= table_name %>, :sort_key, :integer, null: false
add_column :<%= table_name %>, :<%= parent_id %>, :integer, null: false
add_column :<%= table_name %>, :most_recent, null: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
it "includes a foreign key" do
expect(subject).to contain("add_foreign_key :bacon_transitions, :bacons")
end

it { is_expected.to contain(/default: "{}"/) }
end

describe "properly adds class names" do
Expand Down
1 change: 1 addition & 0 deletions spec/generators/statesman/migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
it { is_expected.to contain(/:bacon_transition/) }
it { is_expected.not_to contain(%r{:yummy/bacon}) }
it { is_expected.to contain(/null: false/) }
it { is_expected.to contain(/default: "{}"/) }

it "names the sorting index appropriately" do
expect(subject).
Expand Down

0 comments on commit 7115a10

Please sign in to comment.