Add myself to CONTRIBUTORS.
[dbsrgits/DBIx-Class-UserStamp.git] / lib / DBIx / Class / UserStamp.pm
CommitLineData
7bde9079 1package DBIx::Class::UserStamp;
2
3use base qw(DBIx::Class);
4
5use warnings;
6use strict;
7
ac6874ee 8our $VERSION = '0.11';
7bde9079 9
bb500ec5 10__PACKAGE__->load_components( qw/DynamicDefault/ );
7bde9079 11
12=head1 NAME
13
14DBIx::Class::UserStamp - Automatically set update and create user id fields
15
16=head1 DESCRIPTION
17
18Automatically set fields on 'update' and 'create' that hold user id
19values in a table. This can be used for any user id based
20field that needs trigger like functionality when a record is
21added or updated.
22
23=head1 SYNOPSIS
24
25 package MyApp::Schema;
26
27 __PACKAGE__->mk_group_accessors('simple' => qw/current_user_id/);
28
29
30 package MyApp::Model::MyAppDB;
31 use Moose;
32
33 around 'build_per_context_instance' => sub {
34 my ($meth, $self) = (shift, shift);
35 my ($c) = @_; # There are other params but we dont care about them
36 my $new = bless({ %$self }, ref($self));
37 my $user_info = $c->_user_in_session;
38 my $user = $new->schema->resultset('User')->new_result({ %$user_info });
39 $new->schema->current_user_id($user->id) if (defined $user_info);
40 return $new;
41 };
42
43
44 package MyApp::Schema::SomeTable;
45
46 __PACKAGE__->load_components(qw( UserStamp ... Core ));
47
48 __PACKAGE__->add_columns(
49 id => { data_type => 'integer' },
50 u_created => { data_type => 'int', store_user_on_create => 1 },
51 u_updated => { data_type => 'int',
52 store_user_on_create => 1, store_user_on_update => 1 },
53 );
54
55Now, any update or create actions will update the specified columns with the
56current user_id, using the current_user_id accessor.
57
58This is effectively trigger emulation to ease user id field insertion
59
60=cut
61
62sub add_columns {
bb500ec5 63 my ($self, @cols) = @_;
64 my @columns;
7bde9079 65
bb500ec5 66 while (my $col = shift @cols) {
67 my $info = ref $cols[0] ? shift @cols : {};
7bde9079 68
bb500ec5 69 if ( delete $info->{store_user_on_update} ) {
70 $info->{dynamic_default_on_update} = 'get_current_user_id';
71 }
72 if ( delete $info->{store_user_on_create} ) {
73 $info->{dynamic_default_on_create} = 'get_current_user_id';
74 }
7bde9079 75
bb500ec5 76 push @columns, $col => $info;
7bde9079 77 }
78
bb500ec5 79 return $self->next::method(@columns);
7bde9079 80}
81
82=head1 METHODS
83
84=head2 get_current_user_id
85
86This method is meant to be overridden. The default is to return a
87schema accessor called current_user_id which should be populated as such.
88
89=cut
90sub get_current_user_id { shift->result_source->schema->current_user_id }
91
92=head1 AUTHOR
93
94 Matt S. Trout <mst@shadowcatsystems.co.uk>
95
c597fbb8 96=head1 CONTRIBUTORS
7bde9079 97
98 John Goulah <jgoulah@cpan.org>
c597fbb8 99 Florian Ragwitz <rafl@debian.org>
7bde9079 100
101=head1 COPYRIGHT
102
103This program is free software; you can redistribute
104it and/or modify it under the same terms as Perl itself.
105
106=cut
107
1081;