From 8b2f801ed8690dcbc61d62e6b3518efaac70a4a4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 17 Nov 2010 12:53:38 -0800 Subject: converted migrations to support instance methods --- activerecord/lib/active_record/migration.rb | 40 ++++++++++++++++++++++++----- activerecord/lib/active_record/schema.rb | 6 ++--- 2 files changed, 36 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 2c306d233a..4a1a84b74b 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -287,10 +287,30 @@ module ActiveRecord # In application.rb. # class Migration - @@verbose = true - cattr_accessor :verbose - class << self + attr_accessor :verbose + attr_accessor :delegate # :nodoc: + def method_missing(name, *args, &block) # :nodoc: + (delegate || superclass.delegate).send(name, *args, &block) + end + end + self.delegate = new + self.verbose = true + + def name + self.class.name + end + + def up + self.class.delegate = self + self.class.up + end + + def down + self.class.delegate = self + self.class.down + end + def up_with_benchmarks #:nodoc: migrate(:up) end @@ -309,7 +329,7 @@ module ActiveRecord end result = nil - time = Benchmark.measure { result = send("#{direction}_without_benchmarks") } + time = Benchmark.measure { result = send("#{direction}") } case direction when :up then announce "migrated (%.4fs)" % time.real; write @@ -380,10 +400,19 @@ module ActiveRecord unless arguments.empty? || method == :execute arguments[0] = Migrator.proper_table_name(arguments.first) end + return super unless connection.respond_to?(method) connection.send(method, *arguments, &block) end end + def verbose + self.class.verbose + end + + def verbose= verbosity + self.class.verbose = verbosity + end + def copy(destination, sources, options = {}) copied = [] @@ -425,7 +454,6 @@ module ActiveRecord "%.3d" % number end end - end end # MigrationProxy is used to defer loading of the actual migration classes @@ -451,7 +479,7 @@ module ActiveRecord def load_migration require(File.expand_path(filename)) - name.constantize + name.constantize.new end end diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index c1bc3214ea..a621248cc3 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -30,9 +30,7 @@ module ActiveRecord # ActiveRecord::Schema is only supported by database adapters that also # support migrations, the two features being very similar. class Schema < Migration - private_class_method :new - - def self.migrations_path + def migrations_path ActiveRecord::Migrator.migrations_path end @@ -48,7 +46,7 @@ module ActiveRecord # ... # end def self.define(info={}, &block) - instance_eval(&block) + new.instance_eval(&block) unless info[:version].blank? initialize_schema_migrations_table -- cgit v1.2.3