Deal with polymorphic attributes correctly in the generators

This commit is contained in:
Rafael Mendonça França 2012-12-09 15:20:32 -03:00
parent 4a487f94b9
commit b05b77be9c
6 changed files with 41 additions and 10 deletions

@ -22,6 +22,15 @@ def create_controller_files
hook_for :helper, as: :scaffold do |invoked|
invoke invoked, [ controller_name ]
end
private
def attributes_names
attributes.each_with_object([]) do |attribute, names|
names << (attribute.reference? ? ":#{attribute.name}_id" : ":#{attribute.name}")
names << ":#{attribute.name}_type" if attribute.polymorphic?
end
end
end
end
end

@ -91,10 +91,10 @@ def set_<%= singular_table_name %>
# Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age)
# Also, you can specialize this method with per-user checking of permissible attributes.
def <%= "#{singular_table_name}_params" %>
<%- if attributes.empty? -%>
<%- if attributes_names.empty? -%>
params[<%= ":#{singular_table_name}" %>]
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map { |a| ":#{a.index_name}" }.join(', ') %>)
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.join(', ') %>)
<%- end -%>
end
end

@ -4,11 +4,13 @@
one:
<% attributes.each do |attribute| -%>
<%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %>
<%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %>
<% end -%>
two:
<% attributes.each do |attribute| -%>
<%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %>
<%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %>
<% end -%>
<% else -%>
# This model initially had no columns defined. If you add columns to the

@ -20,11 +20,17 @@ def create_test_files
def attributes_hash
return if attributes.empty?
attributes.map do |a|
name = a.name
name = "#{name}_id" if a.reference?
"#{name}: @#{singular_table_name}.#{name}"
end.sort.join(', ')
hash_values = []
attributes.each do |a|
hash_values << hash_value(a.reference? ? "#{a.name}_id" : a.name)
hash_values << hash_value("#{a.name}_type") if a.polymorphic?
end
hash_values.sort.join(', ')
end
def hash_value(name)
"#{name}: @#{singular_table_name}.#{name}"
end
end
end

@ -278,6 +278,11 @@ def test_fixtures_use_the_references_ids
assert_file "test/fixtures/line_items.yml", /product_id: /, /cart_id: /
end
def test_fixtures_use_the_references_ids_and_type
run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"]
assert_file "test/fixtures/line_items.yml", /product_id: /, /product_type: Product/, /cart_id: /
end
def test_fixture_is_skipped
run_generator ["account", "--skip-fixture"]
assert_no_file "test/fixtures/accounts.yml"

@ -70,6 +70,15 @@ def test_controller_permit_references_attributes
end
end
def test_controller_permit_polymorphic_references_attributes
run_generator ["LineItem", "product:references{polymorphic}"]
assert_file "app/controllers/line_items_controller.rb" do |content|
assert_match(/def line_item_params/, content)
assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content)
end
end
def test_helper_are_invoked_with_a_pluralized_name
run_generator
assert_file "app/helpers/users_helper.rb", /module UsersHelper/
@ -86,13 +95,13 @@ def test_views_are_generated
end
def test_functional_tests
run_generator ["User", "name:string", "age:integer", "organization:references"]
run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"]
assert_file "test/controllers/users_controller_test.rb" do |content|
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
assert_match(/test "should get index"/, content)
assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id \}/, content)
assert_match(/put :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id \}/, content)
assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content)
assert_match(/put :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content)
end
end