Started work on enabling/disabling components

This commit is contained in:
James Brooks
2015-11-03 21:32:45 +00:00
parent e8a3e18d55
commit edd111451e
10 changed files with 122 additions and 14 deletions
+11 -1
View File
@@ -55,6 +55,13 @@ final class AddComponentCommand
*/ */
public $group_id; public $group_id;
/**
* Is the component enabled?
*
* @var bool
*/
public $enabled;
/** /**
* The validation rules. * The validation rules.
* *
@@ -67,6 +74,7 @@ final class AddComponentCommand
'link' => 'url', 'link' => 'url',
'order' => 'int', 'order' => 'int',
'group_id' => 'int', 'group_id' => 'int',
'enabled' => 'bool',
]; ];
/** /**
@@ -78,10 +86,11 @@ final class AddComponentCommand
* @param string $link * @param string $link
* @param int $order * @param int $order
* @param int $group_id * @param int $group_id
* @param bool $enabled
* *
* @return void * @return void
*/ */
public function __construct($name, $description, $status, $link, $order, $group_id) public function __construct($name, $description, $status, $link, $order, $group_id, $enabled)
{ {
$this->name = $name; $this->name = $name;
$this->description = $description; $this->description = $description;
@@ -89,5 +98,6 @@ final class AddComponentCommand
$this->link = $link; $this->link = $link;
$this->order = $order; $this->order = $order;
$this->group_id = $group_id; $this->group_id = $group_id;
$this->enabled = $enabled;
} }
} }
@@ -64,6 +64,13 @@ final class UpdateComponentCommand
*/ */
public $group_id; public $group_id;
/**
* Is the component enabled?
*
* @var bool
*/
public $enabled;
/** /**
* The validation rules. * The validation rules.
* *
@@ -76,6 +83,7 @@ final class UpdateComponentCommand
'link' => 'url', 'link' => 'url',
'order' => 'int', 'order' => 'int',
'group_id' => 'int', 'group_id' => 'int',
'enabled' => 'bool',
]; ];
/** /**
@@ -88,10 +96,11 @@ final class UpdateComponentCommand
* @param string $link * @param string $link
* @param int $order * @param int $order
* @param int $group_id * @param int $group_id
* @param bool $enabled
* *
* @return void * @return void
*/ */
public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id) public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled)
{ {
$this->component = $component; $this->component = $component;
$this->name = $name; $this->name = $name;
@@ -100,5 +109,6 @@ final class UpdateComponentCommand
$this->link = $link; $this->link = $link;
$this->order = $order; $this->order = $order;
$this->group_id = $group_id; $this->group_id = $group_id;
$this->enabled = $enabled;
} }
} }
+4 -4
View File
@@ -35,7 +35,7 @@ class StatusPageComposer
'favicon' => 'favicon-high-alert', 'favicon' => 'favicon-high-alert',
]; ];
if (Component::notStatus(1)->count() === 0) { if (Component::where('enabled', true)->notStatus(1)->count() === 0) {
// If all our components are ok, do we have any non-fixed incidents? // If all our components are ok, do we have any non-fixed incidents?
$incidents = Incident::notScheduled()->orderBy('created_at', 'desc')->get(); $incidents = Incident::notScheduled()->orderBy('created_at', 'desc')->get();
$incidentCount = $incidents->count(); $incidentCount = $incidents->count();
@@ -48,7 +48,7 @@ class StatusPageComposer
]; ];
} }
} else { } else {
if (Component::whereIn('status', [2, 3])->count() > 0) { if (Component::where('enabled', true)->whereIn('status', [2, 3])->count() > 0) {
$withData['favicon'] = 'favicon-medium-alert'; $withData['favicon'] = 'favicon-medium-alert';
} }
} }
@@ -57,9 +57,9 @@ class StatusPageComposer
$scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get(); $scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();
// Component & Component Group lists. // Component & Component Group lists.
$usedComponentGroups = Component::where('group_id', '>', 0)->groupBy('group_id')->lists('group_id'); $usedComponentGroups = Component::where('enabled', true)->where('group_id', '>', 0)->groupBy('group_id')->lists('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); $ungroupedComponents = Component::where('enabled', true)->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
$view->with($withData) $view->with($withData)
->withComponentGroups($componentGroups) ->withComponentGroups($componentGroups)
@@ -26,14 +26,18 @@ class AddComponentCommandHandler
*/ */
public function handle(AddComponentCommand $command) public function handle(AddComponentCommand $command)
{ {
$component = Component::create(array_filter([ $componentData = array_filter([
'name' => $command->name, 'name' => $command->name,
'description' => $command->description, 'description' => $command->description,
'link' => $command->link, 'link' => $command->link,
'status' => $command->status, 'status' => $command->status,
'order' => $command->order, 'order' => $command->order,
'group_id' => $command->group_id, 'group_id' => $command->group_id,
])); ]);
$componentData['enabled'] = $command->enabled;
$component = Component::create($componentData);
event(new ComponentWasAddedEvent($component)); event(new ComponentWasAddedEvent($component));
@@ -27,14 +27,18 @@ class UpdateComponentCommandHandler
public function handle(UpdateComponentCommand $command) public function handle(UpdateComponentCommand $command)
{ {
$component = $command->component; $component = $command->component;
$component->update(array_filter([ $componentData = array_filter([
'name' => $command->name, 'name' => $command->name,
'description' => $command->description, 'description' => $command->description,
'link' => $command->link, 'link' => $command->link,
'status' => $command->status, 'status' => $command->status,
'order' => $command->order, 'order' => $command->order,
'group_id' => $command->group_id, 'group_id' => $command->group_id,
])); ]);
$componentData['enabled'] = $command->enabled;
$component->update($componentData);
event(new ComponentWasUpdatedEvent($component)); event(new ComponentWasUpdatedEvent($component));
@@ -69,7 +69,8 @@ class ComponentController extends AbstractApiController
Binput::get('status'), Binput::get('status'),
Binput::get('link'), Binput::get('link'),
Binput::get('order'), Binput::get('order'),
Binput::get('group_id') Binput::get('group_id'),
(bool) Binput::get('enabled')
)); ));
} catch (Exception $e) { } catch (Exception $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
@@ -109,7 +110,8 @@ class ComponentController extends AbstractApiController
Binput::get('status'), Binput::get('status'),
Binput::get('link'), Binput::get('link'),
Binput::get('order'), Binput::get('order'),
Binput::get('group_id') Binput::get('group_id'),
(bool) Binput::get('enabled')
)); ));
} catch (Exception $e) { } catch (Exception $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
+3
View File
@@ -32,6 +32,7 @@ class Component extends Model implements HasPresenter
'group_id' => 0, 'group_id' => 0,
'description' => '', 'description' => '',
'link' => '', 'link' => '',
'enabled' => true,
]; ];
/** /**
@@ -46,6 +47,7 @@ class Component extends Model implements HasPresenter
'description' => 'string', 'description' => 'string',
'link' => 'string', 'link' => 'string',
'deleted_at' => 'date', 'deleted_at' => 'date',
'enabled' => 'bool',
]; ];
/** /**
@@ -61,6 +63,7 @@ class Component extends Model implements HasPresenter
'link', 'link',
'order', 'order',
'group_id', 'group_id',
'enabled',
]; ];
/** /**
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterTableComponentsAddEnabledColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('components', function (Blueprint $table) {
$table->boolean('enabled')->after('group_id')->default(true);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('components', function (Blueprint $table) {
$table->dropColumn('enabled');
});
}
}
@@ -1,14 +1,14 @@
<ul class="list-group components"> <ul class="list-group components">
@if($component_groups->count() > 0) @if($component_groups->count() > 0)
@foreach($component_groups as $componentGroup) @foreach($component_groups as $componentGroup)
@if($componentGroup->components->count() > 0) @if($componentGroup->components->where('enabled', true)->count() > 0)
<li class="list-group-item group-name"> <li class="list-group-item group-name">
<i class="ion-ios-minus-outline group-toggle"></i> <i class="ion-ios-minus-outline group-toggle"></i>
<strong>{{ $componentGroup->name }}</strong> <strong>{{ $componentGroup->name }}</strong>
</li> </li>
<div class="group-items"> <div class="group-items">
@foreach($componentGroup->components->sortBy('order') as $component) @foreach($componentGroup->components->where('enabled', true)->sortBy('order') as $component)
@include('partials.component', compact($component)) @include('partials.component', compact($component))
@endforeach @endforeach
</div> </div>
+34
View File
@@ -61,11 +61,45 @@ class ComponentTest extends AbstractTestCase
'link' => 'http://example.com', 'link' => 'http://example.com',
'order' => 1, 'order' => 1,
'group_id' => 1, 'group_id' => 1,
'enabled' => true,
]); ]);
$this->seeJson(['name' => 'Foo']); $this->seeJson(['name' => 'Foo']);
$this->assertResponseOk(); $this->assertResponseOk();
} }
public function testPostComponentWithoutEnabledField()
{
$this->beUser();
$this->post('/api/v1/components', [
'name' => 'Foo',
'description' => 'Bar',
'status' => 1,
'link' => 'http://example.com',
'order' => 1,
'group_id' => 1,
]);
$this->seeJson(['name' => 'Foo', 'enabled' => true]);
$this->assertResponseOk();
}
public function testPostDisabledComponent()
{
$this->beUser();
$this->post('/api/v1/components', [
'name' => 'Foo',
'description' => 'Bar',
'status' => 1,
'link' => 'http://example.com',
'order' => 1,
'group_id' => 1,
'enabled' => 0,
]);
$this->seeJson(['name' => 'Foo', 'enabled' => false]);
$this->assertResponseOk();
}
public function testGetNewComponent() public function testGetNewComponent()
{ {
$component = factory('CachetHQ\Cachet\Models\Component')->create(); $component = factory('CachetHQ\Cachet\Models\Component')->create();