diff options
| author | Zog | 2018-01-17 10:16:55 +0100 | 
|---|---|---|
| committer | Zog | 2018-01-17 10:16:55 +0100 | 
| commit | 5dcb49a6a0dfcb657a25f88a5b8303ba4c284a9f (patch) | |
| tree | f3033e2be940bcdad1a5e7214e96ffb471aada34 | |
| parent | a958ae52a763e09574c22625c2cd3481c2b1bb6c (diff) | |
| download | chouette-core-5dcb49a6a0dfcb657a25f88a5b8303ba4c284a9f.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
| -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 | ||||
| -rw-r--r-- | lib/af83/decorator.rb | 12 | ||||
| -rw-r--r-- | lib/af83/enhanced_decorator.rb | 4 | 
6 files changed, 109 insertions, 99 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 diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb index d2e049857..ba9a46cb0 100644 --- a/lib/af83/decorator.rb +++ b/lib/af83/decorator.rb @@ -226,7 +226,7 @@ class AF83::Decorator < ModelDecorator      def html_options        out = {}        options.each do |k, v| -        out[k] = v unless k == :content || k == :href || k.to_s =~ /^_/ +        out[k] = self.send(k) unless k == :content || k == :href || k.to_s =~ /^_/        end        out[:class] = extra_class        out.delete(:link_class) @@ -241,7 +241,15 @@ class AF83::Decorator < ModelDecorator          yield link          return link.bind_to_context(context, @action).to_html        end -      context.h.link_to content, href, html_options +      if type&.to_sym == :button +        HTMLElement.new( +          :button, +          content, +          html_options +        ).to_html +      else +        context.h.link_to content, href, html_options +      end      end    end diff --git a/lib/af83/enhanced_decorator.rb b/lib/af83/enhanced_decorator.rb index 4ade0980b..b4e679164 100644 --- a/lib/af83/enhanced_decorator.rb +++ b/lib/af83/enhanced_decorator.rb @@ -16,6 +16,10 @@ module AF83::EnhancedDecorator        @_action_links[weight] << link      end +    def t key +      eval  "-> (l){ h.t('#{key}') }" +    end +      def with_condition condition, &block        @_condition = condition        instance_eval &block | 
