Show current_user as the preferred schema accessor
[dbsrgits/DBIx-Class-UserStamp.git] / README
1 NAME
2     DBIx::Class::UserStamp - Automatically set update and create user id
3     fields
4
5 DESCRIPTION
6     Automatically set fields on 'update' and 'create' that hold user id
7     values in a table. This can be used for any user id based field that
8     needs trigger like functionality when a record is added or updated.
9
10 SYNOPSIS
11      package MyApp::Schema;
12
13      __PACKAGE__->mk_group_accessors('simple' => qw/current_user_id/);
14
15      package MyApp::Model::MyAppDB;
16      use Moose;
17
18      around 'build_per_context_instance' => sub {
19        my ($meth, $self) = (shift, shift);
20        my ($c) = @_; # There are other params but we dont care about them
21        my $new = bless({ %$self }, ref($self));
22        my $user_info = $c->_user_in_session; 
23        my $user = $new->schema->resultset('User')->new_result({ %$user_info });
24        $new->schema->current_user_id($user->id) if (defined $user_info);
25        return $new;
26      };
27
28      package MyApp::Schema::SomeTable;
29
30      __PACKAGE__->load_components(qw( UserStamp ... Core ));
31  
32      __PACKAGE__->add_columns(
33         id => { data_type => 'integer' },
34         u_created => { data_type => 'int', store_user_on_create => 1 },
35         u_updated => { data_type => 'int',
36             store_user_on_create => 1, store_user_on_update => 1 },
37      );
38
39     Now, any update or create actions will update the specified columns with
40     the current user_id, using the current_user_id accessor.
41
42     This is effectively trigger emulation to ease user id field insertion
43
44 METHODS
45   get_current_user_id
46     This method is meant to be overridden. The default is to return a schema
47     accessor called current_user_id which should be populated as such.
48
49 AUTHOR
50      Matt S. Trout     <mst@shadowcatsystems.co.uk>
51
52 CONTRIBUTORS
53      John Goulah     <jgoulah@cpan.org>
54      Florian Ragwitz <rafl@debian.org>
55
56 COPYRIGHT
57     This program is free software; you can redistribute it and/or modify it
58     under the same terms as Perl itself.
59