lazychaser/cruddy — pull request #75 by lazychaser (2014-11-04 14:24:37+00:00)
Title: Actions

This is a branch that supports extra actions for entities. See #67.

### Defining actions

The shema now has additional method `actions`, where you can specify additional actions, that can be performed when saving the model. In general, action is a class that implements `Kalnoy\Cruddy\Actions\Action` interface. There is a convinient stub for actions, based on Fluent class. Here's the example of basic action:

```php
$actions->define('foo', function ($model)
{
$model->bar = 'baz';
});
```

It's basically an id and a callback, that is executed before model is saved. The callback can be a class name, that is resolved from the IoC:

```php
$actions->define('foo', 'FooAction');
```

```php
class FooAction
{
public function execute($model)
{
$model->foo = 'bar';
}
}
```

You can set `title` of the action:

```php
$actions->define(...)->title('Static Title');
$actions->define(...)->title(function ($model) { return $model->title; });
```

You can also specifiy whether the action is disabled or hidden. Same rules as for title: it can be a static value or a closure.

```php
$action->disable(function ($model) { return $model->is_active; });
$action->hide(function ($model) { return $model->is_hidden; });
```

#### Reusable actions

It is possible to reuse actions across entities by extracting it to separate class, that implements `Kalnoy\Cruddy\Actions\Action` interface:

```php
$actions->add('FooAction');
$actions->add(new BarAction('some parameter'));
```

### An example from test app:

```php
protected function actions($actions)
{
$actions->define('reset_price', function ($model)
{
$model->price = 0;
})
->title(function ($model)
{
return "Reset price from ".$model->price." to zero";
})
->disable(function ($model)
{
return $model->price == 0;
});

$actions->define('random_price', function ($model)
{
$model->price = rand(1, 150);
})
->title('Randomize price');

$actions->add('TestAction');
}
```

Note that many things have been rewritten and refactored, so there might be some incomapatibilies.

I appreciate your feedback!