Remove `required: true` from the model generator template

`belongs_to` association have `required: true` by default
https://github.com/rails/rails/pull/18937 onwards so we don't need it
in the generator template.
We still need the code for required in the command line generator as
it adds `null: false` in the migration.
上级 d1107f4d
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
<% attributes.select(&:reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
<% end -%>
<% attributes.select(&:rich_text?).each do |attribute| -%>
has_rich_text :<%= attribute.name %>
......
......@@ -414,23 +414,12 @@ def test_database_puts_migrations_in_configured_folder_with_aliases
end
end
def test_required_belongs_to_adds_required_association
run_generator ["account", "supplier:references{required}"]
expected_file = <<~FILE
class Account < ApplicationRecord
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
class Account < ApplicationRecord
belongs_to :supplier, polymorphic: true, required: true
belongs_to :supplier, polymorphic: true
end
FILE
assert_file "app/models/account.rb", expected_file
......@@ -441,7 +430,7 @@ def test_required_and_polymorphic_are_order_independent
expected_file = <<~FILE
class Account < ApplicationRecord
belongs_to :supplier, polymorphic: true, required: true
belongs_to :supplier, polymorphic: true
end
FILE
assert_file "app/models/account.rb", expected_file
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册