diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 4521b139..6122bc45 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -12,9 +12,11 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; +use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; use Illuminate\Database\Eloquent\Model; +use McCool\LaravelAutoPresenter\HasPresenter; -class ComponentGroup extends Model +class ComponentGroup extends Model implements HasPresenter { use ValidatingTrait; @@ -46,6 +48,13 @@ class ComponentGroup extends Model 'order' => 'int', ]; + /** + * The relations to eager load on every query. + * + * @var string[] + */ + protected $with = ['enabled_components']; + /** * A group can have many components. * @@ -55,4 +64,24 @@ class ComponentGroup extends Model { return $this->hasMany(Component::class, 'group_id', 'id'); } + + /** + * Return all of the enabled components. + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function enabled_components() + { + return $this->components()->enabled(); + } + + /** + * Get the presenter class. + * + * @return string + */ + public function getPresenterClass() + { + return ComponentGroupPresenter::class; + } } diff --git a/app/Presenters/ComponentGroupPresenter.php b/app/Presenters/ComponentGroupPresenter.php new file mode 100644 index 00000000..e31a45f4 --- /dev/null +++ b/app/Presenters/ComponentGroupPresenter.php @@ -0,0 +1,69 @@ +wrappedObject->enabled_components()->orderBy('status', 'desc')->first()) { + return $enabledComponents->status; + } + } + + /** + * Returns the lowest component status, readable by humans. + * + * @return string|null + */ + public function lowest_human_status() + { + if ($enabledComponents = $this->wrappedObject->enabled_components()->orderBy('status', 'desc')->first()) { + return $enabledComponents->human_status; + } + } + + /** + * Returns the lowest component status color. + * + * @return string|null + */ + public function lowest_status_color() + { + if ($enabledComponents = $this->wrappedObject->enabled_components()->orderBy('status', 'desc')->first()) { + return $enabledComponents->status_color; + } + } + + /** + * Convert the presenter instance to an array. + * + * @return string[] + */ + public function toArray() + { + return array_merge($this->wrappedObject->toArray(), [ + 'created_at' => $this->created_at(), + 'updated_at' => $this->updated_at(), + 'lowest_human_status' => $this->lowest_human_status(), + ]); + } +} diff --git a/resources/views/partials/components.blade.php b/resources/views/partials/components.blade.php index a590e594..2a493307 100644 --- a/resources/views/partials/components.blade.php +++ b/resources/views/partials/components.blade.php @@ -1,14 +1,18 @@