Skip to content

Commit

Permalink
added ability to specify :limit and :precision/:scale column options …
Browse files Browse the repository at this point in the history
…in migration from cli
  • Loading branch information
Dmitrii Samoilov committed Sep 2, 2011
1 parent 12dcac7 commit 4e47df7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
21 changes: 19 additions & 2 deletions railties/lib/rails/generators/generated_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
15 changes: 15 additions & 0 deletions railties/test/generators/model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 4e47df7

Please sign in to comment.