From a0477b03e398efbc83ae707ce118a6a09533123f Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 10 Mar 2016 14:59:13 +0000 Subject: [PATCH] Option to auto-expand groups when components are down. Closes #1602 --- .../AddComponentGroupCommand.php | 11 +++-- .../UpdateComponentGroupCommand.php | 11 +++-- app/Console/Commands/DemoSeederCommand.php | 4 +- app/Models/ComponentGroup.php | 14 ++++++- app/Presenters/ComponentGroupPresenter.php | 20 ++++++++- database/factories/ModelFactory.php | 2 +- ...rTableComponentGroupsMakeColumnInteger.php | 41 +++++++++++++++++++ resources/lang/en/forms.php | 7 +++- .../dashboard/components/groups/add.blade.php | 11 ++--- .../components/groups/edit.blade.php | 11 ++--- resources/views/partials/components.blade.php | 8 ++-- tests/Api/ComponentGroupTest.php | 4 +- .../AddComponentGroupCommandTest.php | 2 +- .../UpdateComponentGroupCommandTest.php | 2 +- 14 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 database/migrations/2016_03_10_144613_AlterTableComponentGroupsMakeColumnInteger.php diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index d311217e..e68f24fd 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -11,6 +11,11 @@ namespace CachetHQ\Cachet\Bus\Commands\ComponentGroup; +/** + * This is the add component group command. + * + * @author James Brooks + */ final class AddComponentGroupCommand { /** @@ -30,7 +35,7 @@ final class AddComponentGroupCommand /** * Is the component group collapsed? * - * @var bool + * @var int */ public $collapsed; @@ -42,7 +47,7 @@ final class AddComponentGroupCommand public $rules = [ 'name' => 'required|string', 'order' => 'int', - 'collapsed' => 'bool', + 'collapsed' => 'int|between:0,3', ]; /** @@ -50,7 +55,7 @@ final class AddComponentGroupCommand * * @param string $name * @param int $order - * @param bool $collapsed + * @param int $collapsed * * @return void */ diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index dea3573e..a20c4944 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -13,6 +13,11 @@ namespace CachetHQ\Cachet\Bus\Commands\ComponentGroup; use CachetHQ\Cachet\Models\ComponentGroup; +/** + * This is the update component group command. + * + * @author James Brooks + */ final class UpdateComponentGroupCommand { /** @@ -39,7 +44,7 @@ final class UpdateComponentGroupCommand /** * Is the component group collapsed? * - * @var bool + * @var int */ public $collapsed; @@ -51,7 +56,7 @@ final class UpdateComponentGroupCommand public $rules = [ 'name' => 'string', 'order' => 'int', - 'collapsed' => 'bool', + 'collapsed' => 'int|between:0,3', ]; /** @@ -60,7 +65,7 @@ final class UpdateComponentGroupCommand * @param \CachetHQ\Cachet\Models\ComponentGroup $group * @param string $name * @param int $order - * @param bool $collapsed + * @param int $collapsed * * @return void */ diff --git a/app/Console/Commands/DemoSeederCommand.php b/app/Console/Commands/DemoSeederCommand.php index 07edad2f..ce3d66ed 100644 --- a/app/Console/Commands/DemoSeederCommand.php +++ b/app/Console/Commands/DemoSeederCommand.php @@ -82,11 +82,11 @@ class DemoSeederCommand extends Command [ 'name' => 'Websites', 'order' => 1, - 'collapsed' => false, + 'collapsed' => 0, ], [ 'name' => 'Alt Three', 'order' => 2, - 'collapsed' => true, + 'collapsed' => 1, ], ]; diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index d68563e0..07c6d617 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -30,7 +30,7 @@ class ComponentGroup extends Model implements HasPresenter protected $casts = [ 'name' => 'string', 'order' => 'int', - 'collapsed' => 'bool', + 'collapsed' => 'int', ]; /** @@ -48,7 +48,7 @@ class ComponentGroup extends Model implements HasPresenter public $rules = [ 'name' => 'required|string', 'order' => 'int', - 'collapsed' => 'bool', + 'collapsed' => 'int', ]; /** @@ -92,6 +92,16 @@ class ComponentGroup extends Model implements HasPresenter return $this->hasMany(Component::class, 'group_id', 'id'); } + /** + * Get the incidents relation. + * + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + */ + public function incidents() + { + return $this->hasManyThrough(Incident::class, Component::class, 'id', 'component_id'); + } + /** * Return all of the enabled components. * diff --git a/app/Presenters/ComponentGroupPresenter.php b/app/Presenters/ComponentGroupPresenter.php index 2b413905..9aebc483 100644 --- a/app/Presenters/ComponentGroupPresenter.php +++ b/app/Presenters/ComponentGroupPresenter.php @@ -63,7 +63,25 @@ class ComponentGroupPresenter extends BasePresenter implements Arrayable */ public function collapse_class() { - return $this->wrappedObject->collapsed ? 'ion-ios-plus-outline' : 'ion-ios-minus-outline'; + return $this->is_collapsed() ? 'ion-ios-plus-outline' : 'ion-ios-minus-outline'; + } + + /** + * Determine if the group should be collapsed. + * + * @return bool + */ + public function is_collapsed() + { + if ($this->wrappedObject->collapsed === 0) { + return false; + } elseif ($this->wrappedObject->collapsed === 1) { + return true; + } + + return $this->wrappedObject->components->filter(function ($component) { + return $component->status > 1; + })->count() === 0; } /** diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 157aa925..b6e2acdd 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -34,7 +34,7 @@ $factory->define(ComponentGroup::class, function ($faker) { return [ 'name' => $faker->words(2, true), 'order' => 0, - 'collapsed' => $faker->boolean(), + 'collapsed' => random_int(0, 3), ]; }); diff --git a/database/migrations/2016_03_10_144613_AlterTableComponentGroupsMakeColumnInteger.php b/database/migrations/2016_03_10_144613_AlterTableComponentGroupsMakeColumnInteger.php new file mode 100644 index 00000000..e5688121 --- /dev/null +++ b/database/migrations/2016_03_10_144613_AlterTableComponentGroupsMakeColumnInteger.php @@ -0,0 +1,41 @@ +integer('collapsed')->unsigned()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('component_groups', function (Blueprint $table) { + $table->boolean('collapsed')->change(); + }); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index 4ace1a9b..10dc1d7a 100755 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -68,8 +68,11 @@ return [ 'enabled' => 'Component enabled?', 'groups' => [ - 'name' => 'Name', - 'collapsed' => 'Collapse the group by default?', + 'name' => 'Name', + 'collapsing' => 'Choose visibility of the group', + 'visible' => 'Always expanded', + 'collapsed' => 'Collapse the group by default', + 'collapsed_incident' => 'Collapse the group, but expand if there are issues', ], ], diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index e6079c42..bf678184 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -22,11 +22,12 @@
- + +
diff --git a/resources/views/dashboard/components/groups/edit.blade.php b/resources/views/dashboard/components/groups/edit.blade.php index 2db43ca1..53400f4b 100644 --- a/resources/views/dashboard/components/groups/edit.blade.php +++ b/resources/views/dashboard/components/groups/edit.blade.php @@ -22,11 +22,12 @@
- + +
diff --git a/resources/views/partials/components.blade.php b/resources/views/partials/components.blade.php index e4e95170..106ed8c3 100644 --- a/resources/views/partials/components.blade.php +++ b/resources/views/partials/components.blade.php @@ -11,10 +11,10 @@ -
- @foreach($componentGroup->enabled_components()->orderBy('order')->get() as $component) - @include('partials.component', compact($component)) - @endforeach +
+ @foreach($componentGroup->enabled_components()->orderBy('order')->get() as $component) + @include('partials.component', compact($component)) + @endforeach
@endif diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index af69a65e..1da912a9 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -58,9 +58,9 @@ class ComponentGroupTest extends AbstractApiTestCase $this->post('/api/v1/components/groups', [ 'name' => 'Foo', 'order' => 1, - 'collapsed' => true, + 'collapsed' => 1, ]); - $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => true]); + $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1]); $this->assertResponseOk(); } diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index d2beed1b..8f4253fe 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -28,7 +28,7 @@ class AddComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['name' => 'Test', 'order' => 0, 'collapsed' => true]; + $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1]; $object = new AddComponentGroupCommand($params['name'], $params['order'], $params['collapsed']); diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index e0f2c88b..6db036bd 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -29,7 +29,7 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['group' => new ComponentGroup(), 'name' => 'Foo', 'order' => 1, 'collapsed' => true]; + $params = ['group' => new ComponentGroup(), 'name' => 'Foo', 'order' => 1, 'collapsed' => 2]; $object = new UpdateComponentGroupCommand( $params['group'], $params['name'],