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) {
|
||||
$this->loadEnv();
|
||||
$this->compileAll();
|
||||
$this->_exec('vendor/bin/codecept build');
|
||||
$this->_exec('vendor/bin/codecept run unit ' . (($singleUnit) ? $singleUnit : ''));
|
||||
}
|
||||
|
@ -10,9 +10,21 @@ class Model extends \Sudzy\ValidModel {
|
||||
}
|
||||
|
||||
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) {
|
||||
$this->created_at = date("Y-m-d H:i:s");
|
||||
}
|
||||
parent::save();
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,25 @@ class Setting extends Model {
|
||||
|
||||
$this->addValidations("name", array(
|
||||
"required" => "name_is_blank",
|
||||
"isString" => "name_is_not_string",
|
||||
"minLength|2" => "name_is_short"
|
||||
"isString" => "name_is_not_string"
|
||||
));
|
||||
$this->addValidations("value", array(
|
||||
"required" => "value_is_blank",
|
||||
"isString" => "value_is_not_string",
|
||||
"minLength|2" => "value_is_short"
|
||||
"isString" => "value_is_not_string"
|
||||
));
|
||||
}
|
||||
|
||||
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
|
||||
namespace MailPoet\Router;
|
||||
use \MailPoet\Models\Setting;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -7,12 +8,17 @@ class Settings {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get($params) {
|
||||
$data = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
);
|
||||
wp_send_json($params);
|
||||
function get() {
|
||||
$settings = Setting::find_array();
|
||||
wp_send_json($settings);
|
||||
}
|
||||
|
||||
function set($args) {
|
||||
$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)
|
||||
{
|
||||
if ($context == $this->_validationOptions['throw'])
|
||||
throw new \ValidationException($this->_validationErrors);
|
||||
throw new \Sudzy\ValidationException($this->_validationErrors);
|
||||
}
|
||||
|
||||
protected function addValidationError($msg, $field = null)
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Sudzy;
|
||||
|
||||
class ValidationException extends \Exception
|
||||
{
|
||||
protected $_validationErrors;
|
||||
@ -14,4 +16,4 @@ class ValidationException extends \Exception
|
||||
{
|
||||
return $this->_validationErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
class SettingCest {
|
||||
|
||||
|
||||
function _before() {
|
||||
$this->before_time = time();
|
||||
$this->data = array(
|
||||
@ -12,7 +12,7 @@ class SettingCest {
|
||||
|
||||
$setting = Setting::create();
|
||||
$setting->hydrate($this->data);
|
||||
$setting->save();
|
||||
$this->result = $setting->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
@ -21,32 +21,12 @@ class SettingCest {
|
||||
expect($setting->id)->notNull();
|
||||
}
|
||||
|
||||
function nameShouldValidate() {
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', '');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_blank');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$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 itHasToBeValid() {
|
||||
expect($this->result)->equals(true);
|
||||
$empty_model = Setting::create();
|
||||
expect($empty_model->save())->equals(false);
|
||||
$validations = $empty_model->getValidationErrors();
|
||||
expect(count($validations))->equals(4);
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
@ -81,7 +61,27 @@ class SettingCest {
|
||||
$time_difference = strtotime($setting->updated_at) >= $update_time;
|
||||
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() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne()
|
||||
|
@ -45,7 +45,7 @@ jQuery(function($) {
|
||||
first_name: 'John'
|
||||
},
|
||||
onSuccess: function(response) {
|
||||
// console.log(response);
|
||||
console.log(response['0'].value);
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user