aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/ResultSet/Body.pm
blob: 0aa3e82404a54d6987e5fd4d3a2f460377342fc4 (plain)
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
package FixMyStreet::DB::ResultSet::Body;
use base 'DBIx::Class::ResultSet';

use strict;
use warnings;
use POSIX qw(strcoll);

=head1 Name

FixMyStreet::DB::ResultSet::Body - a ResultSet class for the Body model.

=head1 Synopsis

    my @bodies = $rs->for_areas(1, 2)->active->with_area_count->translated->all_sorted;

=head1 Functions

=over

=item for_areas

This restricts the ResultSet to bodies covering the area IDs provided.

=cut

sub for_areas {
    my ( $rs, @areas ) = @_;

    my $result = $rs->search(
        { 'body_areas.area_id' => \@areas },
        { join => 'body_areas' }
    );
    return $result;
}

=item active

This restricts the ResultSet to bodies that are not marked as deleted.

=cut

sub active {
    my $rs = shift;
    $rs->search({ deleted => 0 });
}

=item translated

This joins the ResultSet to the translation table, adding the `msgstr`
column containing possible translations of the body name.

=cut

sub translated {
    my $rs = shift;
    my $schema = $rs->result_source->schema;
    $rs->search(undef, {
        '+columns' => { 'msgstr' => 'translations.msgstr' },
        join => 'translations',
        bind => [ 'name', $schema->lang, 'body' ],
    });
}

=item with_area_count

This adds the number of areas associated with each body to the ResultSet,
in the area_count column.

=cut

sub with_area_count {
    my $rs = shift;
    $rs->search(undef, {
        '+select' => [ { count => 'area_id' } ],
        '+as' => [ 'area_count' ],
        join => 'body_areas',
        distinct => 1,
    });
}

=item all_sorted

This returns all results, as C<all()>, but sorted by their name column
(which will be the translated names if present).

=back

=cut

sub all_sorted {
    my $rs = shift;
    my @bodies = $rs->all;
    @bodies = sort { strcoll($a->name, $b->name) } @bodies;
    return @bodies;
}

1;