1 package DBIx::Class::UserStamp;
3 use base qw(DBIx::Class);
7 use Carp qw/ confess /;
11 __PACKAGE__->load_components( qw/DynamicDefault/ );
15 DBIx::Class::UserStamp - Automatically set update and create user id fields
19 Automatically set fields on 'update' and 'create' that hold user id
20 values in a table. This can be used for any user id based
21 field that needs trigger like functionality when a record is
26 package MyApp::Schema;
28 __PACKAGE__->mk_group_accessors('simple' => qw/current_user/);
31 package MyApp::Model::MyAppDB;
33 use namespace::autoclean;
35 extends 'Catalyst::Model::DBIC::Schema';
38 traits => ['WithCurrentUser'], # Requires Catalyst::TraitFor::Model::DBIC::Schema::WithCurrentUser
41 package MyApp::Schema::SomeTable;
43 __PACKAGE__->load_components(qw( UserStamp ... Core ));
45 __PACKAGE__->add_columns(
46 id => { data_type => 'integer' },
47 u_created => { data_type => 'int', store_user_on_create => 1 },
48 u_updated => { data_type => 'int',
49 store_user_on_create => 1, store_user_on_update => 1 },
52 Now, any update or create actions will update the specified columns with the
53 current user_id, using the current_user_id accessor.
55 This is effectively trigger emulation to ease user id field insertion
60 my ($self, @cols) = @_;
63 while (my $col = shift @cols) {
64 my $info = ref $cols[0] ? shift @cols : {};
66 if ( delete $info->{store_user_on_update} ) {
67 $info->{dynamic_default_on_update} = 'get_current_user_id';
69 if ( delete $info->{store_user_on_create} ) {
70 $info->{dynamic_default_on_create} = 'get_current_user_id';
73 push @columns, $col => $info;
76 return $self->next::method(@columns);
81 =head2 get_current_user_id
83 This method is meant to be overridden. The default is to look for a
84 'current_user' accessor on the schema, and call an ->id method
89 sub get_current_user_id {
91 my $schema = $self->result_source->schema;
92 if ($schema->can('current_user')) {
93 return $schema->current_user->id
94 if $schema->current_user;
96 elsif ($schema->can('current_user_id')) {
97 return $schema->current_user_id;
100 confess("Your schema $schema does not have either a 'current_user' or 'current_user_id' accessors needed by " . __PACKAGE__);
106 Matt S. Trout <mst@shadowcatsystems.co.uk>
110 John Goulah <jgoulah@cpan.org>
111 Florian Ragwitz <rafl@debian.org>
115 This program is free software; you can redistribute
116 it and/or modify it under the same terms as Perl itself.