my ($class, @params) = @_;
my $importing = caller();
+ configure_acts_as_list($importing, @params);
+
$importing->before_save( sub { SL::DB::Helper::ActsAsList::set_position(@_) });
$importing->before_delete(sub { SL::DB::Helper::ActsAsList::remove_position(@_) });
- # Use 'goto' so that Exporter knows which module to import into via
- # 'caller()'.
- goto &Exporter::import;
+ # Don't 'goto' to Exporters import, it would try to parse @params
+ __PACKAGE__->export_to_level(1, $class, @EXPORT);
}
#
croak "Invalid parameter 'position'" unless ($params{position} || '') =~ m/^ (?: before | after | first | last ) $/x;
+ my $column = column_name($self);
+
+ $self->remove_from_list if ($self->$column // -1) != -1;
+
if ($params{position} eq 'last') {
set_position($self);
$self->save;
my $table = $self->meta->table;
my $primary_key_col = ($self->meta->primary_key)[0];
- my $column = column_name($self);
my ($group_by, @values) = get_group_by_where($self);
$group_by = " AND ${group_by}" if $group_by;
my $new_position;
=head1 SYNOPSIS
package SL::DB::SomeObject;
- use SL::DB::Helper::ActsAsList;
+ use SL::DB::Helper::ActsAsList [ PARAMS ];
package SL::Controller::SomeController;
...
When the object is deleted all positions greater than the object's old
position are decreased by one.
-The column name to use can be configured via L<configure_acts_as_list>.
+C<PARAMS> will be given to L<configure_acts_as_list> and can be used to
+set the column name.
=head1 CLASS FUNCTIONS
=item C<configure_acts_as_list %params>
-Configures the mixin's behaviour. C<%params> can contain the following
-values:
+Configures the mixin's behaviour. Will get called automatically with the
+include parameters. C<%params> can contain the following values:
=over 2
referenced item by shifting all the appropriate item positions up by
one.
+If C<$self>'s positional column is already set when this function is
+called then L</remove_from_list> will be called first before anything
+else is done.
+
After this function C<$self>'s positional column has been set and
saved to the database.