Skip to content
Browse files

Deal with polymorphic attributes correctly in the generators

  • Loading branch information...
1 parent 4a487f9 commit b05b77be9caf2617f35d93786d9b40c17e07bf64 @rafaelfranca rafaelfranca committed Dec 9, 2012
View
9 railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -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
View
4 railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -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
View
2 railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
@@ -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? %>
@rubys
rubys added a note Dec 11, 2012

This results in double spaced entries for non-polymorphic attributes. (More precisely: lines consisting of exactly two spaces)

@rafaelfranca
Ruby on Rails member
rafaelfranca added a note Dec 11, 2012

Fixed @ 8dac976

Thank you again Mr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
<% 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
View
16 railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
@@ -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
View
5 railties/test/generators/model_generator_test.rb
@@ -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"
View
15 railties/test/generators/scaffold_controller_generator_test.rb
@@ -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

0 comments on commit b05b77b

Please sign in to comment.
Something went wrong with that request. Please try again.