Subject: | Timestamp plugin doesn't respect custom accessor name on column |
Hi,
If you set a custom accessor name for a column, rather than the default
method with the same name as the column, TimeStamp tries to use the
column name anyway.
I've written a test case which Carps if you try and call the column name
method instead of the accessor name. I've also written a patch which
checks for the existence of a custom accessor name and uses that in
preference to the column name.
The diff is against revision 3828. I hope it's useful.
Cheers
Carl
(IRC: CaptainCarlos)
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Carl Vincent http://www.netskills.ac.uk/ (URL)
Systems Manager 0191 222 5003 (voice)
Netskills, Newcastle University 0191 222 5001 (fax)
Training — Accreditation - Consultancy — Development
Subject: | timestamp.diff |
Index: t/10accessor.t
===================================================================
--- t/10accessor.t (revision 0)
+++ t/10accessor.t (revision 0)
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+use DateTime;
+use Time::HiRes;
+use Time::Warp qw|to time|;
+
+# Redefine "now" so that we can warp it.
+no warnings 'redefine';
+local *DateTime::now = sub { shift->from_epoch( epoch => (scalar time), @_ ) };
+use warnings 'redefine';
+
+use lib qw(t/lib);
+use DBIC::Test;
+
+my $schema = DBIC::Test->init_schema;
+my $row;
+
+$row = $schema->resultset('DBIC::Test::Schema::Accessor')
+ ->create({ display_name => 'test record' });
+
+my $time = $row->t_updated;
+ok $row->t_created, 'created timestamp';
+is $row->t_updated, $row->t_created, 'update and create timestamp';
+
+to(time + 60);
+
+$row->update({ display_name => 'updating test record' });
+
+is $row->display_name, 'updating test record', 'update record';
+isnt $row->t_updated, $time, 'timestamp update';
+
Property changes on: t/10accessor.t
___________________________________________________________________
Name: svn:executable
+ *
Index: t/lib/DBIC/Test/Schema/Accessor.pm
===================================================================
--- t/lib/DBIC/Test/Schema/Accessor.pm (revision 0)
+++ t/lib/DBIC/Test/Schema/Accessor.pm (revision 0)
@@ -0,0 +1,43 @@
+package #
+ DBIC::Test::Schema::Accessor;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->load_components(qw/TimeStamp PK::Auto Core/);
+__PACKAGE__->table('test_accessor');
+
+__PACKAGE__->add_columns(
+ 'pk1' => {
+ data_type => 'integer', is_nullable => 0, is_auto_increment => 1
+ },
+ display_name => { data_type => 'varchar', size => 128, is_nullable => 0 },
+ t_created => {
+ data_type => 'datetime', is_nullable => 0,
+ set_on_create => 1, accessor => 't_created_accessor',
+ },
+ t_updated => {
+ data_type => 'datetime', is_nullable => 0,
+ set_on_create => 1, set_on_update => 1, accessor => 't_updated_accessor',
+ },
+);
+
+__PACKAGE__->set_primary_key('pk1');
+
+no warnings 'redefine';
+
+sub t_created {
+ my $self = shift;
+ croak('Shouldnt be trying to update through t_created - should use accessor') if shift;
+
+ return $self->t_created_accessor();
+}
+
+sub t_updated {
+ my $self = shift;
+ croak('Shouldnt be trying to update through t_updated - should use accessor') if shift;
+
+ return $self->t_updated_accessor();
+}
+
+
+1;
Property changes on: t/lib/DBIC/Test/Schema/Accessor.pm
___________________________________________________________________
Name: svn:executable
+ *
Index: lib/DBIx/Class/TimeStamp.pm
===================================================================
--- lib/DBIx/Class/TimeStamp.pm (revision 3828)
+++ lib/DBIx/Class/TimeStamp.pm (working copy)
@@ -85,7 +85,8 @@
foreach my $column ( @columns ) {
next if defined $self->get_column( $column );
- $self->$column($now);
+ my $accessor = $self->column_info($column)->{accessor} || $column;
+ $self->$accessor($now);
}
return $self->next::method(@_);
@@ -100,7 +101,8 @@
foreach my $column ( @columns ) {
next if exists $dirty{ $column };
- $self->$column($now);
+ my $accessor = $self->column_info($column)->{accessor} || $column;
+ $self->$accessor($now);
}
return $self->next::method(@_);