Fix SYNOPSIS - RT#70156
[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;
0b751b44 32 use namespace::autoclean;
7bde9079 33
0b751b44 34 extends 'Catalyst::Model::DBIC::Schema';
35 with 'Catalyst::Component::InstancePerContext';
36
37 sub build_per_context_instance {
7bde9079 38 my ($meth, $self) = (shift, shift);
39 my ($c) = @_; # There are other params but we dont care about them
40 my $new = bless({ %$self }, ref($self));
0b751b44 41 my $user_info = $c->_user_in_session;
42 $new->schema($new->schema->clone);
7bde9079 43 my $user = $new->schema->resultset('User')->new_result({ %$user_info });
44 $new->schema->current_user_id($user->id) if (defined $user_info);
45 return $new;
0b751b44 46 }
7bde9079 47
48
49 package MyApp::Schema::SomeTable;
50
51 __PACKAGE__->load_components(qw( UserStamp ... Core ));
0b751b44 52
7bde9079 53 __PACKAGE__->add_columns(
54 id => { data_type => 'integer' },
55 u_created => { data_type => 'int', store_user_on_create => 1 },
56 u_updated => { data_type => 'int',
57 store_user_on_create => 1, store_user_on_update => 1 },
58 );
59
60Now, any update or create actions will update the specified columns with the
0b751b44 61current user_id, using the current_user_id accessor.
7bde9079 62
0b751b44 63This is effectively trigger emulation to ease user id field insertion
7bde9079 64
65=cut
66
67sub add_columns {
bb500ec5 68 my ($self, @cols) = @_;
69 my @columns;
7bde9079 70
bb500ec5 71 while (my $col = shift @cols) {
72 my $info = ref $cols[0] ? shift @cols : {};
7bde9079 73
bb500ec5 74 if ( delete $info->{store_user_on_update} ) {
75 $info->{dynamic_default_on_update} = 'get_current_user_id';
76 }
77 if ( delete $info->{store_user_on_create} ) {
78 $info->{dynamic_default_on_create} = 'get_current_user_id';
79 }
7bde9079 80
bb500ec5 81 push @columns, $col => $info;
7bde9079 82 }
83
bb500ec5 84 return $self->next::method(@columns);
7bde9079 85}
86
87=head1 METHODS
88
89=head2 get_current_user_id
90
91This method is meant to be overridden. The default is to return a
92schema accessor called current_user_id which should be populated as such.
93
94=cut
95sub get_current_user_id { shift->result_source->schema->current_user_id }
96
97=head1 AUTHOR
98
99 Matt S. Trout <mst@shadowcatsystems.co.uk>
100
c597fbb8 101=head1 CONTRIBUTORS
7bde9079 102
103 John Goulah <jgoulah@cpan.org>
c597fbb8 104 Florian Ragwitz <rafl@debian.org>
7bde9079 105
106=head1 COPYRIGHT
107
108This program is free software; you can redistribute
109it and/or modify it under the same terms as Perl itself.
110
111=cut
112
1131;