Skip to content

Commit dd73f37

Browse files
dorianmariecomrafaelfranca
authored andcommitted
Rails::Generators::GeneratedAttribute#to_s
1 parent c676398 commit dd73f37

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

railties/lib/rails/generators/generated_attribute.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,31 @@ def options_for_migration
239239
end
240240
end
241241
end
242+
243+
def to_s
244+
if has_uniq_index?
245+
"#{name}:#{type}#{print_attribute_options}:uniq"
246+
elsif has_index?
247+
"#{name}:#{type}#{print_attribute_options}:index"
248+
else
249+
"#{name}:#{type}#{print_attribute_options}"
250+
end
251+
end
252+
253+
private
254+
def print_attribute_options
255+
if attr_options.empty?
256+
""
257+
elsif attr_options[:size]
258+
"{#{attr_options[:size]}}"
259+
elsif attr_options[:limit]
260+
"{#{attr_options[:limit]}}"
261+
elsif attr_options[:precision] && attr_options[:scale]
262+
"{#{attr_options[:precision]},#{attr_options[:scale]}}"
263+
else
264+
"{#{attr_options.keys.join(",")}}"
265+
end
266+
end
242267
end
243268
end
244269
end

railties/test/generators/generated_attribute_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,39 @@ def test_parse_required_attribute_with_index_false_when_belongs_to_required_by_d
228228
att = Rails::Generators::GeneratedAttribute.parse("supplier:references:index")
229229
assert_not_predicate att, :required?
230230
end
231+
232+
def test_generated_attribute_to_s
233+
att = Rails::Generators::GeneratedAttribute.parse("name")
234+
assert_equal "name:string", att.to_s
235+
end
236+
237+
def test_generated_attribute_to_s_with_index
238+
att = Rails::Generators::GeneratedAttribute.parse("name:index")
239+
assert_equal "name:string:index", att.to_s
240+
end
241+
242+
def test_generated_attribute_to_s_with_uniq_index
243+
att = Rails::Generators::GeneratedAttribute.parse("name:uniq")
244+
assert_equal "name:string:uniq", att.to_s
245+
end
246+
247+
def test_generated_attribute_to_s_with_limit
248+
att = Rails::Generators::GeneratedAttribute.parse("name:text{140}")
249+
assert_equal "name:text{140}", att.to_s
250+
end
251+
252+
def test_generated_attribute_to_s_with_size
253+
att = Rails::Generators::GeneratedAttribute.parse("name:text{medium}")
254+
assert_equal "name:text{medium}", att.to_s
255+
end
256+
257+
def test_generated_attribute_to_s_with_precision_and_scale
258+
att = Rails::Generators::GeneratedAttribute.parse("name:decimal{1,2}")
259+
assert_equal "name:decimal{1,2}", att.to_s
260+
end
261+
262+
def test_generated_attribute_to_s_with_polymorphic
263+
att = Rails::Generators::GeneratedAttribute.parse("name:references{polymorphic}")
264+
assert_equal "name:references{polymorphic}", att.to_s
265+
end
231266
end

0 commit comments

Comments
 (0)