Jifty/Plugin/ActorMetadata/Mixin/Model/ActorMetadata.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 260 CC 18 SCAR 34.9
CC
18
Cov
0%
import
90.0
before_create
12.0
after_set
12.0
CRAP 32.7
1 use strict;
2 use warnings;
3  
4 package Jifty::Plugin::ActorMetadata::Mixin::Model::ActorMetadata;
5 use Jifty::DBI::Schema;
6 use base 'Jifty::DBI::Record::Plugin';
7  
8 our @EXPORT = qw(current_user_can current_user_is_owner);
9 my %column_names;
10  
11 =head1 NAME
12  
13 Jifty::Plugin::ActorMetadata::Mixin::Model::ActorMetadata - ActorMetadata mixin
14  
15 =head1 SYNOPSIS
16  
17 package MyApp::Model::CoffeeShop;
18 use Jifty::DBI::Schema;
19 use MyApp::Record schema {
20 # custom column definitions
21 };
22  
23 use Jifty::Plugin::ActorMetadata::Mixin::Model::ActorMetadata; # created_by, created_on, updated_on and updated_by
24  
25 =head1 DESCRIPTION
26  
27 =head1 SCHEMA
28  
29 This mixin adds the following columns to the model schema:
30  
31 =head2 created_by
32  
33 =head2 created_on
34  
35 =head2 updated_on
36  
37 =head2 updated_by
38  
39 =cut
40  
41 # XXX: move this to somewhere
42 BEGIN {
43  
44 Jifty::DBI::Schema->register_types(
45 Date =>
46 sub { type is 'date', input_filters are qw/Jifty::DBI::Filter::Date/ },
47 Time =>
48 sub { type is 'time', input_filters are qw/Jifty::DBI::Filter::Time/ },
49 DateTime => sub {
50 type is 'datetime',
51 input_filters are qw/Jifty::DBI::Filter::DateTime/ },
52 TimeStamp => sub {
53 type is 'timestamp',
54 filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime),
55 }
56 );
57 }
58  
59 =head1 METHODS
60  
61 # XXX: pod coverage should count parent classes. this pod is useless
62  
63 =head2 register_triggers
64  
65 Adds the triggers to the model this mixin is added to.
66  
67 =cut
68  
69 sub register_triggers {
70 my $self = shift;
71 $self->add_trigger(name => 'before_create', callback => \&before_create);
72 }
73  
74 =head2 register_triggers_for_column
75  
76 =cut
77  
78 sub register_triggers_for_column {
79 my $self = shift;
80 my $column = shift;
81  
82 return
83 unless $column_names{ ref $self || $self }{'updated_on'}
84 && $column eq $column_names{ ref $self || $self }{'updated_on'}
85 || $column_names{ ref $self || $self }{'updated_by'}
86 && $column eq $column_names{ ref $self || $self }{'updated_by'};
87  
88 $self->add_trigger(name => 'after_set', callback => \&after_set);
89 return 1;
90 }
91  
92 =head2 before_create
93  
94 Sets C<created_by>, C<created_on>, C<updated_on> and C<updated_by> based on the current user and time.
95  
96 =cut
97  
98 sub before_create {
99 my $self = shift;
100 my $args = shift;
101 for my $by (qw/created_by updated_by/) {
102 if ( $column_names{ ref $self || $self }{$by} ) {
103 $args->{$column_names{ ref $self || $self }{$by}} ||= $self->current_user->id;
104 }
105 }
106  
107 for my $time ( qw/created_on updated_on/ ) {
108 if ( $column_names{ ref $self || $self }{$time} ) {
109 $args->{$column_names{ ref $self || $self }{$time}} ||= Jifty::DateTime->now;
110 }
111 }
112  
113 return 1;
114 }
115  
116 =head2 after_set
117  
118 update C<updated_on> and C<updated_by> based on the current user and current time.
119  
120 =cut
121  
122 sub after_set {
123 my $self = shift;
124 if ( $column_names{ ref $self || $self }{'updated_on'} ) {
125 $self->__set(
126 column => $column_names{ ref $self || $self }{'updated_on'},
127 value => Jifty::DateTime->now
128 );
129 }
130 if ( $column_names{ ref $self || $self }{'updated_by'} ) {
131 $self->__set(
132 column => $column_names{ ref $self || $self }{'updated_by'},
133 value => $self->current_user->id
134 );
135 }
136  
137 return 1;
138 }
139  
140 =head2 current_user_can
141  
142 Rejects creation unless there's a current_user.
143  
144 =cut
145  
146 # XXX: Move this to an abortable trigger
147  
148 sub current_user_can {
149 my $self = shift;
150 my $action = shift;
151 my %args = (@_);
152  
153 if ($action eq 'create') {
154 return
155 unless $self->current_user
156 and $self->current_user->id
157 || $self->current_user->is_bootstrap_user;
158 }
159  
160 #Rejects update or deletion unless the current_user is the creator. (Jesse says: this feels like wrong logic for this mixin)
161 # if ($action eq 'update' or $action eq 'delete') {
162 # return undef unless $self->current_user_is_owner;
163 # }
164  
165 return 1;
166 }
167  
168 =head2 current_user_is_owner
169  
170 =cut
171  
172 sub current_user_is_owner {
173 my $self = shift;
174  
175 my $created_by = $self->__value($column_names{ref $self || $self}{'created_by'});
176 return unless $self->current_user && $created_by;
177  
178 return unless $self->current_user->id;
179  
180 return $self->current_user->id == $created_by;
181 }
182  
183 =head2 import
184  
185 to be more flexible, we allow some configurations like:
186 e.g.
187 use Jifty::Plugin::ActorMetadata::Mixin::Model::ActorMetadata
188 user_class => 'Foo::Model::Principal',
189 map => { created_by => 'creator', created_on => 'created' }
190  
191 current valid args are:
192 user_class => 'Foo::Model::User'
193 class that you want created_by and updated_by to be refers_to
194 map => { created_by => 'creator', ... }
195 the real column name you want to use. this also controls whether
196 a column will be added or not. i.e. if the hashref is
197 { created_by => 'creator', created_on => 'created' }, then columns
198 'updated_by' and 'updated_on' will not be added.
199  
200 =cut
201  
202 sub import {
203 my $self = shift;
204 my %args = @_;
205 my $user_class = $args{'user_class'} || Jifty->app_class('Model', 'User');
206  
207 my @columns = qw/created_on created_by updated_on updated_by/;
208 my %map;
209  
210 # fiddle map
211 if ( $args{'map'} && ref $args{'map'} eq 'HASH' ) {
212 for my $column ( keys %{$args{'map'}} ) {
213 $map{$column} = $args{'map'}{$column};
214 }
215 }
216 else {
217 @map{@columns} = @columns;
218 }
219  
220 $column_names{scalar caller} = \%map;
221  
222 Jifty::DBI::Schema->import; # to import subs like schema, references
223 my @ret = schema {
224 if ( $map{'created_by'} ) {
225 column $map{'created_by'} => references $user_class,
226 render_as 'hidden';
227 }
228 if ( $map{'created_on'} ) {
229 column $map{'created_on'} => is TimeStamp,
230 render_as 'hidden';
231 }
232 if ( $map{'updated_by'} ) {
233 column $map{'updated_by'} => references $user_class,
234 render_as 'hidden';
235 }
236 if ( $map{'updated_on'} ) {
237 column $map{'updated_on'} => is TimeStamp,
238 render_as 'hidden';
239 }
240 };
241 require Jifty::Record;
242 Jifty::Record->import( @ret );
243
244 # TODO
245 # below is the import sub from Jifty::DBI::Record::Plugin,
246 # because of some caller stuff, I can't just call SUPER
247 # need to refactor, either here or Jifty::DBI::Record::Plugin
248 my $caller = caller;
249  
250 for ($self->columns) {
251 $caller->_init_methods_for_column($_);
252 # virtual will be handled later
253 $caller->COLUMNS->{$_->name} = $_ unless $_->virtual;
254 }
255 $self->export_to_level(1,undef);
256  
257 if (my $triggers = $self->can('register_triggers') ) {
258 $triggers->($caller)
259 }
260  
261 if (my $triggers_for_column = $self->can('register_triggers_for_column') ) {
262 for my $column (keys %{$caller->_columns_hashref}) {
263 $triggers_for_column->($caller, $column)
264 }
265 }
266 push(@{ $caller->RECORD_MIXINS }, $self);
267 $self->COLUMNS(undef); # reset columns for ActorMetadata.pm
268 }
269  
270 1;