[Bast-commits] r4701 - in
DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class: . Schema/Journal
nothingmuch at dev.catalyst.perl.org
nothingmuch at dev.catalyst.perl.org
Fri Aug 1 00:23:12 BST 2008
Author: nothingmuch
Date: 2008-08-01 00:23:11 +0100 (Fri, 01 Aug 2008)
New Revision: 4701
Modified:
DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Journal.pm
DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Schema/Journal/DB.pm
Log:
refactor journal data creation into separate methods
Modified: DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Journal.pm
===================================================================
--- DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Journal.pm 2008-07-31 21:52:15 UTC (rev 4700)
+++ DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Journal.pm 2008-07-31 23:23:11 UTC (rev 4701)
@@ -18,53 +18,63 @@
# $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 insert
{
- my ($self) = @_;
+ my ($self, @args) = @_;
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 %id = map { $_ => $self->get_column($_)} $self->primary_columns;
+ my $res = $self->next::method(@args);
- my $change = { changeset_id => $al->result_source->schema->current_changeset };
+ $self->journal_log_insert();
- 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 });
- }
- }
-
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,7 +82,14 @@
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)
{
my $s_name = $self->result_source->source_name();
@@ -84,8 +101,6 @@
change => { changeset_id => $ah->result_source->schema->current_changeset },
});
}
-
- $self->next::method($upd, @rest);
}
=head1 NAME
Modified: DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Schema/Journal/DB.pm
===================================================================
--- DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Schema/Journal/DB.pm 2008-07-31 21:52:15 UTC (rev 4700)
+++ DBIx-Class-Journal/1.000/trunk/lib/DBIx/Class/Schema/Journal/DB.pm 2008-07-31 23:23:11 UTC (rev 4701)
@@ -53,4 +53,9 @@
$cs->create({ %changesetdata });
}
+sub journal_create_change {
+ my $self = shift;
+ $self->resultset("ChangeLog")->create({ changeset_id => $self->current_changeset });
+}
+
1;
More information about the Bast-commits
mailing list