diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-10-30 23:33:43 +1300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-11-01 19:37:32 +1300 |
commit | 1163b235391668d53ae0cea80bc22d40b365e0a7 (patch) | |
tree | ce2cf692f41fba52eb42e767611e130399c85499 /app/presenters | |
parent | 88800abcd8741b07114c2850e00b74fbecfbf90e (diff) | |
download | gitlab-ce-1163b235391668d53ae0cea80bc22d40b365e0a7.tar.gz |
Move view and path concerns to presenters
- Move show path for cluster to ClusterPresenter
- Create ClusterablePresenter to encapsulate logic. Consolidates
scattered methods from BaseController and ClustersHelper into an object.
Diffstat (limited to 'app/presenters')
-rw-r--r-- | app/presenters/clusterable_presenter.rb | 30 | ||||
-rw-r--r-- | app/presenters/clusters/cluster_presenter.rb | 8 | ||||
-rw-r--r-- | app/presenters/project_clusterable_presenter.rb | 15 |
3 files changed, 53 insertions, 0 deletions
diff --git a/app/presenters/clusterable_presenter.rb b/app/presenters/clusterable_presenter.rb new file mode 100644 index 00000000000..c857d57b003 --- /dev/null +++ b/app/presenters/clusterable_presenter.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class ClusterablePresenter < Gitlab::View::Presenter::Delegated + presents :clusterable + + def self.fabricate(clusterable, **attributes) + presenter_class = "#{clusterable.class.name}ClusterablePresenter".constantize + attributes_with_presenter_class = attributes.merge(presenter_class: presenter_class) + + Gitlab::View::Presenter::Factory + .new(clusterable, attributes_with_presenter_class) + .fabricate! + end + + def can_create_cluster? + can?(current_user, :create_cluster, clusterable) + end + + def index_path + raise NotImplementedError + end + + def new_path + raise NotImplementedError + end + + def clusterable_params + raise NotImplementedError + end +end diff --git a/app/presenters/clusters/cluster_presenter.rb b/app/presenters/clusters/cluster_presenter.rb index dfdd8e82f97..78d632eb77c 100644 --- a/app/presenters/clusters/cluster_presenter.rb +++ b/app/presenters/clusters/cluster_presenter.rb @@ -11,5 +11,13 @@ module Clusters def can_toggle_cluster? can?(current_user, :update_cluster, cluster) && created? end + + def show_path + if cluster.project_type? + project_cluster_path(project, cluster) + else + raise NotImplementedError + end + end end end diff --git a/app/presenters/project_clusterable_presenter.rb b/app/presenters/project_clusterable_presenter.rb new file mode 100644 index 00000000000..f986b5584a3 --- /dev/null +++ b/app/presenters/project_clusterable_presenter.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class ProjectClusterablePresenter < ClusterablePresenter + def index_path + project_clusters_path(clusterable) + end + + def new_path + new_project_cluster_path(clusterable) + end + + def clusterable_params + { project_id: clusterable.to_param, namespace_id: clusterable.namespace.to_param } + end +end |