simplify previous change, no need to re-retrieve the row because $self is already...
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Journal.pm
index 2ae3725..1757e51 100644 (file)
@@ -5,101 +5,69 @@ use base qw/DBIx::Class/;
 use strict;
 use warnings;
 
-our $VERSION = '0.02_01';
+our $VERSION = '0.900001_04';
+$VERSION = eval $VERSION; # no errors in dev versions
 
-## On create/insert, add new entry to AuditLog
+## On create/insert, add new entry to AuditLog and new content to AuditHistory
 
-# sub new
-# {
-#     my ($class, $attrs, @rest) = @_;
-
-#     $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id});
-
-#     $class->next::method($attrs, @rest);
-# }
-
-sub journal_update_or_create_log_entry
-{
-    my ($self, $field ) = @_;
-    my $rs = $self->result_source;
-    my $s_name = $rs->source_name();
-
-    my $jschema = $rs->schema->_journal_schema;
-
-    my $al = $jschema->resultset("${s_name}AuditLog");
-
-    my %id = map { $_ => $self->get_column($_)} $self->primary_columns;
-
-    my %extra;
-
-    if ( $field ) {
-        $extra{$field} = $jschema->journal_create_change->id;
-    }
-
-    $al->update_or_create({ %extra, %id });
+sub _journal_schema {
+    my $self = shift;
+    $self->result_source->schema->_journal_schema;
 }
 
-sub insert
-{
+sub insert {
     my ($self, @args) = @_;
-
-    return if($self->in_storage);
+    return if $self->in_storage;
 
     my $res = $self->next::method(@args);
-
-    $self->journal_log_insert();
+    $self->journal_log_insert;
 
     return $res;
 }
 
-sub journal_log_insert
-{
+sub journal_log_insert {
     my ($self) = @_;
 
-    $self->journal_update_or_create_log_entry('create_id')
-        if $self->in_storage;
+    if ( $self->in_storage ) {
+        my $j = $self->_journal_schema;
+        my $change_id = $j->journal_create_change()->id;
+        $j->journal_update_or_create_log_entry( $self, create_id => $change_id );
+        $j->journal_record_in_history( $self, audit_change_id => $change_id );
+    }
 }
 
 ## On delete, update delete_id of AuditLog
 
-sub delete
-{
-    my ($self, @rest) = @_;
-    $self->next::method(@rest);
-    $self->journal_log_delete(@rest);
+sub delete {
+    my $self = shift;
+    $self->next::method(@_);
+    $self->journal_log_delete(@_);
 }
 
-sub journal_log_delete
-{
+sub journal_log_delete {
     my ($self) = @_;
 
-    $self->journal_update_or_create_log_entry('delete_id')
-        unless $self->in_storage;
+    unless ($self->in_storage) {
+        my $j = $self->_journal_schema;
+        $j->journal_update_or_create_log_entry( $self, delete_id => $j->journal_create_change->id );
+    }
 }
 
-## On update, copy previous row's contents to AuditHistory
+## On update, copy row's new contents to AuditHistory
 
-sub update 
-{
-    my ($self, $upd, @rest) = @_;
-    $self->journal_log_update($upd, @rest);
-    $self->next::method($upd, @rest);
+sub update {
+    my $self = shift;
+    $self->next::method(@_);
+    $self->journal_log_update(@_);
 }
 
-sub journal_log_update 
-{
-    my ($self, $upd, @rest) = @_;
+sub journal_log_update {
+    my $self = shift;
 
-    if($self->in_storage)
-    {
-        my $s_name = $self->result_source->source_name();
-        my $ah = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditHistory");
-
-        my $obj = $self->result_source->resultset->find( $self->ident_condition );
-        $ah->create({
-            $obj->get_columns,
-            change => { changeset_id => $ah->result_source->schema->current_changeset },
-        });
+    if ($self->in_storage) {
+        my $j = $self->_journal_schema;
+        my $change_id = $j->journal_create_change->id;
+        $j->journal_record_in_history( $self, audit_change_id => $change_id );
     }
 }
 
@@ -109,21 +77,21 @@ DBIx::Class::Journal - auditing for tables managed by DBIx::Class
 
 =head1 SYNOPSIS
 
-  package My::Schema;
-  use base 'DBIx::Class::Schema';
+ package My::Schema;
+ use base 'DBIx::Class::Schema';
 
-  __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Journal/);
+ __PACKAGE__->load_components(qw/Schema::Journal/);
 
-  __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']);
-  __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]);
+ __PACKAGE__->journal_connection(['dbi:SQLite:t/var/Audit.db']);
+ __PACKAGE__->journal_user(['My::Schema::User', {'foreign.userid' => 'self.user_id'}]);
 
 
- ########
+ #######
 
-  $schema->changeset_user($user->id);
-  my $new_artist = $schema->txn_do( sub {
-   return = $schema->resultset('Artist')->create({ name => 'Fred' });
-  });
+ $schema->changeset_user($user->id);
+ my $new_artist = $schema->txn_do( sub {
+    return $schema->resultset('Artist')->create({ name => 'Fred' });
+ });
 
 
 =head1 DESCRIPTION
@@ -135,7 +103,7 @@ create/update/delete operation an id. The creation and deletion date
 of each row is stored, as well as the previous contents of any row
 that gets changed.
 
-All queries which want auditing should be called using
+All queries which need auditing must be called using
 L<DBIx::Class::Schema/txn_do>, which is used to create changesets for
 each transaction.
 
@@ -148,7 +116,7 @@ change, use C<< $schema->_journal_schema >>.
 
 =head2 TABLES
 
-The journal schema contains a number of tables. 
+The journal schema contains a number of tables.
 
 =over
 
@@ -199,6 +167,12 @@ Leaving this blank assumes you want to store the audit data into your current
 database. The storage object will be shared by the regular schema and the
 journalling schema.
 
+=item journal_components @components
+
+If you want to add components to your journal
+(L<DBIx::Class::Schema::Versioned> for example) this would be the
+
+
 =item journal_sources \@source_names
 
 Set a list of source names you would like to audit, if unset, all