From bb516b10c699a75267cb97aea6e24eb139d8fec8 Mon Sep 17 00:00:00 2001 From: Jordi Romero Date: Fri, 17 Dec 2010 22:44:02 +0100 Subject: [PATCH] extracting immortal stuff to the gem --- Gemfile | 1 + Gemfile.lock | 3 + .../20101215120306_create_immortal_models.rb | 15 -- db/schema.rb | 8 - lib/immortal.rb | 80 -------- spec/lib/immortal_spec.rb | 180 ------------------ spec/models/immortal_model_spec.rb | 5 - 7 files changed, 4 insertions(+), 288 deletions(-) delete mode 100644 db/migrate/20101215120306_create_immortal_models.rb delete mode 100644 lib/immortal.rb delete mode 100644 spec/lib/immortal_spec.rb delete mode 100644 spec/models/immortal_model_spec.rb diff --git a/Gemfile b/Gemfile index 0fe4f4fa98..cea6cd0978 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ gem 'vestal_versions', '~> 1.0.2' gem 'paperclip', '~> 2.3.6' gem 'teambox-permalink_fu', :require => 'permalink_fu' gem 'cancan', '~> 1.2.0' +gem 'immortal' #gem 'rails3_acts_as_paranoid', :git => 'git://github.com/goncalossilva/rails3_acts_as_paranoid.git' group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 84c7c1a525..294acf2f96 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -83,6 +83,8 @@ GEM hpricot (0.8.3) i18n (0.5.0) icalendar (1.1.5) + immortal (0.1.0) + activerecord (~> 3.0.3) json (1.4.6) json_pure (1.4.6) launchy (0.3.7) @@ -212,6 +214,7 @@ DEPENDENCIES hpricot (~> 0.8.2) i18n (>= 0.1.3) icalendar (~> 1.1.3) + immortal json launchy (~> 0.3.5) libxml-ruby (= 1.1.3) diff --git a/db/migrate/20101215120306_create_immortal_models.rb b/db/migrate/20101215120306_create_immortal_models.rb deleted file mode 100644 index 5f6dbacaae..0000000000 --- a/db/migrate/20101215120306_create_immortal_models.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateImmortalModels < ActiveRecord::Migration - def self.up - create_table :immortal_models do |t| - t.string :title - t.integer :value - t.boolean :deleted, :default => false - - t.timestamps - end - end - - def self.down - drop_table :immortal_models - end -end diff --git a/db/schema.rb b/db/schema.rb index b8e588a018..740ea28e30 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -171,14 +171,6 @@ add_index "google_docs", ["project_id"], :name => "index_google_docs_on_project_id" add_index "google_docs", ["user_id"], :name => "index_google_docs_on_user_id" - create_table "immortal_models", :force => true do |t| - t.string "title" - t.integer "value" - t.boolean "deleted", :default => false - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "ims", :force => true do |t| t.integer "card_id" t.string "name" diff --git a/lib/immortal.rb b/lib/immortal.rb deleted file mode 100644 index 5f49877c62..0000000000 --- a/lib/immortal.rb +++ /dev/null @@ -1,80 +0,0 @@ -module Immortal - def self.included(base) - base.send :extend, ClassMethods - base.send :include, InstanceMethods - base.class_eval do - class << self - alias :mortal_delete_all :delete_all - alias :delete_all :immortal_delete_all - end - end - end - - module ClassMethods - - def with_deleted - unscoped - end - - def only_deleted - unscoped.where(:deleted => true) - end - - def count_with_deleted(*args) - with_deleted.count(*args) - end - - def count_only_deleted(*args) - only_deleted.count(*args) - end - - def find_with_deleted(*args) - with_deleted.find(*args) - end - - def find_only_deleted(*args) - only_deleted.find(*args) - end - - def immortal_delete_all(*args) - unscoped.update_all ["deleted = ?", true] - end - - def delete_all!(*args) - unscoped.mortal_delete_all - end - - end - - module InstanceMethods - def self.included(base) - base.class_eval do - default_scope where(["deleted IS NULL OR deleted = ?", false]) - alias :mortal_destroy :destroy - alias :destroy :immortal_destroy - end - end - - def immortal_destroy(*args) - run_callbacks :destroy do - destroy_without_callbacks(*args) - end - end - - def destroy!(*args) - mortal_destroy - end - - def destroy_without_callbacks(*args) - self.class.unscoped.update_all ["deleted = ?", true], "id = #{self.id}" - reload - freeze - end - - def recover! - self.class.unscoped.update_all ["deleted = ?", false], "id = #{self.id}" - reload - end - - end -end diff --git a/spec/lib/immortal_spec.rb b/spec/lib/immortal_spec.rb deleted file mode 100644 index 24d8187a74..0000000000 --- a/spec/lib/immortal_spec.rb +++ /dev/null @@ -1,180 +0,0 @@ -require 'spec_helper' - -describe Immortal do - before do - @m = ImmortalModel.create! :title => 'testing immortal' - end - - it "should not be deleted from the database using #destroy" do - expect { - @m.destroy - }.to_not change(ImmortalModel, :count_with_deleted) - end - - it "should be frozen using #destroy" do - @m.destroy - @m.should be_frozen - end - - it "should not be dirty using #destroy" do - @m.destroy - @m.should_not be_changed - end - - it "should be deleted from the database using #destroy!" do - expect { - @m.destroy! - }.to change(ImmortalModel, :count_with_deleted) - end - - it "should find non deleted records" do - ImmortalModel.first.should == @m - ImmortalModel.all.should include(@m) - end - - it "should not find deleted records" do - @m.destroy - ImmortalModel.first.should be_nil - ImmortalModel.all.should be_empty - end - - it "should find deleted records using scope" do - @m.destroy - ImmortalModel.with_deleted.first.should == @m - ImmortalModel.with_deleted.all.should include(@m) - end - - it "should find deleted records using the old method" do - ImmortalModel.find_with_deleted(@m.id).should == @m - @m.destroy - ImmortalModel.find_with_deleted(@m.id).should == @m - end - - it "should count undeleted records by default" do - @m2 = ImmortalModel.create! :title => 'testing immortal again' - ImmortalModel.count_only_deleted.should == 0 - ImmortalModel.only_deleted.count.should == 0 - - @m.destroy - - ImmortalModel.count_only_deleted.should == 1 - ImmortalModel.only_deleted.count.should == 1 - end - - it "should find only deleted records" do - @m2 = ImmortalModel.create! :title => 'testing immortal again' - expect { - ImmortalModel.find_only_deleted(@m.id) - }.to raise_error(ActiveRecord::RecordNotFound) - - expect { - ImmortalModel.only_deleted.find(@m.id) - }.to raise_error(ActiveRecord::RecordNotFound) - - @m.destroy - - ImmortalModel.find_only_deleted(@m.id).should == @m - expect { - ImmortalModel.find_only_deleted(@m2.id) - }.to raise_error(ActiveRecord::RecordNotFound) - - ImmortalModel.only_deleted.should include(@m) - ImmortalModel.only_deleted.should_not include(@m2) - end - - it "should be able to count undeleted records" do - @m2 = ImmortalModel.create! :title => 'testing immortal again' - ImmortalModel.count.should == 2 - - @m.destroy - - ImmortalModel.count.should == 1 - end - - it "should be able to count all the records including deleted" do - @m2 = ImmortalModel.create! :title => 'testing immortal again' - @m.destroy - ImmortalModel.count_with_deleted.should == 2 - ImmortalModel.with_deleted.count.should == 2 - end - - it "should not exist if deleted" do - ImmortalModel.exists?(@m.id).should be_true - @m.destroy - ImmortalModel.exists?(@m.id).should be_false - end - - it "should calculate without deleted" do - @m2 = ImmortalModel.create! :value => 10 - @m3 = ImmortalModel.create! :value => 20 - ImmortalModel.calculate(:sum, :value).should == 30 - @m2.destroy - ImmortalModel.calculate(:sum, :value).should == 20 - end - - it "should execute the before_destroy callback when immortally destroyed" do - @m.destroy - @m.before.should be_true - end - - it "should execute the after_destroy callback when immortally destroyed" do - @m.destroy - @m.after.should be_true - end - - it "should not execute the before_update callback when immortally destroyed" do - @m.destroy - @m.before_update.should be_nil - end - - it "should not execute the after_update callback when immortally destroyed" do - @m.destroy - @m.after_update.should be_nil - end - - it "should not execute the before_destroy callback when immortally destroyed without callbacks" do - @m.destroy_without_callbacks - @m.before.should be_nil - end - - it "should not execute the after_destroy callback when immortally destroyed without callbacks" do - @m.destroy_without_callbacks - @m.after.should be_nil - end - - it "should immortally delete all records with delete_all" do - expect { - ImmortalModel.delete_all - }.to change(ImmortalModel, :count).by(-1) - ImmortalModel.count_with_deleted.should == 1 - end - - it "should immortally delete all records with delete_all!" do - expect { - ImmortalModel.delete_all! - }.to change(ImmortalModel.with_deleted, :count).by(-1) - end - - it "should know if it's deleted" do - @m.should_not be_deleted - @m.destroy - @m.should be_deleted - end - - it "should be recoverable" do - @m.destroy - @m = ImmortalModel.with_deleted.find(@m.id) - @m.recover! - @m.should_not be_frozen - @m.should_not be_changed - ImmortalModel.first.should == @m - end - - it "should consider an object with deleted = nil as not deleted" do - @m2 = ImmortalModel.create! :deleted => nil - @m2.deleted.should be_nil - @m2.should_not be_deleted - ImmortalModel.count.should == 2 - end - -end diff --git a/spec/models/immortal_model_spec.rb b/spec/models/immortal_model_spec.rb deleted file mode 100644 index 83de3cc618..0000000000 --- a/spec/models/immortal_model_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe ImmortalModel do - pending "add some examples to (or delete) #{__FILE__}" -end