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:
@ -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 : ''));
|
||||||
}
|
}
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace Sudzy;
|
||||||
|
|
||||||
class ValidationException extends \Exception
|
class ValidationException extends \Exception
|
||||||
{
|
{
|
||||||
protected $_validationErrors;
|
protected $_validationErrors;
|
||||||
@ -14,4 +16,4 @@ class ValidationException extends \Exception
|
|||||||
{
|
{
|
||||||
return $this->_validationErrors;
|
return $this->_validationErrors;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
|
|
||||||
class SettingCest {
|
class SettingCest {
|
||||||
|
|
||||||
function _before() {
|
function _before() {
|
||||||
$this->before_time = time();
|
$this->before_time = time();
|
||||||
$this->data = array(
|
$this->data = array(
|
||||||
@ -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() {
|
||||||
@ -81,7 +61,27 @@ class SettingCest {
|
|||||||
$time_difference = strtotime($setting->updated_at) >= $update_time;
|
$time_difference = strtotime($setting->updated_at) >= $update_time;
|
||||||
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()
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user