Permalink
Browse files

updates for rails 3. all specs are currently green. still need to inv…

…estigate hatbm associations and migrations.
  • Loading branch information...
mpd
mpd committed Sep 25, 2010
1 parent e4c84ab commit c8dc158fe42516c05c4a5ab58518c078494fb7b8
View
@@ -4,4 +4,6 @@
coverage
pkg
spec/db/log/*.log
-tmp
+tmp
+.bundle/
+.idea/
View
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+gemspec
View
@@ -0,0 +1,19 @@
+PATH
+ remote: .
+ specs:
+ acts_as_archive (0.3.0)
+ require (= 0.2.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ mysql (2.8.1)
+ require (0.2.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ acts_as_archive!
+ mysql
+ require (= 0.2.1)
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{acts_as_archive}
- s.version = "0.2.5"
+ s.version = "0.3.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Winton Welsh"]
@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.5}
s.summary = %q{Don't delete your records, move them to a different table}
+ s.add_development_dependency 'mysql'
+
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
View
0 bin/acts_as_archive 100644 → 100755
No changes.
View
@@ -1,8 +1,10 @@
require File.expand_path("#{File.dirname(__FILE__)}/../require")
Require.lib!
+ActiveRecord::Base.send(:include, ActsAsArchive::Base)
+ActiveRecord::Migration.send(:include, ActsAsArchive::Migration)
+
module ActsAsArchive
-
def self.update(*models)
models.each do |klass|
if klass.respond_to?(:acts_as_archive?) && klass.acts_as_archive?
@@ -16,4 +18,4 @@ def self.update(*models)
end
end
end
-end
+end
@@ -5,19 +5,29 @@ module Destroy
def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.class_eval do
- alias_method :destroy_without_callbacks!, :destroy_without_callbacks
- class <<self
+ alias_method :delete!, :delete
+
+ class << self
alias_method :delete_all!, :delete_all
end
+
+ before_destroy Proc.new { |r|
+ unless r.new_record?
+ r.class.copy_to_archive("#{r.class.primary_key} = #{r.id}", false, false)
+ end
+ }
+
end
- base.send :extend, ClassMethods
+
base.send :include, InstanceMethods
+ base.send :extend, ClassMethods
end
end
module ClassMethods
- def copy_to_archive(conditions, import=false)
- add_conditions!(where = '', conditions)
+ def copy_to_archive(conditions, import = false, delete = true)
+ where = sanitize_sql(conditions)
+ where = "WHERE #{where}" unless where.blank?
insert_cols = column_names.clone
select_cols = column_names.clone
if insert_cols.include?('deleted_at')
@@ -38,7 +48,7 @@ def copy_to_archive(conditions, import=false)
FROM #{table_name}
#{where}
})
- connection.execute("DELETE FROM #{table_name} #{where}")
+ connection.execute("DELETE FROM #{table_name} #{where}") if delete
end
def delete_all(conditions=nil)
@@ -47,23 +57,16 @@ def delete_all(conditions=nil)
end
module InstanceMethods
- def destroy_without_callbacks
+ def delete
unless new_record?
self.class.copy_to_archive("#{self.class.primary_key} = #{id}")
end
@destroyed = true
freeze
end
-
+
def destroy!
- transaction { destroy_with_callbacks! }
- end
-
- def destroy_with_callbacks!
- return false if callback(:before_destroy) == false
- result = destroy_without_callbacks!
- callback(:after_destroy)
- result
+ transaction { delete! }
end
end
end
@@ -12,7 +12,8 @@ def self.included(base)
module ClassMethods
def copy_from_archive(conditions)
- add_conditions!(where = '', conditions)
+ where = sanitize_sql(conditions)
+ where = "WHERE #{where}" unless where.blank?
col_names = column_names - [ 'deleted_at' ]
col_names.map! { |col| connection.quote_column_name(col) }
connection.execute(%{
View
@@ -1,5 +0,0 @@
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.rails_init!
-
-ActiveRecord::Base.send(:include, ActsAsArchive::Base)
-ActiveRecord::Migration.send(:include, ActsAsArchive::Migration)
View
@@ -3,7 +3,7 @@
require 'require'
Require do
- gem(:activerecord) { require 'active_record' }
+ gem(:activerecord, '~> 3.0') { require 'active_record' }
gem :require, '=0.2.1'
gem(:rake, '=0.8.7') { require 'rake' }
gem :rspec, '=1.3.0'
@@ -44,6 +44,6 @@
require 'logger'
require 'yaml'
require 'pp'
- require 'rails/init'
+ require 'acts_as_archive'
end
-end
+end
@@ -14,7 +14,11 @@
end
it "should really delete all records" do
+ Article.count.should == 5
+ Article::Archive.count.should == 0
+
Article.delete_all!
+
Article.count.should == 0
Article::Archive.count.should == 0
end
@@ -28,9 +32,10 @@
@article = Article.first
end
- it "should really destroy a records" do
- @article.destroy!
- Article::Archive.count.should == 0
+ it "should really destroy a record" do
+ lambda {
+ @article.destroy!
+ }.should_not change(Article::Archive, :count)
end
end

0 comments on commit c8dc158

Please sign in to comment.