cleanup of hard to read layout_args code
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Role / Actions.pm
index f1f731c..f2d40fb 100644 (file)
@@ -16,6 +16,10 @@ has action_order => (
   isa => 'ArrayRef'
 );
 
+has action_filter => (
+  isa => 'CodeRef', is => 'ro',
+);
+
 has action_prototypes => (
   is => 'ro',
   isa => 'HashRef',
@@ -29,13 +33,21 @@ has computed_action_order => (
   lazy_build => 1
 );
 
+sub _filter_action_list {
+    my $self = shift;
+    my $actions = [keys %{$self->action_prototypes}];
+    return $self->has_action_filter ?
+        $self->action_filter->($actions, $self->model)
+        : $actions;
+}
+
 sub _build_computed_action_order {
   my $self = shift;
   my $ordered = $self->sort_by_spec(
     ($self->has_action_order ? $self->action_order : []),
-    [ keys %{ $self->action_prototypes } ]
+    $self->_filter_action_list
   );
-  return $ordered ;
+  return $ordered;
 }
 
 sub _build_actions {
@@ -49,11 +61,23 @@ sub _build_actions {
     my $proto = $self->action_prototypes->{$proto_name};
     my $uri = $proto->{uri} or confess('uri is required in prototype action');
     my $label = exists $proto->{label} ? $proto->{label} : $proto_name;
+    my $layout = exists $proto->{layout} ? $proto->{layout} : 'uri';
+
+    my $layout_args;
+    if( exists $proto->{layout_args} ){
+      if( ref($proto->{layout_args}) eq 'CODE' ){
+        $layout_args = $proto->{layout_args}->($target, $ctx);
+      } else {
+        $layout_args = $proto->{layout_args};
+      }
+    }
 
     my $action = Reaction::UI::ViewPort::URI->new(
       location => join ('-', $loc, 'action', $i++),
       uri => ( ref($uri) eq 'CODE' ? $uri->($target, $ctx) : $uri ),
       display => ( ref($label) eq 'CODE' ? $label->($target, $ctx) : $label ),
+      layout => $layout,
+      ( ref($layout_args) eq ' HASH' ? layout_args => $layout_args : () ),
     );
     push(@act, $action);
   }