Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature/allow change model #26

Merged
merged 2 commits into from

3 participants

@jippi

No description provided.

@kimegede

:+1: Looks good

@lorenzo

The on;y thing I don't like is that config('modelName.action') is rather obscure and hard to remember. I'd like a method for it like mapActionView()

@jippi

it's ->config('modelMap.$action', $model); though

@lorenzo
@jippi

lol

@jippi jippi merged commit 39b9a37 into from
@jippi jippi deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
25 Controller/Component/CrudComponent.php
@@ -326,8 +326,15 @@ public function getListener($name, $create = true) {
* @return void
*/
protected function _setModelProperties() {
- $this->_modelName = $this->_controller->modelClass;
+ $configKey = 'modelMap.' . $this->_action;
+ if (!$this->_modelName = $this->config($configKey)) {
+ $this->_modelName = $this->_controller->modelClass;
+ }
+
$this->_model = $this->_controller->{$this->_modelName};
+ if (empty($this->_model)) {
+ throw new RuntimeException('No model loaded in the Controller by the name "' . $this->_modelName . '". Please add it to $uses.');
+ }
}
/**
@@ -532,14 +539,14 @@ protected function _getSubject($additional = array()) {
$this->_setModelProperties();
}
- $subject = new CrudSubject();
- $subject->crud = $this;
- $subject->controller = $this->_controller;
- $subject->model = $this->_model;
- $subject->modelClass = $this->_modelName;
- $subject->action = $this->_action;
- $subject->request = $this->_request;
- $subject->response = $this->_controller->response;
+ $subject = new CrudSubject();
+ $subject->crud = $this;
+ $subject->controller = $this->_controller;
+ $subject->model = $this->_model;
+ $subject->modelClass = $this->_modelName;
+ $subject->action = $this->_action;
+ $subject->request = $this->_request;
+ $subject->response = $this->_controller->response;
$subject->set($additional);
return $subject;
View
75 Test/Case/Controller/Component/CrudComponentTest.php
@@ -97,7 +97,7 @@ public function add() {
/**
* TestCrudComponent
*
- * Expost protected methods so we can test them in issolation
+ * Expose protected methods so we can test them in isolation
*/
class TestCrudComponent extends CrudComponent {
@@ -142,6 +142,28 @@ public function detectPrimaryKeyFieldType() {
public function getSaveAllOptions($action = null) {
return parent::_getSaveAllOptions($action);
}
+
+/**
+ * test visibility wrapper - access protected _modelName property
+ */
+ public function getModelName() {
+ return $this->_modelName;
+ }
+
+/**
+ * test visibility wrapper - call protected method _setModelProperties
+ */
+ public function setModelProperties() {
+ return parent::_setModelProperties();
+ }
+
+/**
+ * test visibility wrapper - allow on the fly change of action name
+ */
+ public function setAction($name) {
+ $this->_action = $name;
+ }
+
}
class CrudController extends Controller {
@@ -221,7 +243,13 @@ public function setUp() {
* tearDown method
*/
public function tearDown() {
- unset($this->Crud);
+ unset(
+ $this->model,
+ $this->request,
+ $this->controller,
+ $this->Crud
+ );
+
parent::tearDown();
}
@@ -1722,4 +1750,47 @@ public function testGetSaveAllOptionsCustomAction() {
$this->assertEqual($value, $expected);
}
+/**
+ * Test that having no mapped model for an action,
+ * just use the modelClass from the controller
+ *
+ * @return void
+ */
+ public function testSetModelPropertiesDefault() {
+ $this->Crud->setAction('index');
+ $this->Crud->setModelProperties();
+ $this->assertSame('CrudExample', $this->Crud->getModelName());
+ }
+
+/**
+ * Test that having mapped a custom model for an action,
+ * the modelName will be as configured
+ *
+ * @return void
+ */
+ public function testSetModelPropertiesChangeModelForAction() {
+ $this->controller->Donkey = new StdClass;
+
+ $this->Crud->setAction('index');
+ $this->Crud->config('modelMap.index', 'Donkey');
+ $this->Crud->setModelProperties();
+
+ $this->assertSame('Donkey', $this->Crud->getModelName());
+ }
+
+/**
+ * Test that having mapped a custom model for an action,
+ * but the custom model isn't loaded, will throw an exception
+ *
+ * @expectedException RuntimeException
+ * @expectedExceptionMessage No model loaded in the Controller by the name "Donkey". Please add it to $uses.
+ */
+ public function testSetModelPropertiesChangeModelForActionNotLoadedModel() {
+ $this->Crud->setAction('index');
+ $this->Crud->config('modelMap.index', 'Donkey');
+ $this->Crud->setModelProperties();
+
+ $this->assertSame('Donkey', $this->Crud->getModelName());
+ }
+
}
Something went wrong with that request. Please try again.