diff options
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Body.pm | 75 |
2 files changed, 75 insertions, 5 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm index e5cd2b907..07bea276c 100644 --- a/perllib/FixMyStreet/DB/Result/Body.pm +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -158,10 +158,11 @@ sub areas { sub first_area_children { my ( $self ) = @_; - my $area_id = $self->body_areas->first->area_id; + my $body_area = $self->body_areas->first; + return unless $body_area; my $cobrand = $self->result_source->schema->cobrand; - my $children = mySociety::MaPit::call('area/children', $area_id, + my $children = mySociety::MaPit::call('area/children', $body_area->area_id, type => $cobrand->area_types_children, ); 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; } |