Permalink
Browse files

added ability to specify from cli when generating a model/migration w…

…hether particular property should be an index like this 'rails g model person name:string:index profile:string'
  • Loading branch information...
1 parent 22e611e commit 3d2eed7679fe63415ec687e5f41d6b780aa75983 Dmitrii Samoilov committed Aug 17, 2011
@@ -3,7 +3,7 @@
module ActiveRecord
module Generators
class MigrationGenerator < Base
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index"
def create_migration_file
set_local_assigns!
@@ -3,13 +3,19 @@ class <%= migration_class_name %> < ActiveRecord::Migration
def change
<% attributes.each do |attribute| -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %>
+ <% if attribute.has_index? -%>
+ add_index :<%= table_name %>, :<%= attribute.name %>
+ <% 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 %>
+ <% if attribute.has_index? && migration_action == 'add' %>
+ add_index :<%= table_name %>, :<%= attribute.name %>
+ <% end -%>
<%- end -%>
<%- end -%>
end
@@ -3,7 +3,7 @@
module ActiveRecord
module Generators
class ModelGenerator < Base
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index"
check_class_collision
@@ -12,6 +12,9 @@ def change
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.name %>_id
<% end -%>
+<% end -%>
+<% attributes.select {|attr| attr.has_index? }.each do |attribute| -%>
+ add_index :<%= table_name %>, :<%= attribute.name %>
<% end -%>
end
end
@@ -4,11 +4,11 @@
module Rails
module Generators
class GeneratedAttribute
- attr_accessor :name, :type
+ attr_accessor :name, :type, :has_index
- def initialize(name, type)
+ def initialize(name, type, has_index = false)
type = :string if type.blank?
- @name, @type = name, type.to_sym
+ @name, @type, @has_index = name, type.to_sym, has_index.eql?("index")
end
def field_type
@@ -48,6 +48,10 @@ def human_name
def reference?
self.type.in?([:references, :belongs_to])
end
+
+ def has_index?
+ @has_index
+ end
end
end
end
@@ -154,8 +154,8 @@ def assign_names!(name) #:nodoc:
# Convert attributes array into GeneratedAttribute objects.
def parse_attributes! #:nodoc:
self.attributes = (attributes || []).map do |key_value|
- name, type = key_value.split(':')
- Rails::Generators::GeneratedAttribute.new(name, type)
+ name, type, has_index = key_value.split(':')
+ Rails::Generators::GeneratedAttribute.new(name, type, has_index)
end
end
@@ -1,7 +1,7 @@
module Rails
module Generators
class MigrationGenerator < NamedBase #metagenerator
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index"
hook_for :orm, :required => true
end
end
@@ -1,7 +1,7 @@
module Rails
module Generators
class ModelGenerator < NamedBase #metagenerator
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type field:type:index"
hook_for :orm, :required => true
end
end
@@ -58,6 +58,32 @@ def test_remove_migration_with_attributes
end
end
+ def test_add_migration_with_attributes_and_indices
+ migration = "add_title_with_index_and_body_to_posts"
+ run_generator [migration, "title:string:index", "body:text"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :posts, :title, :string/, up)
+ assert_match(/add_column :posts, :body, :text/, up)
+ end
+ assert_match(/add_index :posts, :title/, content)
+ end
+ end
+
+ def test_add_migration_with_attributes_and_wrong_index_declaration
+ migration = "add_title_and_content_to_books"
+ run_generator [migration, "title:string:inex", "content:text"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |up|
+ assert_match(/add_column :books, :title, :string/, up)
+ assert_match(/add_column :books, :content, :text/, up)
+ end
+ assert_not_match(/add_index :books, :title/, 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"]
@@ -113,6 +113,36 @@ def test_migration_with_attributes
end
end
+ def test_migration_with_attributes_and_with_index
+ run_generator ["product", "name:string:index", "supplier_id:integer:index"]
+
+ assert_migration "db/migrate/create_products.rb" do |m|
+ assert_method :change, m do |up|
+ assert_match(/create_table :products/, up)
+ assert_match(/t\.string :name/, up)
+ assert_match(/t\.integer :supplier_id/, up)
+
+ assert_match(/add_index :products, :name/, up)
+ assert_match(/add_index :products, :supplier_id/, up)
+ end
+ end
+ end
+
+ def test_migration_with_attributes_and_with_wrong_index_declaration
+ run_generator ["product", "name:string", "supplier_id:integer:inex"]
+
+ assert_migration "db/migrate/create_products.rb" do |m|
+ assert_method :change, m do |up|
+ assert_match(/create_table :products/, up)
+ assert_match(/t\.string :name/, up)
+ assert_match(/t\.integer :supplier_id/, up)
+
+ assert_not_match(/add_index :products, :name/, up)
+ assert_not_match(/add_index :products, :supplier_id/, up)
+ end
+ end
+ end
+
def test_migration_without_timestamps
ActiveRecord::Base.timestamped_migrations = false
run_generator ["account"]

0 comments on commit 3d2eed7

Please sign in to comment.