aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-18 13:44:32 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-18 16:21:11 +0100
commit9fffdc5cdb80b1824473a6d7ae1fedf9e74aa748 (patch)
tree88f429511bf65f1104efeab10d6310b68ab24837 /activerecord
parente75ea474346e74e36d92febd47985c3571b1472b (diff)
downloadrails-9fffdc5cdb80b1824473a6d7ae1fedf9e74aa748.tar.gz
rails-9fffdc5cdb80b1824473a6d7ae1fedf9e74aa748.tar.bz2
rails-9fffdc5cdb80b1824473a6d7ae1fedf9e74aa748.zip
Generators load path now will be Ruby load path. If you want to use rspec:install generator, you need generators/rspec/install_generator in your load path.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/generators/active_record.rb31
-rw-r--r--activerecord/lib/generators/active_record/migration/migration_generator.rb25
-rw-r--r--activerecord/lib/generators/active_record/migration/templates/migration.rb11
-rw-r--r--activerecord/lib/generators/active_record/model/model_generator.rb33
-rw-r--r--activerecord/lib/generators/active_record/model/templates/migration.rb16
-rw-r--r--activerecord/lib/generators/active_record/model/templates/model.rb5
-rw-r--r--activerecord/lib/generators/active_record/observer/observer_generator.rb15
-rw-r--r--activerecord/lib/generators/active_record/observer/templates/observer.rb2
-rw-r--r--activerecord/lib/generators/active_record/session_migration/session_migration_generator.rb24
-rw-r--r--activerecord/lib/generators/active_record/session_migration/templates/migration.rb16
10 files changed, 178 insertions, 0 deletions
diff --git a/activerecord/lib/generators/active_record.rb b/activerecord/lib/generators/active_record.rb
new file mode 100644
index 0000000000..25b982f296
--- /dev/null
+++ b/activerecord/lib/generators/active_record.rb
@@ -0,0 +1,31 @@
+require 'rails/generators/named_base'
+require 'rails/generators/migration'
+require 'rails/generators/active_model'
+require 'active_record'
+
+module ActiveRecord
+ module Generators
+ class Base < Rails::Generators::NamedBase #:nodoc:
+ include Rails::Generators::Migration
+
+ def self.source_root
+ @_ar_source_root ||= begin
+ if base_name && generator_name
+ File.expand_path(File.join(base_name, generator_name, 'templates'), File.dirname(__FILE__))
+ end
+ end
+ end
+
+ protected
+ # Implement the required interface for Rails::Generators::Migration.
+ #
+ def next_migration_number(dirname) #:nodoc:
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+ end
+ end
+end
diff --git a/activerecord/lib/generators/active_record/migration/migration_generator.rb b/activerecord/lib/generators/active_record/migration/migration_generator.rb
new file mode 100644
index 0000000000..7939977f72
--- /dev/null
+++ b/activerecord/lib/generators/active_record/migration/migration_generator.rb
@@ -0,0 +1,25 @@
+require 'generators/active_record'
+
+module ActiveRecord
+ module Generators
+ class MigrationGenerator < Base
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ def create_migration_file
+ set_local_assigns!
+ migration_template "migration.rb", "db/migrate/#{file_name}.rb"
+ end
+
+ protected
+ attr_reader :migration_action
+
+ def set_local_assigns!
+ if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
+ @migration_action = $1
+ @table_name = $2.pluralize
+ end
+ end
+
+ end
+ end
+end
diff --git a/activerecord/lib/generators/active_record/migration/templates/migration.rb b/activerecord/lib/generators/active_record/migration/templates/migration.rb
new file mode 100644
index 0000000000..bbb7c53d86
--- /dev/null
+++ b/activerecord/lib/generators/active_record/migration/templates/migration.rb
@@ -0,0 +1,11 @@
+class <%= migration_class_name %> < ActiveRecord::Migration
+ def self.up<% attributes.each do |attribute| %>
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%>
+ <%- end %>
+ end
+
+ def self.down<% attributes.reverse.each do |attribute| %>
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%>
+ <%- end %>
+ end
+end
diff --git a/activerecord/lib/generators/active_record/model/model_generator.rb b/activerecord/lib/generators/active_record/model/model_generator.rb
new file mode 100644
index 0000000000..2641083e0d
--- /dev/null
+++ b/activerecord/lib/generators/active_record/model/model_generator.rb
@@ -0,0 +1,33 @@
+require 'generators/active_record'
+
+module ActiveRecord
+ module Generators
+ class ModelGenerator < Base
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
+
+ check_class_collision
+
+ class_option :migration, :type => :boolean
+ class_option :timestamps, :type => :boolean
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
+
+ def create_migration_file
+ return unless options[:migration] && options[:parent].nil?
+ migration_template "migration.rb", "db/migrate/create_#{table_name}.rb"
+ end
+
+ def create_model_file
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
+ end
+
+ hook_for :test_framework
+
+ protected
+
+ def parent_class_name
+ options[:parent] || "ActiveRecord::Base"
+ end
+
+ end
+ end
+end
diff --git a/activerecord/lib/generators/active_record/model/templates/migration.rb b/activerecord/lib/generators/active_record/model/templates/migration.rb
new file mode 100644
index 0000000000..1f68487304
--- /dev/null
+++ b/activerecord/lib/generators/active_record/model/templates/migration.rb
@@ -0,0 +1,16 @@
+class <%= migration_class_name %> < ActiveRecord::Migration
+ def self.up
+ create_table :<%= table_name %> do |t|
+<% for attribute in attributes -%>
+ t.<%= attribute.type %> :<%= attribute.name %>
+<% end -%>
+<% if options[:timestamps] %>
+ t.timestamps
+<% end -%>
+ end
+ end
+
+ def self.down
+ drop_table :<%= table_name %>
+ end
+end
diff --git a/activerecord/lib/generators/active_record/model/templates/model.rb b/activerecord/lib/generators/active_record/model/templates/model.rb
new file mode 100644
index 0000000000..21ae29e9f2
--- /dev/null
+++ b/activerecord/lib/generators/active_record/model/templates/model.rb
@@ -0,0 +1,5 @@
+class <%= class_name %> < <%= parent_class_name.classify %>
+<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
+ belongs_to :<%= attribute.name %>
+<% end -%>
+end
diff --git a/activerecord/lib/generators/active_record/observer/observer_generator.rb b/activerecord/lib/generators/active_record/observer/observer_generator.rb
new file mode 100644
index 0000000000..a6b57423b8
--- /dev/null
+++ b/activerecord/lib/generators/active_record/observer/observer_generator.rb
@@ -0,0 +1,15 @@
+require 'generators/active_record'
+
+module ActiveRecord
+ module Generators
+ class ObserverGenerator < Base
+ check_class_collision :suffix => "Observer"
+
+ def create_observer_file
+ template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
+ end
+
+ hook_for :test_framework
+ end
+ end
+end
diff --git a/activerecord/lib/generators/active_record/observer/templates/observer.rb b/activerecord/lib/generators/active_record/observer/templates/observer.rb
new file mode 100644
index 0000000000..b9a3004161
--- /dev/null
+++ b/activerecord/lib/generators/active_record/observer/templates/observer.rb
@@ -0,0 +1,2 @@
+class <%= class_name %>Observer < ActiveRecord::Observer
+end
diff --git a/activerecord/lib/generators/active_record/session_migration/session_migration_generator.rb b/activerecord/lib/generators/active_record/session_migration/session_migration_generator.rb
new file mode 100644
index 0000000000..59c4792066
--- /dev/null
+++ b/activerecord/lib/generators/active_record/session_migration/session_migration_generator.rb
@@ -0,0 +1,24 @@
+require 'generators/active_record'
+
+module ActiveRecord
+ module Generators
+ class SessionMigrationGenerator < Base
+ argument :name, :type => :string, :default => "add_sessions_table"
+
+ def create_migration_file
+ migration_template "migration.rb", "db/migrate/#{file_name}.rb"
+ end
+
+ protected
+
+ def session_table_name
+ current_table_name = ActiveRecord::SessionStore::Session.table_name
+ if ["sessions", "session"].include?(current_table_name)
+ current_table_name = (ActiveRecord::Base.pluralize_table_names ? 'session'.pluralize : 'session')
+ end
+ current_table_name
+ end
+
+ end
+ end
+end
diff --git a/activerecord/lib/generators/active_record/session_migration/templates/migration.rb b/activerecord/lib/generators/active_record/session_migration/templates/migration.rb
new file mode 100644
index 0000000000..919822af7b
--- /dev/null
+++ b/activerecord/lib/generators/active_record/session_migration/templates/migration.rb
@@ -0,0 +1,16 @@
+class <%= migration_class_name %> < ActiveRecord::Migration
+ def self.up
+ create_table :<%= session_table_name %> do |t|
+ t.string :session_id, :null => false
+ t.text :data
+ t.timestamps
+ end
+
+ add_index :<%= session_table_name %>, :session_id
+ add_index :<%= session_table_name %>, :updated_at
+ end
+
+ def self.down
+ drop_table :<%= session_table_name %>
+ end
+end