Permalink
Browse files

Extract a attributes_names method to avoid duplication

  • Loading branch information...
1 parent b05b77b commit 80735ff4c059d634e5eb3a83ce6a0bb8bda5cd8e @rafaelfranca rafaelfranca committed Dec 9, 2012
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit 80735ff

Please sign in to comment.