Permalink
Browse files

merge with winton

  • Loading branch information...
2 parents 31e9a07 + 40e3933 commit 30280a2eb5f887bdf8dda905d3bf8b9de13e4dff @look look committed Feb 8, 2010
View
7 .gitignore
@@ -1,6 +1,7 @@
.DS_Store
+*.gem
+*.gemspec
coverage
pkg
-tmp
-
-spec/db/log/*.log
+spec/db/log/*.log
+tmp
View
34 README.markdown
@@ -5,16 +5,11 @@ Don't delete your records, move them to a different table.
Like <code>acts\_as\_paranoid</code>, but doesn't mess with your SQL queries.
-Compatibility
--------------
-
-Tested with Ruby 1.8.6, 1.8.7, and 1.9.1.
-
Install
-------
<pre>
-sudo gem install acts_as_archive --source http://gemcutter.org
+sudo gem install acts_as_archive
</pre>
**environment.rb**:
@@ -34,23 +29,22 @@ class Article < ActiveRecord::Base
end
</pre>
-<a name="run_acts_as_archive"></a>
+<a name="create_archive_tables"></a>
-Run acts\_as\_archive
+Create archive tables
---------------------
-Terminal:
+Add this line to a migration:
<pre>
-cd your_rails_app
-acts_as_archive Article
+ActsAsArchive.update Article, Comment
</pre>
-This command creates your archive tables (<code>archived_articles</code> as per the example).
+Replace <code>Article, Comment</code> with your own models that use <code>acts_as_archive</code>.
Archive tables mirror your table's structure, but with an additional <code>deleted_at</code> column.
-Run this command every time you add <code>acts\_as\_archive</code> to a new model.
+There is an [alternate way to create archive tables](http://wiki.github.com/winton/acts_as_archive/alternatives-to-migrations) if you don't like migrations.
That's it!
----------
@@ -62,7 +56,9 @@ Records move into the archive table instead of being destroyed.
What if my schema changes?
--------------------------
-Any new migrations on your table are automatically applied to the archive table.
+New migrations are automatically applied to the archive table.
+
+No action is necessary on your part.
Query the archive
-----------------
@@ -85,9 +81,9 @@ Article.restore_all([ 'id = ?', 1 ])
Auto-migrate from acts\_as\_paranoid
------------------------------------
-If you previously used <code>acts\_as\_paranoid</code>, running the <code>acts\_as\_archive</code>
-command will automatically move your deleted records to the archive table
-(see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
+If you previously used <code>acts\_as\_paranoid</code>, the <code>ActsAsArchive.update</code>
+call will automatically move your deleted records to the archive table
+(see <a href="#create_archive_tables">_Create archive tables_</a>).
Original <code>deleted_at</code> values are preserved.
@@ -104,5 +100,5 @@ class Article < ActiveRecord::Base
end
</pre>
-Run the <code>acts\_as\_archive</code> command any time you add new indexes
-(see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
+Call <code>ActsAsArchive.update</code> upon adding new indexes
+(see <a href="#create_archive_tables">_Create archive tables_</a>).
View
51 Rakefile
@@ -1,49 +1,2 @@
-require 'rubygems'
-require 'rake'
-require 'rake/gempackagetask'
-require 'spec/rake/spectask'
-
-GEM_NAME = 'acts_as_archive'
-PKG_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*', 'spec/db/log/*.log']
-
-spec = Gem::Specification.new do |s|
- s.author = "Winton Welsh"
- s.email = "mail@wintoni.us"
- s.executables << GEM_NAME
- s.extra_rdoc_files = [ "README.markdown" ]
- s.files = PKG_FILES.to_a
- s.homepage = "http://github.com/winton/#{GEM_NAME}"
- s.name = GEM_NAME
- s.platform = Gem::Platform::RUBY
- s.require_path = "lib"
- s.summary = "Don't delete your records, move them to a different table"
- s.version = "0.1.6"
-end
-
-desc "Package gem"
-Rake::GemPackageTask.new(spec) do |pkg|
- pkg.gem_spec = spec
-end
-
-desc "Install gem"
-task :install do
- Rake::Task['gem'].invoke
- `gem uninstall #{GEM_NAME} -x`
- `gem install pkg/#{GEM_NAME}*.gem`
- `rm -Rf pkg`
-end
-
-desc "Generate gemspec"
-task :gemspec do
- File.open("#{File.dirname(__FILE__)}/#{GEM_NAME}.gemspec", 'w') do |f|
- f.write(spec.to_ruby)
- end
-end
-
-desc "Run specs"
-Spec::Rake::SpecTask.new do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.spec_files = FileList["spec/**/*_spec.rb"]
-end
-
-task :default => :spec
+require "#{File.dirname(__FILE__)}/require"
+Require.rakefile!
View
3 bin/acts_as_archive
@@ -1,3 +1,2 @@
#!/usr/bin/env ruby
-
-puts `script/runner "ActsAsArchive.update #{ARGV.join ', '}"`
+puts `script/runner "ActsAsArchive.update #{ARGV.join ', '}"`
View
21 lib/acts_as_archive.rb
@@ -1,2 +1,19 @@
-require File.dirname(__FILE__) + "/acts_as_archive/base"
-require File.dirname(__FILE__) + "/acts_as_archive/migration"
+require File.expand_path("#{File.dirname(__FILE__)}/../require")
+Require.lib!
+
+module ActsAsArchive
+
+ def self.update(*models)
+ models.each do |klass|
+ if klass.respond_to?(:acts_as_archive?) && klass.acts_as_archive?
+ time = Benchmark.measure do
+ klass.create_archive_table
+ klass.migrate_from_acts_as_paranoid
+ klass.create_archive_indexes
+ end
+ $stdout.puts "-- ActsAsArchive.update(#{models.join(', ')})"
+ $stdout.puts " -> #{"%.4fs" % time.real}"
+ end
+ end
+ end
+end
View
15 lib/acts_as_archive/base.rb
@@ -1,20 +1,5 @@
-require File.dirname(__FILE__) + "/base/destroy"
-require File.dirname(__FILE__) + "/base/restore"
-require File.dirname(__FILE__) + "/base/table"
-
module ActsAsArchive
- def self.update(*models)
- models.each do |klass|
- if klass.respond_to?(:acts_as_archive?) && klass.acts_as_archive?
- klass.create_archive_table
- klass.migrate_from_acts_as_paranoid
- klass.create_archive_indexes
- puts 'Success!'
- end
- end
- end
-
module Base
def self.included(base)
base.extend ActMethods
View
17 lib/acts_as_archive/base/adapters/mysql.rb
@@ -0,0 +1,17 @@
+module ActsAsArchive
+ module Base
+ module Adapters
+ module MySQL
+
+ private
+
+ def archive_table_indexed_columns
+ index_query = "SHOW INDEX FROM archived_#{table_name}"
+ indexes = connection.select_all(index_query).collect do |r|
+ r["Column_name"]
+ end
+ end
+ end
+ end
+ end
+end
View
34 lib/acts_as_archive/base/adapters/postgresql.rb
@@ -0,0 +1,34 @@
+module ActsAsArchive
+ module Base
+ module Adapters
+ module PostgreSQL
+
+ private
+
+ def archive_table_indexed_columns
+ index_query = <<-SQL
+ SELECT c2.relname as index_name
+ FROM pg_catalog.pg_class c,
+ pg_catalog.pg_class c2,
+ pg_catalog.pg_index i
+ WHERE c.oid = (SELECT c.oid
+ FROM pg_catalog.pg_class c
+ WHERE c.relname ~ '^(archived_#{table_name})$')
+ AND c.oid = i.indrelid
+ AND i.indexrelid = c2.oid
+ SQL
+
+ index_query.gsub!(/\s+/, ' ').strip!
+ indexes = connection.select_all(index_query).collect do |r|
+ r["index_name"]
+ end
+
+ # HACK: reverse engineer the column name
+ indexes.map do |index|
+ index.split("_on_").last
+ end
+ end
+ end
+ end
+ end
+end
View
10 lib/acts_as_archive/base/table.rb
@@ -6,6 +6,14 @@ def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.send :extend, ClassMethods
base.send :include, InstanceMethods
+
+ if base.connection.class.to_s.include?('Mysql')
+ base.send :extend, ActsAsArchive::Base::Adapters::MySQL
+ elsif base.connection.class.to_s.include?('PostgreSQL')
+ base.send :extend, ActsAsArchive::Base::Adapters::PostgreSQL
+ else
+ raise 'acts_as_archive does not support this database adapter'
+ end
end
end
@@ -19,7 +27,7 @@ def create_archive_table
if table_exists? && !archive_table_exists?
connection.execute(%{
CREATE TABLE archived_#{table_name}
- #{"ENGINE=InnoDB" if connection.class.to_s == "ActiveRecord::ConnectionAdapters::MysqlAdapter"}
+ #{"ENGINE=InnoDB" if connection.class.to_s.include?('Mysql')}
AS SELECT * from #{table_name}
WHERE false;
})
View
5 rails/init.rb
@@ -1,4 +1,5 @@
-require File.expand_path(File.dirname(__FILE__) + "/../lib/acts_as_archive")
+require File.expand_path("#{File.dirname(__FILE__)}/../require")
+Require.rails_init!
ActiveRecord::Base.send(:include, ActsAsArchive::Base)
-ActiveRecord::Migration.send(:include, ActsAsArchive::Migration)
+ActiveRecord::Migration.send(:include, ActsAsArchive::Migration)
View
49 require.rb
@@ -0,0 +1,49 @@
+require 'rubygems'
+gem 'require'
+require 'require'
+
+Require do
+ gem(:activerecord) { require 'active_record' }
+ gem :require, '=0.2.1'
+ gem(:rake, '=0.8.7') { require 'rake' }
+ gem :rspec, '=1.3.0'
+
+ gemspec do
+ author 'Winton Welsh'
+ dependencies do
+ gem :require
+ end
+ email 'mail@wintoni.us'
+ name 'acts_as_archive'
+ homepage "http://github.com/winton/#{name}"
+ summary "Don't delete your records, move them to a different table"
+ version '0.2.0'
+ end
+
+ lib do
+ require "lib/acts_as_archive/base"
+ require "lib/acts_as_archive/base/adapters/mysql"
+ require "lib/acts_as_archive/base/adapters/postgresql"
+ require "lib/acts_as_archive/base/destroy"
+ require "lib/acts_as_archive/base/restore"
+ require "lib/acts_as_archive/base/table"
+ require "lib/acts_as_archive/migration"
+ end
+
+ rails_init { require 'lib/acts_as_archive' }
+
+ rakefile do
+ gem(:rake) { require 'rake/gempackagetask' }
+ gem(:rspec) { require 'spec/rake/spectask' }
+ require 'require/tasks'
+ end
+
+ spec_helper do
+ require 'require/spec_helper'
+ gem :activerecord
+ require 'logger'
+ require 'yaml'
+ require 'pp'
+ require 'rails/init'
+ end
+end
View
20 spec/spec_helper.rb
@@ -1,13 +1,5 @@
-$TESTING=true
-SPEC = File.expand_path(File.dirname(__FILE__))
-
-require 'rubygems'
-require 'active_record'
-require 'logger'
-require 'yaml'
-require 'pp'
-
-require File.expand_path("#{SPEC}/../rails/init")
+require File.expand_path("#{File.dirname(__FILE__)}/../require")
+Require.spec_helper!
Spec::Runner.configure do |config|
end
@@ -54,12 +46,6 @@ def create_records(klass=Article, values={})
end
end
-def debug(object)
- puts "<pre>"
- pp object
- puts "</pre>"
-end
-
def establish_test_db
# Establish connection
unless ActiveRecord::Base.connected?
@@ -94,4 +80,4 @@ def migrate_up(directory='migrate')
ActiveRecord::Migrator.migrate("#{SPEC}/db/#{directory}")
@new_article_columns = columns("articles")
@new_archive_columns = columns("archived_articles")
-end
+end

0 comments on commit 30280a2

Please sign in to comment.