aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-07-04 15:16:00 -0600
committerSean Griffin <sean@thoughtbot.com>2014-08-08 10:29:27 -0600
commitfdfc0fc6c91b46a3ff769687e1a456ef2b205417 (patch)
tree9ff94e55f5837376b0b65de720137fe65a6040bc /railties/test/generators
parentc69e21d36b3a14f6894fb768ffeb165bd8d7c533 (diff)
downloadrails-fdfc0fc6c91b46a3ff769687e1a456ef2b205417.tar.gz
rails-fdfc0fc6c91b46a3ff769687e1a456ef2b205417.tar.bz2
rails-fdfc0fc6c91b46a3ff769687e1a456ef2b205417.zip
Add a `required` option to the model generator
Syntax was chosen to follow the passing of multiple options to decimal/numeric types. Curly braces, and allowing any of `,`, `.`, or `-` to be used as a separator to avoid the need for shell quoting. (I'm intending to expand this to all columns, but that's another PR. The `required` option will cause 2 things to change. `required: true` will be added to the association. `null: false` will be added to the column in the migration.
Diffstat (limited to 'railties/test/generators')
-rw-r--r--railties/test/generators/migration_generator_test.rb12
-rw-r--r--railties/test/generators/model_generator_test.rb44
2 files changed, 56 insertions, 0 deletions
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 6fac643ed0..72f5fe29ca 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -159,6 +159,18 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_add_migration_with_required_references
+ migration = "add_references_to_books"
+ run_generator [migration, "author:belongs_to{required}", "distributor:references{polymorphic,required}"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |change|
+ assert_match(/add_reference :books, :author, index: true, null: false/, change)
+ assert_match(/add_reference :books, :distributor, polymorphic: true, index: true, null: false/, change)
+ end
+ end
+ end
+
def test_create_join_table_migration
migration = "add_media_join_table"
run_generator [migration, "artist_id", "musics:uniq"]
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb
index b67cf02d7b..8e5b556a7d 100644
--- a/railties/test/generators/model_generator_test.rb
+++ b/railties/test/generators/model_generator_test.rb
@@ -1,5 +1,6 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/model/model_generator'
+require 'active_support/core_ext/string/strip'
class ModelGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
@@ -363,6 +364,49 @@ class ModelGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_required_belongs_to_adds_required_association
+ run_generator ["account", "supplier:references{required}"]
+
+ expected_file = <<-FILE.strip_heredoc
+ class Account < ActiveRecord::Base
+ belongs_to :supplier, required: true
+ end
+ FILE
+ assert_file "app/models/account.rb", expected_file
+ end
+
+ def test_required_polymorphic_belongs_to_generages_correct_model
+ run_generator ["account", "supplier:references{required,polymorphic}"]
+
+ expected_file = <<-FILE.strip_heredoc
+ class Account < ActiveRecord::Base
+ belongs_to :supplier, polymorphic: true, required: true
+ end
+ FILE
+ assert_file "app/models/account.rb", expected_file
+ end
+
+ def test_required_and_polymorphic_are_order_independent
+ run_generator ["account", "supplier:references{polymorphic.required}"]
+
+ expected_file = <<-FILE.strip_heredoc
+ class Account < ActiveRecord::Base
+ belongs_to :supplier, polymorphic: true, required: true
+ end
+ FILE
+ assert_file "app/models/account.rb", expected_file
+ end
+
+ def test_required_adds_null_false_to_column
+ run_generator ["account", "supplier:references{required}"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_match(/t\.references :supplier,.*\snull: false/, up)
+ end
+ end
+ end
+
private
def assert_generated_fixture(path, parsed_contents)
fixture_file = File.new File.expand_path(path, destination_root)