1 |
use strict; |
2 |
use warnings; |
3 |
|
4 |
package Grep::Model::Item; |
5 |
use Jifty::DBI::Schema; |
6 |
|
7 |
use Grep::Model::Feed; |
8 |
|
9 |
use Grep::Record schema { |
10 |
|
11 |
column in_feed => |
12 |
refers_to Grep::Model::Feed by 'id', |
13 |
is mandatory; |
14 |
|
15 |
column title => |
16 |
type is 'text', |
17 |
is mandatory; |
18 |
|
19 |
column link => |
20 |
type is 'text', |
21 |
is mandatory; |
22 |
|
23 |
column content => |
24 |
type is 'text', |
25 |
is mandatory; |
26 |
|
27 |
column summary => |
28 |
type is 'text'; |
29 |
|
30 |
column category => |
31 |
type is 'text'; |
32 |
|
33 |
column author => |
34 |
type is 'text'; |
35 |
|
36 |
column created_on => |
37 |
type is 'timestamp', |
38 |
default is defer { DateTime->now }, |
39 |
filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ); |
40 |
|
41 |
column last_update => |
42 |
type is 'timestamp', |
43 |
filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ); |
44 |
|
45 |
}; |
46 |
|
47 |
sub since { '0.0.1' } |
48 |
|
49 |
# Your model-specific methods go here. |
50 |
|
51 |
sub current_user_can { |
52 |
my $self = shift; |
53 |
my $what = shift; |
54 |
|
55 |
my $uid = Jifty->web->current_user->id; |
56 |
|
57 |
# superuser |
58 |
return 1 if ( defined $uid && $uid == 0 ); |
59 |
|
60 |
return 1 if ( $uid && $what eq 'create' ); |
61 |
|
62 |
return 1 if ( $uid && ! $self->id ); |
63 |
|
64 |
my $owner_id = $self->in_feed->owner->id; |
65 |
|
66 |
# Jifty->log->debug("current_user[$uid]_can($what) on Item ", $self->id || '???', " owner ", $owner_id || '???'); |
67 |
|
68 |
return 1 if ( $uid && $owner_id && $uid == $owner_id ); |
69 |
|
70 |
# system user |
71 |
return 1 if ( defined( $uid ) && $uid == 0 ); |
72 |
|
73 |
warn "$uid can't $what"; |
74 |
} |
75 |
|
76 |
sub _set { |
77 |
my $self = shift; |
78 |
my ($val, $msg) = $self->SUPER::_set(@_); |
79 |
|
80 |
$self->SUPER::_set(column => 'last_update', value => defer { DateTime->now }); |
81 |
|
82 |
return ($val, $msg); |
83 |
} |
84 |
|
85 |
1; |
86 |
|