This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed hooks to work with Resource#destroy when paranoid type used * Specified behaviour of Resource#destroy and Resource#destroy! when a paranoid type is used * Specified behaviour of Model#with_deleted when a paranoia property is declared. Previous behaviour seemed broken, since it would only have returned deleted resources, but "with deleted" implies you will be returning all resources, including the deleted ones. * Moved common code from paranoid types into a module [#520 state:resolved] [#883 state:resolved]
- Loading branch information
Showing
9 changed files
with
360 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module DataMapper | ||
module Types | ||
module Paranoid | ||
module Base | ||
def self.included(type) | ||
type.extend ClassMethods | ||
end | ||
|
||
def paranoid_destroy | ||
return false unless saved? | ||
model.paranoid_properties.each do |name, block| | ||
attribute_set(name, block.call(self)) | ||
end | ||
save_self | ||
set_destroyed_state | ||
true | ||
end | ||
|
||
private | ||
|
||
# @api private | ||
def _destroy(safe) | ||
if safe | ||
paranoid_destroy | ||
else | ||
super | ||
end | ||
end | ||
end # module Methods | ||
|
||
module ClassMethods | ||
def with_deleted | ||
with_exclusive_scope({}) { block_given? ? yield : all } | ||
end | ||
end # module ClassMethods | ||
end # module Paranoid | ||
end # module Types | ||
end # module DataMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe DataMapper::Property, 'ParanoidBoolean type' do | ||
before :all do | ||
module ::Blog | ||
class Article | ||
include DataMapper::Resource | ||
|
||
attr_reader :hook_called | ||
|
||
property :id, Serial | ||
property :deleted, ParanoidBoolean | ||
|
||
before :destroy do | ||
@hook_called ||= 0 | ||
@hook_called += 1 | ||
end | ||
end | ||
end | ||
|
||
@model = Blog::Article | ||
end | ||
|
||
supported_by :all do | ||
describe 'Resource#destroy' do | ||
subject { @resource.destroy } | ||
|
||
describe 'with a new resource' do | ||
before do | ||
@resource = @model.new | ||
end | ||
|
||
it { should be_false } | ||
|
||
it 'should not delete the resource from the datastore' do | ||
method(:subject).should_not change { @model.with_deleted.size }.from(0) | ||
end | ||
|
||
it 'should not set the paranoid column' do | ||
method(:subject).should_not change { @resource.deleted }.from(false) | ||
end | ||
|
||
it 'should run the destroy hook' do | ||
method(:subject).should change { @resource.hook_called }.from(nil).to(1) | ||
end | ||
end | ||
|
||
describe 'with a saved resource' do | ||
before do | ||
@resource = @model.create | ||
end | ||
|
||
it { should be_true } | ||
|
||
it 'should not delete the resource from the datastore' do | ||
method(:subject).should_not change { @model.with_deleted.size }.from(1) | ||
end | ||
|
||
it 'should set the paranoid column' do | ||
method(:subject).should change { @resource.deleted }.from(false).to(true) | ||
end | ||
|
||
it 'should run the destroy hook' do | ||
method(:subject).should change { @resource.hook_called }.from(nil).to(1) | ||
end | ||
end | ||
end | ||
|
||
describe 'Resource#destroy!' do | ||
subject { @resource.destroy! } | ||
|
||
describe 'with a new resource' do | ||
before do | ||
@resource = @model.new | ||
end | ||
|
||
it { should be_false } | ||
|
||
it 'should not delete the resource from the datastore' do | ||
method(:subject).should_not change { @model.with_deleted.size }.from(0) | ||
end | ||
|
||
it 'should not set the paranoid column' do | ||
method(:subject).should_not change { @resource.deleted }.from(false) | ||
end | ||
|
||
it 'should not run the destroy hook when #destroy called' do | ||
method(:subject).should_not change { @resource.hook_called }.from(nil) | ||
end | ||
end | ||
|
||
describe 'with a saved resource' do | ||
before do | ||
@resource = @model.create | ||
end | ||
|
||
it { should be_true } | ||
|
||
it 'should delete the resource from the datastore' do | ||
method(:subject).should change { @model.with_deleted.size }.from(1).to(0) | ||
end | ||
|
||
it 'should not set the paranoid column' do | ||
method(:subject).should_not change { @resource.deleted }.from(false) | ||
end | ||
|
||
it 'should not run the destroy hook when #destroy called' do | ||
method(:subject).should_not change { @resource.hook_called }.from(nil) | ||
end | ||
end | ||
end | ||
|
||
describe 'Model#with_deleted' do | ||
before do | ||
@resource = @model.create | ||
@resource.destroy | ||
end | ||
|
||
describe 'with a block' do | ||
subject { @model.with_deleted { @model.all } } | ||
|
||
it 'should scope the block to return all resources' do | ||
subject.map { |resource| resource.key }.should == [ @resource.key ] | ||
end | ||
end | ||
|
||
describe 'without a block' do | ||
subject { @model.with_deleted } | ||
|
||
it 'should return a collection scoped to return all resources' do | ||
subject.map { |resource| resource.key }.should == [ @resource.key ] | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.