From: Yuval Kogman Date: Thu, 31 Jul 2008 23:23:11 +0000 (+0000) Subject: refactor journal data creation into separate methods X-Git-Tag: v0.900201~66 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6bfb7a1dd7d1acc3ba95670ab09d35abc6c3bdbb;p=dbsrgits%2FDBIx-Class-Journal.git refactor journal data creation into separate methods --- diff --git a/lib/DBIx/Class/Journal.pm b/lib/DBIx/Class/Journal.pm index 3187b0c..2ae3725 100644 --- a/lib/DBIx/Class/Journal.pm +++ b/lib/DBIx/Class/Journal.pm @@ -18,53 +18,63 @@ our $VERSION = '0.02_01'; # $class->next::method($attrs, @rest); # } -sub insert +sub journal_update_or_create_log_entry { - my ($self) = @_; + my ($self, $field ) = @_; + my $rs = $self->result_source; + my $s_name = $rs->source_name(); - return if($self->in_storage); - ## create new transaction here? - my $res = $self->next::method(); - if($self->in_storage) - { - my $s_name = $self->result_source->source_name(); - my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog"); + my $jschema = $rs->schema->_journal_schema; + + my $al = $jschema->resultset("${s_name}AuditLog"); - my %id = map { $_ => $self->get_column($_)} $self->primary_columns; + my %id = map { $_ => $self->get_column($_)} $self->primary_columns; - my $change = { changeset_id => $al->result_source->schema->current_changeset }; + my %extra; - if ( my $log = $al->find(\%id) ) { - #$log->created($change); # FIXME should this work? - $log->created($al->related_resultset("created")->create($change)); - $log->update; - } else { - $al->create({ %id, created => $change }); - } + if ( $field ) { + $extra{$field} = $jschema->journal_create_change->id; } + $al->update_or_create({ %extra, %id }); +} + +sub insert +{ + my ($self, @args) = @_; + + return if($self->in_storage); + + my $res = $self->next::method(@args); + + $self->journal_log_insert(); + return $res; } +sub journal_log_insert +{ + my ($self) = @_; + + $self->journal_update_or_create_log_entry('create_id') + if $self->in_storage; +} + ## On delete, update delete_id of AuditLog sub delete { my ($self, @rest) = @_; $self->next::method(@rest); + $self->journal_log_delete(@rest); +} - if(!$self->in_storage) - { - my $s_name = $self->result_source->source_name(); - my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog"); - my $alentry = $al->find_or_create({ map { $_ => $self->get_column($_)} $self->primary_columns }); - - ## bulk_update doesnt do "create new item on update of rel-accessor with hashref, yet - my $change = $self->result_source->schema->_journal_schema->resultset('ChangeLog')->create({ changeset_id => $self->result_source->schema->_journal_schema->current_changeset }); - $alentry->delete_id($change->id); - $alentry->update(); - } - +sub journal_log_delete +{ + my ($self) = @_; + + $self->journal_update_or_create_log_entry('delete_id') + unless $self->in_storage; } ## On update, copy previous row's contents to AuditHistory @@ -72,6 +82,13 @@ sub delete sub update { my ($self, $upd, @rest) = @_; + $self->journal_log_update($upd, @rest); + $self->next::method($upd, @rest); +} + +sub journal_log_update +{ + my ($self, $upd, @rest) = @_; if($self->in_storage) { @@ -84,8 +101,6 @@ sub update change => { changeset_id => $ah->result_source->schema->current_changeset }, }); } - - $self->next::method($upd, @rest); } =head1 NAME diff --git a/lib/DBIx/Class/Schema/Journal/DB.pm b/lib/DBIx/Class/Schema/Journal/DB.pm index da13faf..68c2024 100644 --- a/lib/DBIx/Class/Schema/Journal/DB.pm +++ b/lib/DBIx/Class/Schema/Journal/DB.pm @@ -53,4 +53,9 @@ sub journal_create_changeset { $cs->create({ %changesetdata }); } +sub journal_create_change { + my $self = shift; + $self->resultset("ChangeLog")->create({ changeset_id => $self->current_changeset }); +} + 1;