The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Reaction::Manual::ActionPrototypes - Changes to the Action Prototype Mechanism

DESCRIPTION

After Reaction 0.001001 the API used to create links for different actions in the ViewPort::Collection::Grid changed significantly. The aim of the changes was to create a simpler API that was more concise, flexible, and didn't tie unneccessary controller logic in the ViewPort layer.

Major Changes

Controller Layer

Reaction::UI::Controller::Collection

The default display class for the list action is now Grid.
Addition of the default_member_actions and default_collection_actions
Addition of the _build_member_action_prototype and _build_collection_action_prototype methods. These are used by _build_action_viewport_args to create prototypes for collection and member actions.

Reaction::UI::Controller::Collection::CRUD

By default, enable create, update, delete, delete_all, actions.

ViewPort Layer

Reaction::UI::ViewPort::Collection::Grid

Add the member_action_count attribute. It allows the controller to know how many actions to expect to lay out the UI properly.
Default to member-class Grid::Member

Reaction::UI::ViewPort::Role::Actions

Completely revamped the action-prototypes, added ordering support and moved to using the new ViewPort::URI|Reaction::UI::ViewPort::URI.

Most notably action_prototypes is now a HASH ref.

Migration

In most cases, you shouldn't need to change much for migration, but if you had custom actions in your controllers that were linked to by the CRUD system, or you had excluded certain classes, you'll need to create some minor updates.

A custom collection action in your controller.

    #old code
    sub custom_action { ... }
    sub _build_action_viewport_map {
      my $map = shift->next::method(@_);
      $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
      return $map;
    }
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      my $custom_proto = {
        label => 'Create',
        action => sub { [ '', 'create',    $_[1]->req->captures ] } 
      };
      my $protos = $args->{list}->{action_prototypes};
      push(@$protos, $custom_proto);
      return $args;
    }

    #new code:
    sub custom_action { ... }
    sub _build_action_viewport_map {
      my $map = shift->next::method(@_);
      $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
      return $map;
    }
    sub _build_default_collection_actions {
      [ @{shift->next::method(@_)}, 'custom_action'];
    }

A custom member action in your controller.

    #old code
    sub custom_action { ... }
    sub _build_action_viewport_map {
      my $map = shift->next::method(@_);
      $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
      return $map;
    }
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      my $custom_proto = {
        label => 'Create',
        action => sub { [ '', 'create',    $_[1]->req->captures ] } 
      };
      my $protos = $args->{list}->{Member}->{action_prototypes};
      push(@$protos, $custom_proto);
      return $args;
    }

    #new code:
    sub custom_action { ... }
    sub _build_action_viewport_map {
      my $map = shift->next::method(@_);
      $map->{custom_action} = 'Reaction::UI::ViewPort::Action';
      return $map;
    }
    sub _build_default_member_actions {
      [ @{shift->next::method(@_)}, 'custom_action'];
    }

Disabling a default collection action

    #old code
    sub delete_all {}
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      #remove the delete all action
      my $protos = $args->{list}->{action_prototypes};
      @$protos = grep { $_->{label} !~ /Delete all/i } @$protos;
      return $args;
    }

    #new code
    sub delete_all {}
    sub _build_default_collection_actions {
      [ grep {$_ ne 'delete_all'} @{ shift->next::method(@_) } ];
    }

    #or ...
    sub delete_all {}
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      my $protos = $args->{list}->{action_prototypes};
      delete $protos->{delete_all};
      return $args;
    }

Changing the label of a collection action

    #old code
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      my $protos = $args->{list}->{action_prototypes};
      $proto = grep { $_->{label} eq 'Delete all' } @$protos;
      $proto->{label} = 'New Label';
      return $args;
    }

    #new code
    sub delete_all {}
    sub _build_action_viewport_args {
      my $args = shift->next::method(@_);
      my $protos = $args->{list}->{action_prototypes};
      $proto->{delete_all}->{label} = 'New Label';
      return $args;
    }

    #or ...
    __PACKAGE__->config(action => { list => { ViewPort => {
        action_prototypes => { delete_all => {label => 'New Label'} }
      },
    );