Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract a attributes_names method to avoid duplication

  • Loading branch information...
commit 80735ff4c059d634e5eb3a83ce6a0bb8bda5cd8e 1 parent b05b77b
@rafaelfranca rafaelfranca authored
View
7 railties/lib/rails/generators/named_base.rb
@@ -160,6 +160,13 @@ def parse_attributes! #:nodoc:
end
end
+ def attributes_names
+ @attributes_names ||= attributes.each_with_object([]) do |a, names|
+ names << (a.reference? ? "#{a.name}_id" : a.name)
+ names << "#{a.name}_type" if a.polymorphic?
+ end
+ end
+
def pluralize_table_names?
!defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
end
View
9 railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -22,15 +22,6 @@ 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
2  railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -94,7 +94,7 @@ def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params[<%= ":#{singular_table_name}" %>]
<%- else -%>
- params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.join(', ') %>)
+ params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
<%- end -%>
end
end
View
16 railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
@@ -18,19 +18,11 @@ def create_test_files
private
def attributes_hash
- return if attributes.empty?
+ return if attributes_names.empty?
- 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}"
+ attributes_names.map do |name|
+ "#{name}: @#{singular_table_name}.#{name}"
+ end.sort.join(', ')
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.