Settings router.

Here's a simple settings router. the createOrUpdate method lives
in the model, and by default all models will return true on save
or false if save went wrong.
This commit is contained in:
marco
2015-08-19 23:55:30 +02:00
parent ada1bfdcd9
commit ce730d7c3b
8 changed files with 76 additions and 45 deletions

View File

@ -73,7 +73,6 @@ class RoboFile extends \Robo\Tasks {
function testUnit($singleUnit = null) { function testUnit($singleUnit = null) {
$this->loadEnv(); $this->loadEnv();
$this->compileAll();
$this->_exec('vendor/bin/codecept build'); $this->_exec('vendor/bin/codecept build');
$this->_exec('vendor/bin/codecept run unit ' . (($singleUnit) ? $singleUnit : '')); $this->_exec('vendor/bin/codecept run unit ' . (($singleUnit) ? $singleUnit : ''));
} }

View File

@ -10,9 +10,21 @@ class Model extends \Sudzy\ValidModel {
} }
function save() { function save() {
$this->setTimestamp();
try {
parent::save();
return true;
} catch (\Sudzy\ValidationException $e) {
return false;
} catch (Exception $e) {
return false;
}
}
private function setTimestamp() {
if ($this->created_at === null) { if ($this->created_at === null) {
$this->created_at = date("Y-m-d H:i:s"); $this->created_at = date("Y-m-d H:i:s");
} }
parent::save();
} }
} }

View File

@ -11,13 +11,25 @@ class Setting extends Model {
$this->addValidations("name", array( $this->addValidations("name", array(
"required" => "name_is_blank", "required" => "name_is_blank",
"isString" => "name_is_not_string", "isString" => "name_is_not_string"
"minLength|2" => "name_is_short"
)); ));
$this->addValidations("value", array( $this->addValidations("value", array(
"required" => "value_is_blank", "required" => "value_is_blank",
"isString" => "value_is_not_string", "isString" => "value_is_not_string"
"minLength|2" => "value_is_short"
)); ));
} }
public static function createOrUpdate($model) {
$exists = Setting::where('name', $model['name'])
->find_one();
if($exists === false) {
$new_model = Setting::create();
$new_model->hydrate($model);
return $new_model->save();
}
$exists->value = $model['value'];
return $exists->save();
}
} }

View File

@ -1,5 +1,6 @@
<?php <?php
namespace MailPoet\Router; namespace MailPoet\Router;
use \MailPoet\Models\Setting;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@ -7,12 +8,17 @@ class Settings {
function __construct() { function __construct() {
} }
function get($params) { function get() {
$data = array( $settings = Setting::find_array();
'first_name' => 'John', wp_send_json($settings);
'last_name' => 'Mailer', }
'email' => 'john@mailpoet.com'
); function set($args) {
wp_send_json($params); $save = function($setting) {
Setting::createOrUpdate($setting);
};
$results = array_map($save, $args);
wp_send_json(in_array(false, $results));
} }
} }

View File

@ -143,7 +143,7 @@ abstract class ValidModel extends \Model
protected function doValidationError($context) protected function doValidationError($context)
{ {
if ($context == $this->_validationOptions['throw']) if ($context == $this->_validationOptions['throw'])
throw new \ValidationException($this->_validationErrors); throw new \Sudzy\ValidationException($this->_validationErrors);
} }
protected function addValidationError($msg, $field = null) protected function addValidationError($msg, $field = null)

View File

@ -1,5 +1,7 @@
<?php <?php
namespace Sudzy;
class ValidationException extends \Exception class ValidationException extends \Exception
{ {
protected $_validationErrors; protected $_validationErrors;

View File

@ -12,7 +12,7 @@ class SettingCest {
$setting = Setting::create(); $setting = Setting::create();
$setting->hydrate($this->data); $setting->hydrate($this->data);
$setting->save(); $this->result = $setting->save();
} }
function itCanBeCreated() { function itCanBeCreated() {
@ -21,32 +21,12 @@ class SettingCest {
expect($setting->id)->notNull(); expect($setting->id)->notNull();
} }
function nameShouldValidate() { function itHasToBeValid() {
$conflict_setting = Setting::create(); expect($this->result)->equals(true);
$conflict_setting->validateField('name', ''); $empty_model = Setting::create();
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_blank'); expect($empty_model->save())->equals(false);
$validations = $empty_model->getValidationErrors();
$conflict_setting = Setting::create(); expect(count($validations))->equals(4);
$conflict_setting->validateField('name', 31337);
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_not_string');
$conflict_setting = Setting::create();
$conflict_setting->validateField('name', 'a');
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_short');
}
function valueShouldValidate() {
$conflict_setting = Setting::create();
$conflict_setting->validateField('value', '');
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_blank');
$conflict_setting = Setting::create();
$conflict_setting->validateField('value', 31337);
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_not_string');
$conflict_setting = Setting::create();
$conflict_setting->validateField('value', 'a');
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_short');
} }
function itHasACreatedAtOnCreation() { function itHasACreatedAtOnCreation() {
@ -82,6 +62,26 @@ class SettingCest {
expect($time_difference)->equals(true); expect($time_difference)->equals(true);
} }
function itCanCreateOrUpdate() {
$data = array(
'name' => 'new',
'value' => 'data'
);
$result = Setting::createOrUpdate($data);
expect($result)->equals(true);
$record = Setting::where('name', $data['name'])
->find_one();
expect($record->value)->equals($data['value']);
$data['value'] = 'new data';
$result = Setting::createOrUpdate($data);
expect($result)->equals(true);
$record = Setting::where('name', $data['name'])
->find_one();
expect($record->value)->equals('new data');
}
function _after() { function _after() {
$setting = Setting::where('name', $this->data['name']) $setting = Setting::where('name', $this->data['name'])
->findOne() ->findOne()

View File

@ -45,7 +45,7 @@ jQuery(function($) {
first_name: 'John' first_name: 'John'
}, },
onSuccess: function(response) { onSuccess: function(response) {
// console.log(response); console.log(response['0'].value);
} }
}); });