krolow/Attach — pull request #17 by moacirosa (2013-02-20 19:09:47+00:00)
Title: Make possible define a method to custom filenames

Using the Cake Behavior's behavior the idea here is make possible to write a method in the model itself. It was already possible to custom the filename using the $actsAs attribute like:

```php
public $actsAs = array(
'Attach.Upload' => array(
'xml' => array(
'dir' => 'webroot{DS}files{DS}news',
'filename' => 'not-so-cool-custom-name'
)
)
);
```

Its still working but will be overwritten in case you implement your custom method in the specific model like:

```php
public function generateName($type, $index = null)
{
$dir = $this->getUploadFolder($type);

$customName = 'xmlReallyCoolCustom';

if (is_null($index)) {

$clientName = $this->data[$this->alias][$type]['name'];

$extension = $this->getFileExtension(
$clientName
);

} else {
$extension = $this->getFileExtension(
$this->data[$this->alias][$type][$index]['name']
);
}

if (!is_null($index)) {
return $dir
. $type
. '_'
. $index
. '_'
. $this->id
. '.'
. $extension;
}

return $dir . $customName . '_' . $this->id . '.' . $extension;
}
```

As we still receive $type parameter we could handle multiple file inside our custom method through some logic (switches, if's...) and if the model doesn't have this method the default name, from Behavior, will be used.

What do you think about it? Hope it helps!