diff options
| author | Teddy Wing | 2017-07-11 17:05:59 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-07-12 11:11:14 +0200 |
| commit | ccbd1cfa43662933991dd85bef072e5ca0ae77c7 (patch) | |
| tree | 800b6e5f37efe33545b3018f7d931163fa4ce893 | |
| parent | d6185a881dc0428950629acfd16c632f413ac728 (diff) | |
| download | chouette-core-ccbd1cfa43662933991dd85bef072e5ca0ae77c7.tar.bz2 | |
Networks#show: Move header links into a decorator
In order to allow us to use these links in tables of networks via the
new table builder, move the link definitions into decorator
`#action_links`.
This changes both of these types of pages:
* http://stif-boiv.dev:3000/referentials/4/networks/118
* http://stif-boiv.dev:3000/line_referentials/1/networks/118
Refs #3479
| -rw-r--r-- | app/controllers/networks_controller.rb | 5 | ||||
| -rw-r--r-- | app/controllers/referential_networks_controller.rb | 8 | ||||
| -rw-r--r-- | app/decorators/network_decorator.rb | 44 | ||||
| -rw-r--r-- | app/decorators/referential_network_decorator.rb | 38 | ||||
| -rw-r--r-- | app/views/networks/show.html.slim | 14 | ||||
| -rw-r--r-- | app/views/referential_networks/show.html.slim | 14 |
6 files changed, 107 insertions, 16 deletions
diff --git a/app/controllers/networks_controller.rb b/app/controllers/networks_controller.rb index ea8410c5b..2e2f3f0d1 100644 --- a/app/controllers/networks_controller.rb +++ b/app/controllers/networks_controller.rb @@ -12,7 +12,12 @@ class NetworksController < BreadcrumbController def show @map = NetworkMap.new(resource).with_helpers(self) + show! do + @network = @network.decorate(context: { + line_referential: line_referential + }) + build_breadcrumb :show end end diff --git a/app/controllers/referential_networks_controller.rb b/app/controllers/referential_networks_controller.rb index 30c7dd244..8426c6ba3 100644 --- a/app/controllers/referential_networks_controller.rb +++ b/app/controllers/referential_networks_controller.rb @@ -10,7 +10,15 @@ class ReferentialNetworksController < ChouetteController def show @map = NetworkMap.new(resource).with_helpers(self) + show! do + @network = ReferentialNetworkDecorator.decorate( + @network, + context: { + referential: referential + } + ) + build_breadcrumb :show end end diff --git a/app/decorators/network_decorator.rb b/app/decorators/network_decorator.rb new file mode 100644 index 000000000..1f62fe512 --- /dev/null +++ b/app/decorators/network_decorator.rb @@ -0,0 +1,44 @@ +class NetworkDecorator < Draper::Decorator + decorates Chouette::Network + + delegate_all + + # Requires: + # context: { + # line_referential: , + # } + def action_links + links = [] + + if h.policy(Chouette::Network).create? + links << Link.new( + content: h.t('networks.actions.new'), + href: h.new_line_referential_network_path(context[:line_referential]) + ) + end + + if h.policy(object).update? + links << Link.new( + content: h.t('networks.actions.edit'), + href: h.edit_line_referential_network_path( + context[:line_referential], + object + ) + ) + end + + if h.policy(object).destroy? + links << Link.new( + content: h.destroy_link_content('networks.actions.destroy'), + href: h.line_referential_network_path( + context[:line_referential], + object + ), + method: :delete, + data: { confirm: t('networks.actions.destroy_confirm') } + ) + end + + links + end +end diff --git a/app/decorators/referential_network_decorator.rb b/app/decorators/referential_network_decorator.rb new file mode 100644 index 000000000..9eb94c8d2 --- /dev/null +++ b/app/decorators/referential_network_decorator.rb @@ -0,0 +1,38 @@ +class ReferentialNetworkDecorator < Draper::Decorator + decorates Chouette::Network + + delegate_all + + # Requires: + # context: { + # referential: , + # } + def action_links + links = [] + + if h.policy(Chouette::Network).create? + links << Link.new( + content: h.t('networks.actions.new'), + href: h.new_referential_network_path(context[:referential]) + ) + end + + if h.policy(object).update? + links << Link.new( + content: h.t('networks.actions.edit'), + href: h.edit_referential_network_path(context[:referential], object) + ) + end + + if h.policy(object).destroy? + links << Link.new( + content: h.destroy_link_content('networks.actions.destroy'), + href: h.referential_network_path(context[:referential], object), + method: :delete, + data: { confirm: t('networks.actions.destroy_confirm') } + ) + end + + links + end +end diff --git a/app/views/networks/show.html.slim b/app/views/networks/show.html.slim index 1f24fc5c1..09edbad2e 100644 --- a/app/views/networks/show.html.slim +++ b/app/views/networks/show.html.slim @@ -7,14 +7,12 @@ / Below is secundary actions & optional contents (filters, ...) .row .col-lg-12.text-right.mb-sm - - if policy(Chouette::Network).create? - = link_to t('networks.actions.new'), new_line_referential_network_path(@line_referential), class: 'btn btn-primary' - - if policy(@network).update? - = link_to t('networks.actions.edit'), edit_line_referential_network_path(@line_referential, @network), class: 'btn btn-primary' - - if policy(@network).destroy? - = link_to line_referential_network_path(@line_referential, @network), method: :delete, data: { confirm: t('networks.actions.destroy_confirm')}, class: 'btn btn-primary' do - span.fa.fa-trash - span = t('networks.actions.destroy') + - @network.action_links.each do |link| + = link_to link.href, + method: link.method, + data: link.data, + class: 'btn btn-primary' do + = link.content / PageContent .page_content diff --git a/app/views/referential_networks/show.html.slim b/app/views/referential_networks/show.html.slim index 59bdd0dee..b9d9d5d8b 100644 --- a/app/views/referential_networks/show.html.slim +++ b/app/views/referential_networks/show.html.slim @@ -7,14 +7,12 @@ / Below is secundary actions & optional contents (filters, ...) .row .col-lg-12.text-right.mb-sm - - if policy(Chouette::Network).create? - = link_to t('networks.actions.new'), new_referential_network_path(@referential), class: 'btn btn-primary' - - if policy(@network).update? - = link_to t('networks.actions.edit'), edit_referential_network_path(@referential, @network), class: 'btn btn-primary' - - if policy(@network).destroy? - = link_to referential_network_path(@referential, @network), method: :delete, data: { confirm: t('networks.actions.destroy_confirm')}, class: 'btn btn-primary' do - span.fa.fa-trash - span = t('networks.actions.destroy') + - @network.action_links.each do |link| + = link_to link.href, + method: link.method, + data: link.data, + class: 'btn btn-primary' do + = link.content / PageContent .page_content |
