aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/ResultSet/Body.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet/Body.pm')
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/Body.pm75
1 files changed, 72 insertions, 3 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Body.pm b/perllib/FixMyStreet/DB/ResultSet/Body.pm
index e79d038b1..0aa3e8240 100644
--- a/perllib/FixMyStreet/DB/ResultSet/Body.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/Body.pm
@@ -3,6 +3,25 @@ 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 ) = @_;
@@ -14,14 +33,64 @@ sub for_areas {
return $result;
}
-sub all_translated {
+=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;
- my @bodies = $rs->search(undef, {
+ $rs->search(undef, {
'+columns' => { 'msgstr' => 'translations.msgstr' },
join => 'translations',
bind => [ 'name', $schema->lang, 'body' ],
- })->all;
+ });
+}
+
+=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;
}