[Bps-public-commit] jifty-plugin-recordhistory branch, master, updated. 0.03-8-g51428d7

Shawn Moore sartak at bestpractical.com
Fri Feb 18 18:57:14 EST 2011


The branch, master has been updated
       via  51428d7a16f6bad48edf7da83bcf864d0a1bf53b (commit)
      from  5db16c435d3c386e8665630510c532a8f3db4b52 (commit)

Summary of changes:
 Changes                                            |    1 +
 lib/Jifty/Plugin/RecordHistory.pm                  |    7 +++
 .../RecordHistory/Mixin/Model/RecordHistory.pm     |   15 ++++++
 .../Plugin/RecordHistory/Model/{CD.pm => DVD.pm}   |    6 +-
 .../t/007-delete-change.t                          |   49 ++++++++++++++++++++
 5 files changed, 75 insertions(+), 3 deletions(-)
 copy t/TestApp-Plugin-RecordHistory/lib/TestApp/Plugin/RecordHistory/Model/{CD.pm => DVD.pm} (71%)
 create mode 100644 t/TestApp-Plugin-RecordHistory/t/007-delete-change.t

- Log -----------------------------------------------------------------
commit 51428d7a16f6bad48edf7da83bcf864d0a1bf53b
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Feb 18 18:56:54 2011 -0500

    Implement doc and test delete_change
    
        for creating a delete change when you delete the record

diff --git a/Changes b/Changes
index 100e23e..469316f 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@
     - Allow control of cascaded_delete (Sartak)
     - Allow control over current_user_can on Change and ChangeField by
       implementing current_user_can_for_change in your record class (Sartak)
+    - Provide a way to create a delete_change for record delete (Sartak)
 
 0.03  2011-02-17
     - Provide a brief description of the object being viewed in the page title (trs)
diff --git a/lib/Jifty/Plugin/RecordHistory.pm b/lib/Jifty/Plugin/RecordHistory.pm
index 5854bb0..fe6fe08 100644
--- a/lib/Jifty/Plugin/RecordHistory.pm
+++ b/lib/Jifty/Plugin/RecordHistory.pm
@@ -49,6 +49,7 @@ of history. Here are the defaults:
 
     use Jifty::Plugin::RecordHistory::Mixin::Model::RecordHistory (
         cascaded_delete => 1,
+        delete_change   => 0,
     );
 
 If C<cascaded_delete> is true, then
@@ -58,6 +59,12 @@ same time the original record they refer to is deleted. If C<cascaded_delete>
 is false, then the Change and ChangeField records persist even if the original
 record is deleted.
 
+If C<delete_change> is true, then when your record is deleted we create a
+L<Jifty::Plugin::RecordHistory::Model::Change> record whose type is C<delete>.
+If C<delete_change> is false, then we do not record the deletion. If
+both C<cascaded_delete> I<and> C<delete_change> are true, then you will end up
+with only one change after the record is deleted -- the C<delete>.
+
 =head2 Grouping
 
 By default, the only mechanism that groups together change_fields onto a single
diff --git a/lib/Jifty/Plugin/RecordHistory/Mixin/Model/RecordHistory.pm b/lib/Jifty/Plugin/RecordHistory/Mixin/Model/RecordHistory.pm
index 6f4ed74..a40f65c 100644
--- a/lib/Jifty/Plugin/RecordHistory/Mixin/Model/RecordHistory.pm
+++ b/lib/Jifty/Plugin/RecordHistory/Mixin/Model/RecordHistory.pm
@@ -13,6 +13,7 @@ sub import {
     my $class = shift;
     my %args  = (
         cascaded_delete => 1,
+        delete_change   => 0,
         @_,
     );
 
@@ -78,6 +79,20 @@ sub import {
         });
     }
 
+    # this is intentionally added AFTER the previous trigger
+    if ($args{delete_change}) {
+        $caller->add_trigger(before_delete => sub {
+            my $self = shift;
+
+            my $change = Jifty::Plugin::RecordHistory::Model::Change->new;
+            $change->create(
+                record_class => ref($self),
+                record_id    => $self->id,
+                type         => 'delete',
+            );
+        });
+    }
+
     # wrap update actions in a change so we can group them as one change with
     # many field changes
     $caller->add_trigger(start_update_action => sub {
diff --git a/t/TestApp-Plugin-RecordHistory/lib/TestApp/Plugin/RecordHistory/Model/DVD.pm b/t/TestApp-Plugin-RecordHistory/lib/TestApp/Plugin/RecordHistory/Model/DVD.pm
new file mode 100644
index 0000000..e228ff5
--- /dev/null
+++ b/t/TestApp-Plugin-RecordHistory/lib/TestApp/Plugin/RecordHistory/Model/DVD.pm
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+
+package TestApp::Plugin::RecordHistory::Model::DVD;
+use Jifty::DBI::Schema;
+
+use TestApp::Plugin::RecordHistory::Record schema {
+    column title =>
+        type is 'varchar';
+    column director =>
+        type is 'varchar';
+};
+
+use Jifty::Plugin::RecordHistory::Mixin::Model::RecordHistory (
+    delete_change => 1,
+);
+
+1;
+
diff --git a/t/TestApp-Plugin-RecordHistory/t/007-delete-change.t b/t/TestApp-Plugin-RecordHistory/t/007-delete-change.t
new file mode 100644
index 0000000..9073369
--- /dev/null
+++ b/t/TestApp-Plugin-RecordHistory/t/007-delete-change.t
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+use Jifty::Test::Dist tests => 21;
+
+my $dvd = TestApp::Plugin::RecordHistory::Model::DVD->new;
+$dvd->create(
+    title => 'Silence of the Lambs',
+);
+ok($dvd->id, 'created a dvd');
+
+isa_ok($dvd->changes, 'Jifty::Plugin::RecordHistory::Model::ChangeCollection');
+is($dvd->changes->count, 1, 'one change');
+my $change = $dvd->changes->first;
+is($change->record_id, $dvd->id, 'record id');
+is($change->record_class, 'TestApp::Plugin::RecordHistory::Model::DVD', 'record class');
+is($change->type, 'create', 'change has type create');
+is($change->record->title, 'Silence of the Lambs', 'change->record');
+
+isa_ok($change->change_fields, 'Jifty::Plugin::RecordHistory::Model::ChangeFieldCollection', 'change field collection');
+is($change->change_fields->count, 0, 'generate no ChangeFields for create');
+
+$dvd->set_title('That Hannibal Movie');
+
+isa_ok($dvd->changes, 'Jifty::Plugin::RecordHistory::Model::ChangeCollection');
+is($dvd->changes->count, 2, 'two changes');
+is($dvd->changes->first->type, 'create', 'first change is the create');
+$change = $dvd->changes->last;
+is($change->type, 'update', 'second change is the update');
+is($change->change_fields->count, 1, 'one field updated');
+
+my $change_field = $change->change_fields->first;
+is($change_field->change->id, $change->id, 'associated with the right change');
+is($change_field->field, 'title');
+is($change_field->new_value, 'That Hannibal Movie');
+is($change_field->old_value, 'Silence of the Lambs');
+
+$dvd->delete;
+
+my $changes = Jifty::Plugin::RecordHistory::Model::ChangeCollection->new;
+$changes->unlimit;
+is($changes->count, 1, 'one change');
+is($changes->first->type, 'delete', 'only change is a delete');
+
+my $change_fields = Jifty::Plugin::RecordHistory::Model::ChangeFieldCollection->new;
+$change_fields->unlimit;
+is($change_fields->count, 0, 'deleted all change fields');
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list