Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add GeneratedAttribute#column_name to get the name of the column in the

database
  • Loading branch information...
commit 95ccbd847da149b194cbf423e04e3d19a812dce4 1 parent 80735ff
@rafaelfranca rafaelfranca authored
View
10 railties/lib/rails/generators/generated_attribute.rb
@@ -99,13 +99,17 @@ def human_name
end
def index_name
- @index_name ||= if reference?
- polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id"
+ @index_name ||= if polymorphic?
+ %w(id type).map { |t| "#{name}_#{t}" }
else
- name
+ column_name
end
end
+ def column_name
+ @column_name ||= reference? ? "#{name}_id" : name
+ end
+
def foreign_key?
!!(name =~ /_id$/)
end
View
2  railties/lib/rails/generators/named_base.rb
@@ -162,7 +162,7 @@ def parse_attributes! #:nodoc:
def attributes_names
@attributes_names ||= attributes.each_with_object([]) do |a, names|
- names << (a.reference? ? "#{a.name}_id" : a.name)
+ names << a.column_name
names << "#{a.name}_type" if a.polymorphic?
end
end
View
4 railties/lib/rails/generators/test_unit/model/templates/fixtures.yml
@@ -3,13 +3,13 @@
<% unless attributes.empty? -%>
one:
<% attributes.each do |attribute| -%>
- <%= attribute.reference? ? "#{attribute.name}_id" : attribute.name %>: <%= attribute.default %>
+ <%= attribute.column_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.column_name %>: <%= attribute.default %>
<%= "#{attribute.name}_type: #{attribute.human_name}" if attribute.polymorphic? %>
<% end -%>
<% else -%>
View
11 railties/test/generators/generated_attribute_test.rb
@@ -117,13 +117,13 @@ def test_polymorphic_reference_is_true
assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
end
end
-
+
def test_polymorphic_reference_is_false
%w(foo bar baz).each do |attribute_type|
assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic?
end
end
-
+
def test_blank_type_defaults_to_string_raises_exception
assert_equal :string, create_generated_attribute(nil, 'title').type
assert_equal :string, create_generated_attribute("", 'title').type
@@ -132,6 +132,13 @@ def test_blank_type_defaults_to_string_raises_exception
def test_handles_index_names_for_references
assert_equal "post", create_generated_attribute('string', 'post').index_name
assert_equal "post_id", create_generated_attribute('references', 'post').index_name
+ assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name
assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name
end
+
+ def test_handles_index_names_for_references
+ assert_equal "post", create_generated_attribute('string', 'post').column_name
+ assert_equal "post_id", create_generated_attribute('references', 'post').column_name
+ assert_equal "post_id", create_generated_attribute('belongs_to', 'post').column_name
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.