aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-06-12 18:59:23 +0200
committerTeddy Wing2017-06-12 18:59:23 +0200
commit07ed048cd2f2231fa26345a94c725363344ee77b (patch)
tree472445c9931d139629516e312a9daa640bc16843
parent53c68600f1e3f80d2765fe41ed3a451c17eeabea (diff)
downloadchouette-core-07ed048cd2f2231fa26345a94c725363344ee77b.tar.bz2
referentials/show.html.slim: Extract header buttons to a decorator
Add a new `ReferentialDecorator` that we can use to define the links that appear as buttons in the header of the `/referentials/:id` page. The idea is that these links will take the place of the existing view code and in the view, we'll instead loop over the links provided by the decorator and render them as buttons. I decided to do it this way because there's a requirement that the links in the gear menu in tables show the same links that appear in the header of the page. So the Workbenches#show page would show a table of referentials, and their gear menu links should ostensibly be the same as the header links in the Referentials#show page. My goal was to abstract links, and try to separate them from the presentation layer. Still having trouble with this though. I created a new `Link` class to represent a link. We can then use this in the template to create a `link_to`. It becomes messy, though, when we want to put other elements inside a certain link, as evidenced by the `:delete` link. Don't like how that's done, but still working out the best approach to make it cleaner. Refs #3479
-rw-r--r--app/controllers/referentials_controller.rb2
-rw-r--r--app/decorators/referential_decorator.rb47
-rw-r--r--app/views/referentials/show.html.slim47
-rw-r--r--lib/link.rb10
4 files changed, 90 insertions, 16 deletions
diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb
index c65e6552c..50b2e47c6 100644
--- a/app/controllers/referentials_controller.rb
+++ b/app/controllers/referentials_controller.rb
@@ -26,6 +26,8 @@ class ReferentialsController < BreadcrumbController
def show
resource.switch
show! do |format|
+ @referential = ReferentialDecorator.new(@referential)
+
format.json {
render :json => { :lines_count => resource.lines.count,
:networks_count => resource.networks.count,
diff --git a/app/decorators/referential_decorator.rb b/app/decorators/referential_decorator.rb
new file mode 100644
index 000000000..1c2347529
--- /dev/null
+++ b/app/decorators/referential_decorator.rb
@@ -0,0 +1,47 @@
+class ReferentialDecorator < Draper::Decorator
+ delegate_all
+
+ def action_links
+ links = [
+ Link.new(
+ name: h.t('time_tables.index.title'),
+ href: h.referential_time_tables_path(object)
+ )
+ ]
+
+ if h.policy(object).clone?
+ links << Link.new(
+ name: h.t('actions.clone'),
+ href: h.new_referential_path(from: object.id)
+ )
+ end
+
+ if h.policy(object).edit?
+ # button.btn.btn-primary type='button' data-toggle='modal' data-target='#purgeModal' Purger
+
+ if object.archived?
+ links << Link.new(
+ name: h.t('actions.unarchive'),
+ href: h.unarchive_referential_path(object.id),
+ method: :put
+ )
+ else
+ links << Link.new(
+ name: h.t('actions.archive'),
+ href: h.archive_referential_path(object.id),
+ method: :put
+ )
+ end
+ end
+
+ if h.policy(object).destroy?
+ links << Link.new(
+ href: h.referential_path(object),
+ method: :delete,
+ data: { confirm: h.t('referentials.actions.destroy_confirm') }
+ )
+ end
+
+ links
+ end
+end
diff --git a/app/views/referentials/show.html.slim b/app/views/referentials/show.html.slim
index 3c1e36302..1bbf350b4 100644
--- a/app/views/referentials/show.html.slim
+++ b/app/views/referentials/show.html.slim
@@ -8,23 +8,38 @@
/ Below is secondary actions & optional contents (filters, ...)
.row.mb-sm
.col-lg-12.text-right
- = link_to t('time_tables.index.title'), referential_time_tables_path(@referential), class: 'btn btn-primary'
-
- - if policy(@referential).clone?
- = link_to t('actions.clone'), new_referential_path(from: @referential.id), class: 'btn btn-primary'
-
- - if policy(@referential).edit?
- button.btn.btn-primary type='button' data-toggle='modal' data-target='#purgeModal' Purger
-
- - if @referential.archived?
- = link_to t('actions.unarchive'), unarchive_referential_path(@referential.id), method: :put, class: 'btn btn-primary'
+ /= link_to t('time_tables.index.title'), referential_time_tables_path(@referential), class: 'btn btn-primary'
+ /
+ /- if policy(@referential).clone?
+ / = link_to t('actions.clone'), new_referential_path(from: @referential.id), class: 'btn btn-primary'
+ /
+ /- if policy(@referential).edit?
+ / button.btn.btn-primary type='button' data-toggle='modal' data-target='#purgeModal' Purger
+ /
+ / - if @referential.archived?
+ / = link_to t('actions.unarchive'), unarchive_referential_path(@referential.id), method: :put, class: 'btn btn-primary'
+ / - else
+ / = link_to t('actions.archive'), archive_referential_path(@referential.id), method: :put, class: 'btn btn-primary'
+ /
+ /- if policy(@referential).destroy?
+ / = link_to referential_path(@referential), method: :delete, data: {confirm: t('referentials.actions.destroy_confirm')}, class: 'btn btn-primary' do
+ / span.fa.fa-trash
+ / span = t('actions.destroy')
+ /= leslinks.map { |l| link_to l.href, l.label }
+ - @referential.action_links.each do |link|
+ - if link.method == :delete
+ = link_to link.href,
+ method: link.method,
+ data: link.data,
+ class: 'btn btn-primary' do
+ span.fa.fa-trash
+ span = t('actions.destroy')
- else
- = link_to t('actions.archive'), archive_referential_path(@referential.id), method: :put, class: 'btn btn-primary'
-
- - if policy(@referential).destroy?
- = link_to referential_path(@referential), method: :delete, data: {confirm: t('referentials.actions.destroy_confirm')}, class: 'btn btn-primary' do
- span.fa.fa-trash
- span = t('actions.destroy')
+ = link_to link.name,
+ link.href,
+ method: link.method,
+ data: link.data,
+ class: 'btn btn-primary'
/ PageContent
.page_content
diff --git a/lib/link.rb b/lib/link.rb
new file mode 100644
index 000000000..911f189c9
--- /dev/null
+++ b/lib/link.rb
@@ -0,0 +1,10 @@
+class Link
+ attr_reader :name, :href, :method, :data
+
+ def initialize(name: nil, href:, method: :get, data: nil)
+ @name = name
+ @href = href
+ @method = method
+ @data = data
+ end
+end