Skip to content

Commit

Permalink
Added remaining methods to MigrationStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
tpett committed Mar 16, 2012
1 parent c0e23fb commit 3d47f9e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/undestroy/binding/active_record.rb
Expand Up @@ -18,11 +18,15 @@ def before_destroy(instance)
config.internals[:archive].new(:config => config, :source => instance).run
end

def prefix_table_name(name)
self.config.prefix.to_s + name.to_s
end

protected

def set_defaults
self.config.source_class = self.model
self.config.table_name ||= table_prefix + self.model.table_name if self.model.respond_to?(:table_name)
self.config.table_name ||= prefix_table_name(self.model.table_name) if self.model.respond_to?(:table_name)
self.config.target_class ||= create_target_class
ensure_is_ar! self.config.target_class
end
Expand All @@ -34,10 +38,6 @@ def create_target_class
end
end

def table_prefix
"archive_"
end

def ensure_is_ar!(klass)
raise ArgumentError, "#{klass.inspect} must be an ActiveRecord model" unless is_ar?(klass)
end
Expand Down
36 changes: 33 additions & 3 deletions lib/undestroy/binding/active_record/migration_statement.rb
Expand Up @@ -33,6 +33,13 @@ def config
end.first
end

def target_arguments
self.arguments.dup.tap do |args|
args[0] = target_table_name
args[1] = binding.prefix_table_name(args[1]) if rename_table?
end
end

def schema_action?
SCHEMA.include?(method_name)
end
Expand All @@ -44,11 +51,34 @@ def index_action?
def run?
(
arguments.present? &&
config &&
config.migrate &&
schema_action?
config && config.migrate &&
!rename_table_exception? &&
(
schema_action? ||
index_action? && config.indexes
)
)
end

def run!(callable)
callable.call(method_name, *target_arguments, &block)
end

protected

# We don't want to run rename_table on the target when the table name is
# explicitly set in the configuration. The user must do manual migrating
# in that case.
def rename_table_exception?
rename_table? && config.table_name
end

def rename_table?
method_name == :rename_table
end

def binding
config.source_class.undestroy_model_binding
end
end

3 changes: 2 additions & 1 deletion lib/undestroy/config.rb
@@ -1,13 +1,14 @@
class Undestroy::Config
OPTIONS = [
:table_name, :abstract_class, :fields, :migrate, :indexes,
:table_name, :abstract_class, :fields, :migrate, :indexes, :prefix,
:source_class, :target_class, :internals
]
attr_accessor *OPTIONS

def initialize(options={})
self.indexes = false
self.migrate = true
self.prefix = "archive_"
self.fields = {}
self.internals = {
:archive => Undestroy::Archive,
Expand Down
72 changes: 63 additions & 9 deletions test/unit/binding/active_record/migration_statement_test.rb
Expand Up @@ -157,27 +157,81 @@ class RunQueryMethod < Base
assert_not obj.run?
end

should "return true if :index is configured and index_action? is true"
should "return false if :index is configured and index_action? is false"
should "return false if :index is not configured for this table"
should "return true if :index is configured and index_action? is true" do
@source_class.table_name = 'bar'
@source_class.undestroy :indexes => true
obj = subject.new :add_index, :bar
assert obj.index_action?
assert obj.run?
end

should "return false if :index is configured and index_action? is false" do
@source_class.table_name = 'bar'
@source_class.undestroy :indexes => true
obj = subject.new :method, :bar
assert_not obj.index_action?
assert_not obj.run?
end

should "return false if :index is not configured for this table" do
@source_class.table_name = 'bar'
@source_class.undestroy
obj = subject.new :add_index, :bar
assert obj.index_action?
assert_not obj.run?
end

# We will not rename a table that has been configured to a specific name
should "return false if :method_name is rename_table and :table_name configuration is set explicitly"
should "return false if :method_name is rename_table and :table_name configuration is set explicitly" do
@source_class.table_name = 'bar'
@source_class.undestroy :table_name => 'old_bar'
obj = subject.new :rename_table, :bar, :baz
assert_not obj.run?
end
end

class TargetArgsMethod < Base
desc 'target_arguments method'

should "substitute source table_name for target table_name"
# TODO: Figure out how to make this know the correct table names
should "substitute arg[1] for target table_name on rename_table method"
setup do
@source_class.table_name = 'source'
@source_class.undestroy
end

should "leave original arguments alone" do
obj = subject.new :add_column, :source, :foo, :string
args = obj.arguments.dup
obj.target_arguments
assert_equal args, obj.arguments
end

should "substitute source table_name for target table_name" do
obj = subject.new :add_column, :source, :foo, :string
assert_equal ['archive_source', :foo, :string], obj.target_arguments
end

should "substitute arg[1] for target table_name on rename_table method" do
obj = subject.new :rename_table, :source, :new_source
assert_equal ['archive_source', 'archive_new_source'], obj.target_arguments
end
end

class RunBangMethod < Base
desc 'run! method'

should "accept callable argument to run on"
should "call the method with (method_name, target_args, block)"
setup do
@source_class.table_name = 'source'
@source_class.undestroy
end

should "accept callable argument to run on and call with (method_name, target_args, block)" do
called = false
callable = proc { |*args, &block| called = [args, block] }
block = proc { }
obj = subject.new :add_column, :source, :foo, :string, &block
obj.run!(callable)
assert_equal [obj.method_name, obj.target_arguments, block].flatten, called.flatten
end
end

end
Expand Down
16 changes: 13 additions & 3 deletions test/unit/binding/active_record_test.rb
Expand Up @@ -132,10 +132,10 @@ class InitMethod < Base
assert_equal @model, binding.config.source_class
end

should "default :table_name to 'archive_{source.table_name}'" do
should "default :table_name to '{config.prefix}{source.table_name}'" do
@model.table_name = :foobar
binding = subject.new(@model)
assert_equal 'archive_foobar', binding.config.table_name
binding = subject.new(@model, :prefix => "prefix_archive_")
assert_equal 'prefix_archive_foobar', binding.config.table_name
end

should "create a target_class if none provided" do
Expand Down Expand Up @@ -191,5 +191,15 @@ class BeforeDestroy < Base
end
end

class PrefixTableNameMethod < Base
desc 'prefix_table_name method'
subject { @binding ||= Undestroy::Binding::ActiveRecord.new(@model) }

should "return {config.prefix}{source.table_name}" do
subject.config.prefix = "archive_prefix_"
assert_equal "archive_prefix_foo", subject.prefix_table_name("foo")
end
end

end

7 changes: 6 additions & 1 deletion test/unit/config_test.rb
Expand Up @@ -87,7 +87,7 @@ class BasicInstance < Base
desc 'basic instance'
subject { Undestroy::Config.new }

should have_accessors :table_name, :abstract_class, :fields, :migrate, :indexes
should have_accessors :table_name, :abstract_class, :fields, :migrate, :indexes, :prefix
should have_accessors :source_class, :target_class, :internals
end

Expand Down Expand Up @@ -120,6 +120,11 @@ class InitMethod < Base
assert_equal false, config.indexes
end

should "default prefix to 'archive_'" do
config = subject.new
assert_equal 'archive_', config.prefix
end

should "set config options using provided hash" do
config = subject.new :table_name => "foo",
:abstract_class => "test_archive",
Expand Down

0 comments on commit 3d47f9e

Please sign in to comment.