Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added ability to specify :limit and :precision/:scale column options …

…in migration from cli
  • Loading branch information...
commit 4e47df7b889d4b3b42d741cb6c25b647e282778f 1 parent 12dcac7
@german authored
View
10 activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -2,17 +2,17 @@ class <%= migration_class_name %> < ActiveRecord::Migration
<%- if migration_action == 'add' -%>
def change
<% attributes.each do |attribute| -%>
- add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %>
- <% if attribute.has_index? -%>
+ add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
+ <%- if attribute.has_index? -%>
add_index :<%= table_name %>, :<%= attribute.name %>
- <% end %>
+ <%- end %>
<%- end -%>
end
<%- else -%>
def up
<% attributes.each do |attribute| -%>
<%- if migration_action -%>
- <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %>
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
<% if attribute.has_index? && migration_action == 'add' %>
add_index :<%= table_name %>, :<%= attribute.name %>
<% end -%>
@@ -23,7 +23,7 @@ def up
def down
<% attributes.reverse.each do |attribute| -%>
<%- if migration_action -%>
- <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %>
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
<%- end -%>
<%- end -%>
end
View
4 activerecord/lib/rails/generators/active_record/model/templates/migration.rb
@@ -2,7 +2,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
def change
create_table :<%= table_name %> do |t|
<% attributes.each do |attribute| -%>
- t.<%= attribute.type %> :<%= attribute.name %>
+ t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
<% end -%>
<% if options[:timestamps] %>
t.timestamps
@@ -14,7 +14,7 @@ def change
<% end -%>
<% end -%>
<% attributes.select {|attr| attr.has_index? }.each do |attribute| -%>
- add_index :<%= table_name %>, :<%= attribute.name %>
+ add_index :<%= table_name %>, :<%= attribute.name %>
<% end -%>
end
end
View
21 railties/lib/rails/generators/generated_attribute.rb
@@ -4,11 +4,11 @@
module Rails
module Generators
class GeneratedAttribute
- attr_accessor :name, :type, :has_index
+ attr_accessor :name, :type, :has_index, :attr_options
def initialize(name, type, has_index = false)
type = :string if type.blank?
- @name, @type, @has_index = name, type.to_sym, has_index.eql?("index")
+ @name, @type, @attr_options, @has_index = name, *parse_type_and_options(type), has_index.eql?("index")
end
def field_type
@@ -52,6 +52,23 @@ def reference?
def has_index?
@has_index
end
+
+ # parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals
+ # when declaring options square brackets should be used since bash interpreter fails when parentheses are used
+ def parse_type_and_options(type)
+ attribute_options = case type
+ when /(string|text|binary|integer)\[(\d+)\]/
+ {:limit => $2.to_i}
+ when /decimal\[(\d+)\.(\d+)\]/
+ {:precision => $1.to_i, :scale => $2.to_i}
+ else; {}
+ end
+ [type.to_s.gsub(/\[.*\]/,'').to_sym, attribute_options]
+ end
+
+ def inject_options
+ @attr_options.blank? ? '' : ", #{@attr_options.to_s.gsub(/[{}]/, '')}"
+ end
end
end
end
View
15 railties/test/generators/migration_generator_test.rb
@@ -97,6 +97,21 @@ def test_add_migration_with_attributes_without_type_and_index
end
end
+ def test_add_migration_with_attributes_index_declaration_and_attribute_options
+ migration = "add_title_and_content_to_books"
+ run_generator [migration, "title:string[40]:index", "content:string[255]", "price:decimal[5.2]:index"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :books, :title, :string, :limit=>40/, up)
+ assert_match(/add_column :books, :content, :string, :limit=>255/, up)
+ assert_match(/add_column :books, :price, :decimal, :precision=>5, :scale=>2/, up)
+ end
+ assert_match(/add_index :books, :title/, content)
+ assert_match(/add_index :books, :price/, content)
+ end
+ end
+
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
migration = "create_books"
run_generator [migration, "title:string", "content:text"]
View
15 railties/test/generators/model_generator_test.rb
@@ -159,6 +159,21 @@ def test_migration_with_missing_attribute_type_and_with_index
end
end
+ def test_add_migration_with_attributes_index_declaration_and_attribute_options
+ run_generator ["product", "title:string[40]:index", "content:string[255]", "price:decimal[5.2]:index"]
+
+ assert_migration "db/migrate/create_products.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/create_table :products/, up)
+ assert_match(/t.string :title, :limit=>40/, up)
+ assert_match(/t.string :content, :limit=>255/, up)
+ assert_match(/t.decimal :price, :precision=>5, :scale=>2/, up)
+ end
+ assert_match(/add_index :products, :title/, content)
+ assert_match(/add_index :products, :price/, content)
+ end
+ end
+
def test_migration_without_timestamps
ActiveRecord::Base.timestamped_migrations = false
run_generator ["account"]
Please sign in to comment.
Something went wrong with that request. Please try again.