This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Project.pm
257 lines (184 loc) Β· 7.35 KB
/
Project.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package Coocook::Schema::Result::Project;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
use Carp;
use Coocook::Util;
use DateTime;
extends 'Coocook::Schema::Result';
__PACKAGE__->table('projects');
__PACKAGE__->add_columns(
id => { data_type => 'integer', is_auto_increment => 1 },
name => { data_type => 'text' },
url_name => { data_type => 'text' },
url_name_fc => { data_type => 'text' }, # fold cased
description => { data_type => 'text' },
is_public => { data_type => 'boolean', default_value => 1 },
owner_id => { data_type => 'integer' },
created => {
data_type => 'timestamp without time zone',
default_value => \'CURRENT_TIMESTAMP',
set_on_create => 1,
},
archived => { data_type => 'timestamp without time zone', is_nullable => 1 },
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraints( ['name'], ['url_name'], ['url_name_fc'] );
__PACKAGE__->belongs_to( owner => 'Coocook::Schema::Result::User', 'owner_id' );
__PACKAGE__->has_many(
organizations_projects => 'Coocook::Schema::Result::OrganizationProject',
'project_id'
);
__PACKAGE__->many_to_many( organizations => organizations_projects => 'organization' );
__PACKAGE__->has_many( projects_users => 'Coocook::Schema::Result::ProjectUser', 'project_id' );
__PACKAGE__->many_to_many( users => projects_users => 'user' );
__PACKAGE__->has_many( articles => 'Coocook::Schema::Result::Article', 'project_id' );
__PACKAGE__->has_many( meals => 'Coocook::Schema::Result::Meal', 'project_id' );
__PACKAGE__->has_many( purchase_lists => 'Coocook::Schema::Result::PurchaseList', 'project_id' );
__PACKAGE__->has_many( quantities => 'Coocook::Schema::Result::Quantity', 'project_id' );
__PACKAGE__->has_many( recipes => 'Coocook::Schema::Result::Recipe', 'project_id' );
__PACKAGE__->has_many( shop_sections => 'Coocook::Schema::Result::ShopSection', 'project_id' );
__PACKAGE__->has_many( tags => 'Coocook::Schema::Result::Tag', 'project_id' );
__PACKAGE__->has_many( tag_groups => 'Coocook::Schema::Result::TagGroup', 'project_id' );
__PACKAGE__->has_many( units => 'Coocook::Schema::Result::Unit', 'project_id' );
# trigger for generating url_name[_fc]
before store_column => sub {
my ( $self, $column, $value ) = @_;
if ( $column eq 'name' ) {
$self->set_columns( Coocook::Util::url_names_hashref($value) );
}
};
__PACKAGE__->meta->make_immutable;
sub archive {
my $self = shift;
$self->archived
and croak "Project already archived";
$self->update( { archived => DateTime->now() } );
}
sub unarchive {
my $self = shift;
$self->archived
or croak "Project not archived";
$self->update( { archived => undef } );
}
# pseudo-relationship
sub dishes {
my $self = shift;
return $self->result_source->schema->resultset('Dish')->search(
{
'meal.project_id' => $self->id,
},
{
join => 'meal',
}
);
}
# fetch articles, units and cache their relationships
sub articles_cached_units {
my $self = shift;
my $articles = $self->articles;
my @articles = $articles->sorted->all;
my @units = $self->units->sorted->all;
my %units = map { $_->id => $_ } @units;
my %units_with_articles;
my %articles_units;
{
my $articles_units = $articles->search_related('articles_units');
while ( my $a_u = $articles_units->next ) {
my $a = $a_u->article_id;
my $u = $a_u->unit_id;
$a_u->related_resultset('unit')->set_cache( [ $units{$u} ] );
push @{ $articles_units{$a} }, $a_u;
$units_with_articles{$u}++;
}
}
for my $article (@articles) {
$article->related_resultset('articles_units')->set_cache( $articles_units{ $article->id } );
}
if (wantarray) {
# this is probably (?) faster than additionally querying the relationship in SQL
# TODO speed up
return \@articles, [ grep { exists $units_with_articles{ $_->id } } @units ];
}
else {
return \@articles;
}
}
=head2 dish_ingredients()
Shortcut.
=cut
sub dish_ingredients { shift->meals->search_related('dishes')->search_related('ingredients') }
=head2 inventory()
Returns a hashref with counts for related object.
=cut
sub inventory {
my $self = shift;
my $self_rs = $self->self_rs;
return $self_rs->search(
undef,
{
columns => {
articles => $self_rs->search_related('articles')->count_rs->as_query,
dishes => $self_rs->search_related('meals')->search_related('dishes')->count_rs->as_query,
meals => $self_rs->search_related('meals')->count_rs->as_query,
purchase_lists => $self_rs->search_related('purchase_lists')->count_rs->as_query,
quantities => $self_rs->search_related('quantities')->count_rs->as_query,
recipes => $self_rs->search_related('recipes')->count_rs->as_query,
shop_sections => $self_rs->search_related('shop_sections')->count_rs->as_query,
tags => $self_rs->search_related('tags')->count_rs->as_query,
tag_groups => $self_rs->search_related('tag_groups')->count_rs->as_query,
units => $self_rs->search_related('units')->count_rs->as_query,
unassigned_items =>
$self_rs->search_related('meals')->search_related('dishes')->search_related('ingredients')
->unassigned->count_rs->as_query,
},
}
)->hri->single;
}
=head2 is_stale()
Returns a boolean value indicating whether the whole project is already past.
Indicates if this project can be archived.
=cut
sub is_stale {
my ( $self, $pivot_date ) = @_;
return $self->self_rs->stale($pivot_date)->results_exist();
}
=head2 tags_from_names($names)
Returns a resultset with all tags matching names in C<$names>.
TODO: Should probably autocreate tags that do not yet exist.
=cut
sub tags_from_names {
my ( $self, $names ) = @_;
return $self->tags->from_names($names);
}
=head2 organizations_without_permission
Returns a resultset with all C<Result::Organization>s without any related C<organizations_projects> record.
=cut
sub organizations_without_permission {
my $self = shift;
my $permitted_organizations = $self->organizations_projects->get_column('organization_id');
return $self->result_source->schema->resultset('Organization')->search(
{
id => { -not_in => $permitted_organizations->as_query },
}
);
}
=head2 other_projects
Returns a resultset to all projects except itself.
=cut
sub other_projects {
my $self = shift;
return $self->result_source->resultset->search( { id => { '!=' => $self->id } } );
}
=head2 users_without_permission
Returns a resultset with all C<Result::User>s without any related C<projects_users> record.
=cut
sub users_without_permission {
my $self = shift;
my $permitted_users = $self->projects_users->get_column('user_id');
return $self->result_source->schema->resultset('User')->search(
{
id => { -not_in => $permitted_users->as_query },
}
);
}
1;