diff options
| author | Zog | 2018-01-15 10:34:30 +0100 |
|---|---|---|
| committer | Zog | 2018-01-25 17:17:57 +0100 |
| commit | 8c9ce8f5c143d1e9d3f2c038e447a01ee59c91d3 (patch) | |
| tree | ed94d90b5ae6e6ad77973bd0e8dddcf335a0d995 /app | |
| parent | 5ecadfdead964381304fcf56a2564e2045988ef7 (diff) | |
| download | chouette-core-8c9ce8f5c143d1e9d3f2c038e447a01ee59c91d3.tar.bz2 | |
Refs #5586 @4h; First `action_links` refactor
- Implement new API
- Migrate the LineDecorator
- ensure no change on the "lines/index" view
Diffstat (limited to 'app')
| -rw-r--r-- | app/decorators/line_decorator.rb | 111 | ||||
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 2 | ||||
| -rw-r--r-- | app/views/lines/index.html.slim | 1 |
3 files changed, 52 insertions, 62 deletions
diff --git a/app/decorators/line_decorator.rb b/app/decorators/line_decorator.rb index 9c0cf7292..8e8e3bbc5 100644 --- a/app/decorators/line_decorator.rb +++ b/app/decorators/line_decorator.rb @@ -1,76 +1,67 @@ -class LineDecorator < Draper::Decorator +class LineDecorator < AF83::Decorator decorates Chouette::Line delegate_all - # Requires: - # context: { - # line_referential: , - # current_organisation: - # } - def action_links - links = [] + ### primary (and secondary) can be + ### - a single action + ### - an array of actions + ### - a boolean - links << Link.new( - content: h.t('lines.actions.show_network'), - href: [context[:line_referential], object.network] - ) + action_link primary: :index do |l| + l.content h.t('lines.actions.show') + l.href { [context[:line_referential], object] } + end - links << Link.new( - content: h.t('lines.actions.show_company'), - href: [context[:line_referential], object.company], - disabled: object.company.nil? - ) + action_link do |l| + l.content h.t('lines.actions.show_network') + l.href { [context[:line_referential], object.network] } + end - if h.policy(Chouette::Line).create? && - context[:line_referential].organisations.include?( - context[:current_organisation] - ) - links << Link.new( - content: h.t('lines.actions.edit'), - href: h.edit_line_referential_line_path(context[:line_referential], object.id) - ) - end + action_link do |l| + l.content { h.t('lines.actions.show_company') } + l.href { [context[:line_referential], object.company] } + l.disabled { object.company.nil? } + end - if h.policy(Chouette::Line).create? && - context[:line_referential].organisations.include?( - context[:current_organisation] - ) - links << Link.new( - content: h.t('lines.actions.new'), - href: h.new_line_referential_line_path(context[:line_referential]) - ) - end + can_edit_line = ->(){ h.policy(Chouette::Line).create? && context[:line_referential].organisations.include?(context[:current_organisation]) } - if h.policy(object).deactivate? - links << Link.new( - content: h.deactivate_link_content('lines.actions.deactivate'), - href: h.deactivate_line_referential_line_path(context[:line_referential], object), - method: :put, - data: {confirm: h.t('lines.actions.deactivate_confirm')}, - extra_class: "delete-action" - ) + with_condition can_edit_line do + action_link on: :index do |l| + l.content { h.t('lines.actions.new') } + l.href { h.new_line_referential_line_path(context[:line_referential]) } end - if h.policy(object).activate? - links << Link.new( - content: h.activate_link_content('lines.actions.activate'), - href: h.activate_line_referential_line_path(context[:line_referential], object), - method: :put, - data: {confirm: h.t('lines.actions.activate_confirm')}, - extra_class: "delete-action" - ) + action_link on: %i(index show), primary: :show do |l| + l.content { h.t('lines.actions.edit') } + l.href { h.edit_line_referential_line_path(context[:line_referential], object.id) } end + end - if h.policy(object).destroy? - links << Link.new( - content: h.destroy_link_content('lines.actions.destroy'), - href: h.line_referential_line_path(context[:line_referential], object), - method: :delete, - data: {confirm: h.t('lines.actions.destroy_confirm')} - ) - end + ### the option :policy will automatically check for the corresponding method + ### on the object's policy + + action_link policy: :deactivate, secondary: true do |l| + l.content { h.deactivate_link_content('lines.actions.deactivate') } + l.href { h.deactivate_line_referential_line_path(context[:line_referential], object) } + l.method :put + l.data confirm: h.t('lines.actions.deactivate_confirm') + l.extra_class "delete-action" + end + + action_link policy: :activate, secondary: true do |l| + l.content { h.activate_link_content('lines.actions.activate') } + l.href { h.activate_line_referential_line_path(context[:line_referential], object) } + l.method :put + l.data confirm: h.t('lines.actions.activate_confirm') + l.extra_class "delete-action" + end - links + action_link policy: :destroy do |l| + l.content { h.destroy_link_content('lines.actions.destroy') } + l.href { h.line_referential_line_path(context[:line_referential], object) } + l.method :delete + l.data confirm: h.t('lines.actions.destroy_confirm') + l.extra_class "delete-action" end end diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index dede51920..e66e9c942 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -309,7 +309,7 @@ module TableBuilderHelper menu = content_tag :ul, class: 'dropdown-menu' do ( CustomLinks.new(item, pundit_user, links, referential).links + - item.action_links.select { |link| link.is_a?(Link) } + item.action_links ).map do |link| gear_menu_link(link) end.join.html_safe diff --git a/app/views/lines/index.html.slim b/app/views/lines/index.html.slim index b62263263..399a2d8e1 100644 --- a/app/views/lines/index.html.slim +++ b/app/views/lines/index.html.slim @@ -52,7 +52,6 @@ attribute: Proc.new { |n| n.transport_submode.present? ? t("enumerize.transport_submode.#{n.try(:transport_submode)}") : "-" } \ ) \ ], - links: [:show], cls: 'table has-filter has-search' = new_pagination @lines, 'pull-right' |
