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 |
default is defer { DateTime->now }, |
44 |
filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ); |
45 |
|
46 |
}; |
47 |
|
48 |
sub since { '0.0.1' } |
49 |
|
50 |
# Your model-specific methods go here. |
51 |
|
52 |
sub current_user_can { |
53 |
my $self = shift; |
54 |
my $what = shift; |
55 |
|
56 |
my $uid = Jifty->web->current_user->id; |
57 |
|
58 |
# superuser |
59 |
return 1 if ( defined $uid && $uid == 0 ); |
60 |
|
61 |
return 1 if ( $uid && $what eq 'create' ); |
62 |
|
63 |
return 1 if ( $uid && ! $self->id ); |
64 |
|
65 |
my $owner_id = $self->in_feed->owner->id; |
66 |
|
67 |
# Jifty->log->debug("current_user[$uid]_can($what) on Item ", $self->id || '???', " owner ", $owner_id || '???'); |
68 |
|
69 |
return 1 if ( $uid && $owner_id && $uid == $owner_id ); |
70 |
|
71 |
# system user |
72 |
return 1 if ( defined( $uid ) && $uid == 0 ); |
73 |
|
74 |
warn "$uid can't $what"; |
75 |
} |
76 |
|
77 |
sub _set { |
78 |
my $self = shift; |
79 |
my ($val, $msg) = $self->SUPER::_set(@_); |
80 |
|
81 |
$self->SUPER::_set(column => 'last_update', value => defer { DateTime->now }); |
82 |
|
83 |
return ($val, $msg); |
84 |
} |
85 |
|
86 |
1; |
87 |
|