Permalink
Browse files

Version 0.1.2: New rakefile, using bin/acts_as_archive to generate ar…

…chive tables and indexes instead of doing it when Rails starts
  • Loading branch information...
winton committed Apr 25, 2009
1 parent 36fd1c1 commit 7424077a353a3666d0cdd42eafb76b0ea344eb99
Showing with 133 additions and 84 deletions.
  1. +19 −8 README.markdown
  2. +31 −25 Rakefile
  3. +28 −39 acts_as_archive.gemspec
  4. +6 −0 bin/acts_as_archive
  5. +13 −4 lib/acts_as_archive/base.rb
  6. +24 −8 lib/acts_as_archive/base/table.rb
  7. +12 −0 rails/init.rb
View
@@ -31,16 +31,32 @@ Add <code>acts\_as\_archive</code> to your models:
<pre>
class Article < ActiveRecord::Base
+
acts_as_archive
+
+ # Or you can add custom archive table indexes
+ acts_as_archive :indexes => [ :id, :deleted_at ]
end
</pre>
+Run acts\_as\_archive
+---------------------
+
+Terminal:
+
+<pre>
+cd your_rails_app
+acts_as_archive
+</pre>
+
+This command replicates your table's structure into <code>archived\_articles</code>
+(as per the example), with an additional <code>deleted\_at</code> column.
+
+Run again if you add more <code>:indexes</code>.
+
That's it!
----------
-Next time your Rails instance boots up, the plugin replicates your table's structure into
-<code>archived\_articles</code> (as per the example), with an additional <code>deleted\_at</code> column.
-
Use <code>destroy</code>, <code>delete</code>, and <code>delete_all</code> like you normally would.
Records are copied into the archive table before being destroyed.
@@ -72,8 +88,3 @@ Auto-migrate from acts\_as\_paranoid
If a <code>deleted\_at</code> column is present in your table, the plugin will attempt to move deleted
records to the archive table. The <code>deleted\_at</code> value is preserved.
-
-Production
-----------
-
-Any time you add a new <code>acts\_as\_archive</code> method call, please run <code>script/console</code> on your production copy before it goes live.
View
@@ -1,39 +1,45 @@
-require 'rake'
require 'rubygems'
+require 'rake'
+require 'rake/gempackagetask'
require 'spec/rake/spectask'
GEM_NAME = 'acts_as_archive'
-task :default => "#{GEM_NAME}.gemspec"
+PKG_FILES = FileList['**/*']
+
+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.2"
+end
-file "#{GEM_NAME}.gemspec" => FileList[ '{lib,spec}/**', 'Rakefile' ] do |f|
- # Read spec file and split out manifest section
- spec = File.read(f.name)
- parts = spec.split(" # = MANIFEST =\n")
- fail 'bad spec' if parts.length != 3
- # Determine file list from git ls-files
- files = `git ls-files`.
- split("\n").
- sort.
- reject{ |file| file =~ /^\./ }.
- reject { |file| file =~ /^doc/ }.
- map{ |file| " #{file}" }.
- join("\n")
- # Piece file back together and write
- parts[1] = " s.files = %w[\n#{files}\n ]\n"
- spec = parts.join(" # = MANIFEST =\n")
- File.open(f.name, 'w') { |io| io.write(spec) }
- puts "Updated #{f.name}"
+desc "Package gem"
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
end
-# Install the gem
+desc "Install gem"
task :install do
+ Rake::Task['gem'].invoke
`sudo gem uninstall #{GEM_NAME} -x`
- `gem build #{GEM_NAME}.gemspec`
- `sudo gem install #{GEM_NAME}*.gem`
- `rm #{GEM_NAME}*.gem`
+ `sudo 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
-# rake spec
desc "Run specs"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ["--format", "specdoc", "--colour"]
View
@@ -1,40 +1,29 @@
+# -*- encoding: utf-8 -*-
+
Gem::Specification.new do |s|
- s.name = 'acts_as_archive'
- s.version = '0.1.1'
- s.date = '2009-04-23'
-
- s.summary = "Don't delete your records, move them to a different table"
- s.description = "Don't delete your records, move them to a different table"
-
- s.author = 'Winton Welsh'
- s.email = 'mail@wintoni.us'
- s.homepage = 'http://github.com/winton/acts_as_archive'
-
- # = MANIFEST =
- s.files = %w[
- CHANGELOG.markdown
- MIT-LICENSE
- README.markdown
- Rakefile
- acts_as_archive.gemspec
- init.rb
- lib/acts_as_archive.rb
- lib/acts_as_archive/base.rb
- lib/acts_as_archive/base/destroy.rb
- lib/acts_as_archive/base/restore.rb
- lib/acts_as_archive/base/table.rb
- lib/acts_as_archive/migration.rb
- rails/init.rb
- spec/acts_as_archive/base/destroy_spec.rb
- spec/acts_as_archive/base/restore_spec.rb
- spec/acts_as_archive/base/table_spec.rb
- spec/acts_as_archive/migration_spec.rb
- spec/db/config/database.yml.example
- spec/db/log/.gitignore
- spec/db/migrate/001_add_to_articles.rb
- spec/db/models/article.rb
- spec/spec.opts
- spec/spec_helper.rb
- ]
- # = MANIFEST =
-end
+ s.name = %q{acts_as_archive}
+ s.version = "0.1.2"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Winton Welsh"]
+ s.date = %q{2009-04-25}
+ s.default_executable = %q{acts_as_archive}
+ s.email = %q{mail@wintoni.us}
+ s.executables = ["acts_as_archive"]
+ s.extra_rdoc_files = ["README.markdown"]
+ s.files = ["acts_as_archive.gemspec", "bin", "bin/acts_as_archive", "CHANGELOG.markdown", "init.rb", "lib", "lib/acts_as_archive", "lib/acts_as_archive/base", "lib/acts_as_archive/base/destroy.rb", "lib/acts_as_archive/base/restore.rb", "lib/acts_as_archive/base/table.rb", "lib/acts_as_archive/base.rb", "lib/acts_as_archive/migration.rb", "lib/acts_as_archive.rb", "MIT-LICENSE", "rails", "rails/init.rb", "Rakefile", "README.markdown", "spec", "spec/acts_as_archive", "spec/acts_as_archive/base", "spec/acts_as_archive/base/destroy_spec.rb", "spec/acts_as_archive/base/restore_spec.rb", "spec/acts_as_archive/base/table_spec.rb", "spec/acts_as_archive/migration_spec.rb", "spec/db", "spec/db/config", "spec/db/config/database.yml", "spec/db/config/database.yml.example", "spec/db/log", "spec/db/log/test.log", "spec/db/migrate", "spec/db/migrate/001_add_to_articles.rb", "spec/db/models", "spec/db/models/article.rb", "spec/spec.opts", "spec/spec_helper.rb"]
+ s.homepage = %q{http://github.com/winton/acts_as_archive}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{Don't delete your records, move them to a different table}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+$:.push File.expand_path("#{File.dirname(__FILE__)}/../lib")
+require 'acts_as_archive'
+
+puts `script/runner "ActsAsArchive.update"`
@@ -9,16 +9,25 @@ def self.included(base)
end
module ActMethods
- def acts_as_archive
- include Destroy
- include Restore
- include Table
+ def acts_as_archive(options={})
class_eval <<-end_eval
+
+ def self.acts_as_archive?
+ self.to_s == #{self.to_s.inspect}
+ end
+
+ def self.archive_indexes
+ #{Array(options[:indexes]).collect(&:to_s).inspect}
+ end
+
class Archive < ActiveRecord::Base
self.record_timestamps = false
self.table_name = "archived_#{self.table_name}"
end
end_eval
+ include Destroy
+ include Restore
+ include Table
end
end
end
@@ -6,15 +6,17 @@ def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.send :extend, ClassMethods
base.send :include, InstanceMethods
- # Create archive table when Rails starts
- base.create_archive_table unless $TESTING
end
end
module ClassMethods
+ def archive_table_exists?
+ connection.table_exists?("archived_#{table_name}")
+ end
+
def create_archive_table
- if connection.table_exists?(table_name) && !connection.table_exists?("archived_#{table_name}")
+ if table_exists? && !archive_table_exists?
connection.execute(%{
CREATE TABLE archived_#{table_name}
ENGINE=InnoDB
@@ -25,16 +27,30 @@ def create_archive_table
unless columns.include?('deleted_at')
connection.add_column("archived_#{table_name}", :deleted_at, :datetime)
end
- connection.add_index("archived_#{table_name}", :id)
- connection.add_index("archived_#{table_name}", :deleted_at)
- migrate_from_acts_as_paranoid
+ end
+ end
+
+ def create_archive_indexes
+ if archive_table_exists?
+ indexes = "SHOW INDEX FROM archived_#{table_name}"
+ indexes = connection.select_all(indexes).collect do |r|
+ r["Column_name"]
+ end
+ (archive_indexes - indexes).each do |index|
+ connection.add_index("archived_#{table_name}", index)
+ end
end
end
def migrate_from_acts_as_paranoid
if column_names.include?('deleted_at')
- # Base::Destroy.copy_to_archive
- copy_to_archive('deleted_at IS NOT NULL', true)
+ if table_exists? && archive_table_exists?
+ condition = "deleted_at IS NOT NULL"
+ if self.count_by_sql("SELECT COUNT(*) FROM #{table_name} WHERE #{condition}") > 0
+ # Base::Destroy.copy_to_archive
+ copy_to_archive(condition, true)
+ end
+ end
end
end
end
View
@@ -2,3 +2,15 @@
ActiveRecord::Base.send(:include, ActsAsArchive::Base)
ActiveRecord::Migration.send(:include, ActsAsArchive::Migration)
+
+module ActsAsArchive
+ def self.update
+ Object.subclasses_of(ActiveRecord::Base).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
+ end
+ end
+ end
+end

0 comments on commit 7424077

Please sign in to comment.