1 package SL::DB::Helper::AttrSorted;
4 use List::Util qw(max);
8 use parent qw(Exporter);
9 our @EXPORT = qw(attr_sorted);
12 my ($package, @attributes) = @_;
14 _make_sorted($package, $_) for @attributes;
18 my ($package, $attribute) = @_;
20 my %params = ref($attribute) eq 'HASH' ? %{ $attribute } : ( unsorted => $attribute );
21 my $unsorted_sub = $params{unsorted};
22 my $sorted_sub = $params{sorted} // $params{unsorted} . '_sorted';
23 my $position_sub = $params{position} // 'position';
27 *{ $package . '::' . $sorted_sub } = sub {
30 croak 'not an accessor' if @_ > 1;
32 my $next_position = (max map { $_->$position_sub // 0 } @{ $self->$unsorted_sub }) + 1;
35 sort { $a->[0] <=> $b->[0] }
36 map { [ $_->$position_sub // ($next_position++), $_ ] }
37 @{ $self->$unsorted_sub }
51 SL::DB::Helper::AttrSorted - Attribute helper for sorting to-many
52 relationships by a positional attribute
57 use SL::DB::Helper::AttrSorted;
58 __PACKAGE__->attr_sorted('items');
60 # Use in controller or whereever:
61 my $items = @{ $invoice->items_sorted };
65 Creates a function that returns a sorted relationship. Requires that
66 the linked objects have some kind of positional column.
68 Items for which no position has been set (e.g. because they haven't
69 been saved yet) are sorted last but kept in the order they appear in
76 =item C<attr_sorted @attributes>
78 Package method. Call with the names of the attributes for which the
79 helper methods should be created. Each attribute name can be either a
80 scalar or a hash reference if you need custom options.
82 If it's a hash reference then the following keys are supported:
86 =item * C<unsorted> is the name of the relationship accessor that
87 returns the list to be sorted. This is required, and if only a scalar
88 is given instead of a hash reference then that scalar value is
89 interpreted as C<unsorted>.
91 =item * C<sorted> is the name of the new function to create. It
92 defaults to the unsorted name postfixed with C<_sorted>.
94 =item * C<position> must be a function name to be called on the
95 objects to be sorted. It is supposed to return either C<undef> (no
96 position has been set yet) or a numeric value. Defaults to C<position>.
108 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>