Skip to content

Commit

Permalink
Update behavior generation and some internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 18, 2014
1 parent 7b096f0 commit eea4468
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
63 changes: 32 additions & 31 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -124,9 +124,13 @@ public function generate($name) {
$displayField = $this->getDisplayField($model);
$fields = $this->getFields($model);
$validation = $this->getValidation($model);
$behaviors = $this->getBehaviors($model);

$this->bake($object, false);
$this->bakeFixture($model, $useTable);
$data = compact('associations', 'primaryKey', 'displayField',
'fields', 'validation', 'behaviors');
$this->bakeEntity($object);
$this->bakeTable($object);
$this->bakeFixture($model, $table);
$this->bakeTest($model);
}

Expand All @@ -144,11 +148,7 @@ public function all() {
}
$modelClass = Inflector::classify($table);
$this->out(__d('cake_console', 'Baking %s', $modelClass));
$object = $this->_getModelObject($modelClass, $table);
if ($this->bake($object, false) && $unitTestExists) {
$this->bakeFixture($modelClass, $table);
$this->bakeTest($modelClass);
}
$this->generate($table);
}
}

Expand Down Expand Up @@ -440,6 +440,31 @@ public function fieldValidation($fieldName, $metaData, $primaryKey) {
];
}

/**
* Get behaviors
*
* @param Cake\ORM\Table $model
* @return array Behaviors
*/
public function getBehaviors($model) {
$behaviors = [];
$schema = $model->schema();
$fields = $schema->columns();
if (empty($fields)) {
return [];
}
if (in_array('created', $fields) || in_array('modified', $fields)) {
$behaviors[] = 'Timestamp';
}

if (in_array('lft', $fields) && $schema->columnType('lft') === 'integer' &&
in_array('rght', $fields) && $schema->columnType('rght') === 'integer' &&
in_array('parent_id', $fields)
) {
$behaviors[] = 'Tree';
}
return $behaviors;
}

/**
* Generate a key value list of options and a prompt.
Expand Down Expand Up @@ -575,30 +600,6 @@ protected function _interactive() {
}
}

/**
* Handles behaviors
*
* @param Model $model
* @return array Behaviors
*/
public function doActsAs($model) {
if (!$model instanceof Model) {
return false;
}
$behaviors = [];
$fields = $model->schema(true);
if (empty($fields)) {
return [];
}

if (isset($fields['lft']) && $fields['lft']['type'] === 'integer' &&
isset($fields['rght']) && $fields['rght']['type'] === 'integer' &&
isset($fields['parent_id'])) {
$behaviors[] = 'Tree';
}
return $behaviors;
}

/**
* Assembles and writes a Model file.
*
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Console/Command/Task/ModelTaskTest.php
Expand Up @@ -373,14 +373,14 @@ public function testGetValidation() {
*
* @return void
*/
public function testDoActsAs() {
$this->markTestIncomplete('Not done here yet');
$this->Task->connection = 'test';
$this->Task->interactive = false;
$model = new Model(array('ds' => 'test', 'name' => 'NumberTree'));
$result = $this->Task->doActsAs($model);
public function testGetBehaviors() {
$model = TableRegistry::get('NumberTrees');
$result = $this->Task->getBehaviors($model);
$this->assertEquals(['Tree'], $result);

$this->assertEquals(array('Tree'), $result);
$model = TableRegistry::get('BakeArticles');
$result = $this->Task->getBehaviors($model);
$this->assertEquals(['Timestamp'], $result);
}

/**
Expand Down

0 comments on commit eea4468

Please sign in to comment.