1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Simplified default folder concept

This commit is contained in:
Laurent Cozic 2017-06-04 13:53:38 +01:00
parent fdab917043
commit 8d51fb779d
2 changed files with 3 additions and 28 deletions

View File

@ -51,22 +51,10 @@ class Folder extends BaseItem {
public function save(Array $options = array()) {
$dirty = $this->getDirty();
// Handling of default folder is done in several steps:
// - If changing is_default to false and this is the only default folder - throw an exception.
// - Then save the folder
// - Then, if the folder was set to be the new default, set all the other folders to non-default.
if (isset($dirty['is_default'])) {
$defaultFolders = self::where('owner_id', '=', $this->owner_id)->where('is_default', '=', 1);
if (!$dirty['is_default'] && $defaultFolders->count() == 1 && self::defaultFolder($this->owner_id)->id == $this->id) {
throw new \Exception(sprintf('Cannot make folder %s non-default - there should be at least one default folder', BaseModel::hex($this->id)));
}
}
$output = parent::save($options);
// If the folder was set to be the new default, set all the other folders to non-default.
if ($output && isset($dirty['is_default'])) {
if (!!$dirty['is_default']) {
self::where('owner_id', '=', $this->owner_id)->where('id', '!=', $this->id)->update(array('is_default' => 0));

View File

@ -12,7 +12,7 @@ class FolderTest extends BaseTestCase {
Folder::truncate();
}
public function testDefault() {
public function testNewFolderBecomesDefault() {
$f1 = new Folder();
$f1->is_default = true;
$f1->owner_id = TestUtils::userId();
@ -29,18 +29,5 @@ class FolderTest extends BaseTestCase {
$this->assertTrue(!$f1->is_default);
$this->assertTrue(!!$f2->is_default);
}
public function testDefaultException() {
$this->expectException(Exception::class);
$f1 = new Folder();
$f1->is_default = true;
$f1->owner_id = TestUtils::userId();
$f1->save();
$f1 = Folder::find($f1->id);
$f1->is_default = false;
$f1->save();
}
}