diff options
| author | Zog | 2018-01-17 10:16:55 +0100 |
|---|---|---|
| committer | Zog | 2018-01-25 17:18:00 +0100 |
| commit | fc462f368bc45143b9f0b8649e5bec1f69e414c7 (patch) | |
| tree | 568369ff753ff1b2e7d74ad884d580d65d86671e /app | |
| parent | b802b16973bbf760aa8af720856c4127656c2e07 (diff) | |
| download | chouette-core-fc462f368bc45143b9f0b8649e5bec1f69e414c7.tar.bz2 | |
Refs #5586 @1h; Refactor ReferentialDecorator
Plus:
- Fix a bug on `html_options` in AF83::Decorator::Link
- Add a `t` helper in AF83::Decorator to handle i18n
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/referentials_controller.rb | 3 | ||||
| -rw-r--r-- | app/decorators/referential_decorator.rb | 142 | ||||
| -rw-r--r-- | app/views/layouts/navigation/_page_header.html.slim | 31 | ||||
| -rw-r--r-- | app/views/referentials/show.html.slim | 16 |
4 files changed, 95 insertions, 97 deletions
diff --git a/app/controllers/referentials_controller.rb b/app/controllers/referentials_controller.rb index 436d5ccb5..41ddc3870 100644 --- a/app/controllers/referentials_controller.rb +++ b/app/controllers/referentials_controller.rb @@ -80,6 +80,7 @@ class ReferentialsController < ChouetteController referential.archive! redirect_to workbench_path(referential.workbench_id), notice: t('notice.referential.archived') end + def unarchive if referential.unarchive! flash[:notice] = t('notice.referential.unarchived') @@ -97,7 +98,7 @@ class ReferentialsController < ChouetteController helper_method :current_referential def resource - @referential ||= current_organisation.find_referential(params[:id]) + @referential ||= current_organisation.find_referential(params[:id]).decorate end def collection diff --git a/app/decorators/referential_decorator.rb b/app/decorators/referential_decorator.rb index d75ad1050..ebde97671 100644 --- a/app/decorators/referential_decorator.rb +++ b/app/decorators/referential_decorator.rb @@ -1,88 +1,100 @@ -class ReferentialDecorator < Draper::Decorator - delegate_all +class ReferentialDecorator < AF83::Decorator + decorates Referential - def action_links - policy = h.policy(object) - links = [] + with_instance_decorator do |instance_decorator| + instance_decorator.action_link primary: %i(show index) do |l| + l.content { h.t('actions.edit') } + l.href { [object] } + end - if has_feature?(:referential_vehicle_journeys) - links << Link.new( - content: h.t('referential_vehicle_journeys.index.title'), - href: h.referential_vehicle_journeys_path(object) - ) + instance_decorator.action_link feature: :referential_vehicle_journeys, secondary: :show do |l| + l.content t('referential_vehicle_journeys.index.title') + l.href { h.referential_vehicle_journeys_path(object) } end - if has_feature?(:purchase_windows) - links << Link.new( - content: h.t('purchase_windows.index.title'), - href: h.referential_purchase_windows_path(object) - ) + instance_decorator.action_link feature: :purchase_windows, secondary: :show do |l| + l.content t('purchase_windows.index.title') + l.href { h.referential_purchase_windows_path(object) } end - links << Link.new( - content: h.t('time_tables.index.title'), - href: h.referential_time_tables_path(object) - ) + instance_decorator.action_link secondary: :show do |l| + l.content t('time_tables.index.title') + l.href { h.referential_time_tables_path(object) } + end - if policy.clone? - links << Link.new( - content: h.t('actions.clone'), - href: h.new_referential_path(from: object.id, current_workbench_id: context[:current_workbench_id]) - ) + instance_decorator.action_link policy: :clone, secondary: :show do |l| + l.content t('actions.clone') + l.href { h.new_referential_path(from: object.id, current_workbench_id: context[:current_workbench_id]) } end - if policy.validate? - links << Link.new( - content: h.t('actions.validate'), - href: h.referential_select_compliance_control_set_path(object.id) - ) + instance_decorator.action_link policy: :validate, secondary: :show do |l| + l.content t('actions.validate') + l.href { h.referential_select_compliance_control_set_path(object.id) } end - if policy.archive? - links << Link.new( - content: h.t('actions.archive'), - href: h.archive_referential_path(object.id), - method: :put - ) + instance_decorator.action_link policy: :archive, secondary: :show do |l| + l.content t('actions.archive') + l.href { h.archive_referential_path(object.id) } + l.method :put end - if policy.unarchive? - links << Link.new( - content: h.t('actions.unarchive'), - href: h.unarchive_referential_path(object.id), - method: :put - ) + instance_decorator.action_link policy: :unarchive, secondary: :show do |l| + l.content t('actions.unarchive') + l.href { h.unarchive_referential_path(object.id) } + l.method :put end - if policy.edit? - links << HTMLElement.new( - :button, - 'Purger', - type: 'button', - data: { - toggle: 'modal', - target: '#purgeModal' - } - ) + instance_decorator.action_link policy: :edit, secondary: :show do |l| + l.content 'Purger' + l.href '#' + l.type 'button' + l.data {{ + toggle: 'modal', + target: '#purgeModal' + }} end - if policy.destroy? - links << Link.new( - content: h.destroy_link_content, - href: h.referential_path(object), - method: :delete, - data: { confirm: h.t('referentials.actions.destroy_confirm') } - ) + instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l| + l.content { h.destroy_link_content } + l.href { h.referential_path(object) } + l.method { :delete } + l.data {{ confirm: h.t('referentials.actions.destroy_confirm') }} end - links end + # def action_links + # policy = h.policy(object) + # links = [] + # + # if policy.edit? + # links << HTMLElement.new( + # :button, + # 'Purger', + # type: 'button', + # data: { + # toggle: 'modal', + # target: '#purgeModal' + # } + # ) + # end + # + # if policy.destroy? + # links << Link.new( + # content: h.destroy_link_content, + # href: h.referential_path(object), + # method: :delete, + # data: { confirm: h.t('referentials.actions.destroy_confirm') } + # ) + # end - private - - # TODO move to a base Decorator (ApplicationDecorator) - def has_feature?(*features) - h.has_feature?(*features) rescue false - end + # links + # end + # + # private + # + # # TODO move to a base Decorator (ApplicationDecorator) + # def has_feature?(*features) + # h.has_feature?(*features) rescue false + # end end diff --git a/app/views/layouts/navigation/_page_header.html.slim b/app/views/layouts/navigation/_page_header.html.slim index c916c4037..8240aa4c0 100644 --- a/app/views/layouts/navigation/_page_header.html.slim +++ b/app/views/layouts/navigation/_page_header.html.slim @@ -1,22 +1,22 @@ - action_links = resource.action_links(params[:action]) rescue nil - action_links ||= decorated_collection.action_links(params[:action]) rescue nil -div.page_header - div.container-fluid - div.row - div.col-lg-9.col-md-8.col-sm-7.col-xs-7 +.page_header + .container-fluid + .row + .col-lg-9.col-md-8.col-sm-7.col-xs-7 - if defined?(resource_class) - div.page-icon + .page-icon span.sb class="sb-#{resource_class.model_name.name.underscore}" - div.page-title + .page-title - if content_for? :page_header_title h1 = yield :page_header_title - else - if defined?(resource_class) h1 = t("#{resource_class.model_name.name.underscore.pluralize}.#{params[:action]}.title") - div.col-lg-3.col-md-4.col-sm-5.col-xs-5.text-right - div.page-action + .col-lg-3.col-md-4.col-sm-5.col-xs-5.text-right + .page-action - if content_for? :page_header_meta = yield :page_header_meta - if content_for? :page_header_actions @@ -27,10 +27,11 @@ div.page_header - l.class "btn btn-default #{l.disabled ? "disabled" : ""}" - if action_links&.secondary&.any? || content_for?(:page_header_content) - .row - .col-lg-12.text-right.mb-sm - - action_links && action_links.secondary.each do |link| - = link.to_html do |l| - - l.class "btn btn-primary #{l.disabled ? "disabled" : ""}" - - if content_for? :page_header_content - = yield :page_header_content + .container-fluid + .row + .col-lg-12.text-right.mb-sm + - action_links && action_links.secondary.each do |link| + = link.to_html do |l| + - l.class "btn btn-primary #{l.disabled ? "disabled" : ""}" + - if content_for? :page_header_content + = yield :page_header_content diff --git a/app/views/referentials/show.html.slim b/app/views/referentials/show.html.slim index cbb622c44..46019be35 100644 --- a/app/views/referentials/show.html.slim +++ b/app/views/referentials/show.html.slim @@ -1,21 +1,5 @@ - breadcrumb @referential - page_header_content_for @referential -- content_for :page_header_actions do - - unless (@referential.referential_read_only? || !policy(@referential).edit?) - = link_to(t('actions.edit'), edit_referential_path(@referential), class: 'btn btn-default') - -- content_for :page_header_content do - .row.mb-sm - .col-lg-12.text-right - - @referential.action_links.each do |link| - - if link.is_a?(HTMLElement) - = link.to_html(class: 'btn btn-primary') - - else - = link_to link.href, - method: link.method, - data: link.data, - class: 'btn btn-primary' do - = link.content .page_content .container-fluid |
