diff options
author | Rémy Coutable <remy@rymai.me> | 2017-01-09 21:45:49 +0100 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-01-18 16:38:34 +0100 |
commit | bf789ff567c71ff68c216bfa8f3d43e09b6f49fb (patch) | |
tree | 28d4ac2aa74824eb5d85dc8271474980a3237bbe /lib | |
parent | fd72c0f4c748658f539d24a286366e9ac7a22b57 (diff) | |
download | gitlab-ce-bf789ff567c71ff68c216bfa8f3d43e09b6f49fb.tar.gz |
Improve presenter architecture
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/view/presenter.rb | 32 | ||||
-rw-r--r-- | lib/gitlab/view/presenter/base.rb | 26 | ||||
-rw-r--r-- | lib/gitlab/view/presenter/delegated.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/view/presenter/simple.rb | 17 |
4 files changed, 62 insertions, 32 deletions
diff --git a/lib/gitlab/view/presenter.rb b/lib/gitlab/view/presenter.rb deleted file mode 100644 index 8b63b271b99..00000000000 --- a/lib/gitlab/view/presenter.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Gitlab - module View - module Presenter - extend ActiveSupport::Concern - - included do - include Gitlab::Routing - include Gitlab::Allowable - end - - def with_subject(subject) - tap { @subject = subject } - end - - def with_user(user) - tap { @user = user } - end - - private - - attr_reader :subject, :user - - class_methods do - def presents(name) - define_method(name) do - subject - end - end - end - end - end -end diff --git a/lib/gitlab/view/presenter/base.rb b/lib/gitlab/view/presenter/base.rb new file mode 100644 index 00000000000..51e7ab064fe --- /dev/null +++ b/lib/gitlab/view/presenter/base.rb @@ -0,0 +1,26 @@ +module Gitlab + module View + module Presenter + module Base + extend ActiveSupport::Concern + + include Gitlab::Routing + include Gitlab::Allowable + + attr_reader :subject + + def can?(user, action) + super(user, action, subject) + end + + private + + class_methods do + def presents(name) + define_method(name) { subject } + end + end + end + end + end +end diff --git a/lib/gitlab/view/presenter/delegated.rb b/lib/gitlab/view/presenter/delegated.rb new file mode 100644 index 00000000000..f4d330c590e --- /dev/null +++ b/lib/gitlab/view/presenter/delegated.rb @@ -0,0 +1,19 @@ +module Gitlab + module View + module Presenter + class Delegated < SimpleDelegator + include Gitlab::View::Presenter::Base + + def initialize(subject, **attributes) + @subject = subject + + attributes.each do |key, value| + define_singleton_method(key) { value } + end + + super(subject) + end + end + end + end +end diff --git a/lib/gitlab/view/presenter/simple.rb b/lib/gitlab/view/presenter/simple.rb new file mode 100644 index 00000000000..b7653a0f3cc --- /dev/null +++ b/lib/gitlab/view/presenter/simple.rb @@ -0,0 +1,17 @@ +module Gitlab + module View + module Presenter + class Simple + include Gitlab::View::Presenter::Base + + def initialize(subject, **attributes) + @subject = subject + + attributes.each do |key, value| + define_singleton_method(key) { value } + end + end + end + end + end +end |