From: Florian Ragwitz Date: Tue, 26 Aug 2008 04:03:18 +0000 (+0000) Subject: Port to DBIx::Class::DynamicDefault. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bb500ec58eb28cdbdacc19e4b2f256a6e1fa228d;p=dbsrgits%2FDBIx-Class-UserStamp.git Port to DBIx::Class::DynamicDefault. git-svn-id: http://dev.catalyst.perl.org/repos/bast/DBIx-Class-UserStamp/1.000/trunk@4774 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/Makefile.PL b/Makefile.PL index ddd9e15..4fff0bd 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -4,6 +4,7 @@ name 'DBIx-Class-UserStamp'; all_from 'lib/DBIx/Class/UserStamp.pm'; requires 'DBIx::Class'; +requires 'DBIx::Class::DynamicDefault'; build_requires 'Class::Accessor::Grouped'; diff --git a/lib/DBIx/Class/UserStamp.pm b/lib/DBIx/Class/UserStamp.pm index 3115bbf..58414ed 100644 --- a/lib/DBIx/Class/UserStamp.pm +++ b/lib/DBIx/Class/UserStamp.pm @@ -7,11 +7,7 @@ use strict; our $VERSION = '0.10'; -__PACKAGE__->mk_classdata( - '__column_userstamp_triggers' => { - on_update => [], on_create => [] - } -); +__PACKAGE__->load_components( qw/DynamicDefault/ ); =head1 NAME @@ -64,63 +60,23 @@ This is effectively trigger emulation to ease user id field insertion =cut sub add_columns { - my $self = shift; - - # Add everything else, get everything setup, and then process - $self->next::method(@_); - - my @update_columns = (); - my @create_columns = (); - - foreach my $column ( $self->columns ) { - my $info = $self->column_info($column); - if ( $info->{store_user_on_update} ) { - push @update_columns, $column; - } - if ( $info->{store_user_on_create} ) { - push @create_columns, $column; - } - } - if ( @update_columns or @create_columns ) { - my $triggers = { - on_update => [ @update_columns ], - on_create => [ @create_columns ], - }; - $self->__column_userstamp_triggers($triggers); - } -} - -sub insert { - my $self = shift; - my $attrs = shift; + my ($self, @cols) = @_; + my @columns; - my $user_id = $self->get_current_user_id; + while (my $col = shift @cols) { + my $info = ref $cols[0] ? shift @cols : {}; - my @columns = @{ $self->__column_userstamp_triggers()->{on_create} }; - - foreach my $column ( @columns ) { - next if defined $self->get_column( $column ); - my $accessor = $self->column_info($column)->{accessor} || $column; - $self->$accessor($user_id); - } - - return $self->next::method(@_); -} - -sub update { - my $self = shift; - - my $user_id = $self->get_current_user_id; - my %dirty = $self->get_dirty_columns(); - my @columns = @{ $self->__column_userstamp_triggers()->{on_update} }; + if ( delete $info->{store_user_on_update} ) { + $info->{dynamic_default_on_update} = 'get_current_user_id'; + } + if ( delete $info->{store_user_on_create} ) { + $info->{dynamic_default_on_create} = 'get_current_user_id'; + } - foreach my $column ( @columns ) { - next if exists $dirty{ $column }; - my $accessor = $self->column_info($column)->{accessor} || $column; - $self->$accessor($user_id); + push @columns, $col => $info; } - return $self->next::method(@_); + return $self->next::method(@columns); } =head1 METHODS