diff options
| author | Teddy Wing | 2018-01-25 14:01:13 +0100 | 
|---|---|---|
| committer | Zog | 2018-01-25 17:18:03 +0100 | 
| commit | 2bf4cf2e3a9e54bc9b91bf3efcac721951df730d (patch) | |
| tree | 19c9c1c588b13b721c7d0aa61d7c9b1684f22cf4 | |
| parent | 9ee14d84ccfd19fb6a53c83826f352088f06b57c (diff) | |
| download | chouette-core-2bf4cf2e3a9e54bc9b91bf3efcac721951df730d.tar.bz2 | |
ComplianceControlDecorator: Convert to new action links interface
I was getting this error:
    ActionView::Template::Error (undefined method `split' for nil:NilClass
    Did you mean?  split_all):
        16:
        17:   / compliance controls without block
        18:   = render_compliance_control_block
        19:   = render_compliance_controls(@direct_compliance_controls)
        20:
        21:   / compliance controls with block
        22:   - if params[:q] && params[:q][:compliance_control_block_id_eq_any].try(:present?)
      app/helpers/table_builder_helper.rb:110:in `item_row_class_name'
      app/helpers/table_builder_helper.rb:290:in `tbody'
      app/helpers/table_builder_helper.rb:97:in `table_builder_2'
      app/helpers/compliance_control_sets_helper.rb:79:in `block in render_table_builder'
      app/helpers/compliance_control_sets_helper.rb:78:in `render_table_builder'
      app/helpers/compliance_control_sets_helper.rb:71:in `block (2 levels) in render_compliance_controls'
      app/helpers/compliance_control_sets_helper.rb:70:in `block in render_compliance_controls'
      app/helpers/compliance_control_sets_helper.rb:69:in `render_compliance_controls'
      app/views/compliance_control_sets/show.html.slim:19:in `_app_views_compliance_control_sets_show_html_slim___3528509151208629266_70279137561480'
The model name wasn't accessible from the view because this collection
is set up in an unorthodox way without a decorated collection.
Got help from Johan, and we ended up using the `model` param passed into
the table builder for this. It looks terrible with `model` all over the
place, but at least it gets us past the problem.
Refs #5586
| -rw-r--r-- | app/controllers/compliance_control_sets_controller.rb | 5 | ||||
| -rw-r--r-- | app/decorators/compliance_control_decorator.rb | 51 | ||||
| -rw-r--r-- | app/helpers/compliance_control_sets_helper.rb | 3 | ||||
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 25 | ||||
| -rw-r--r-- | app/views/compliance_controls/show.html.slim | 2 | 
5 files changed, 44 insertions, 42 deletions
| diff --git a/app/controllers/compliance_control_sets_controller.rb b/app/controllers/compliance_control_sets_controller.rb index 0173838a2..ae1d01feb 100644 --- a/app/controllers/compliance_control_sets_controller.rb +++ b/app/controllers/compliance_control_sets_controller.rb @@ -42,10 +42,7 @@ class ComplianceControlSetsController < ChouetteController    end    def decorate_compliance_controls(compliance_controls) -    ModelDecorator.decorate( -      compliance_controls, -      with: ComplianceControlDecorator, -    ) +    ComplianceControlDecorator.decorate(compliance_controls)    end    def compliance_control_set_params diff --git a/app/decorators/compliance_control_decorator.rb b/app/decorators/compliance_control_decorator.rb index f56e80417..a0445faa2 100644 --- a/app/decorators/compliance_control_decorator.rb +++ b/app/decorators/compliance_control_decorator.rb @@ -1,30 +1,33 @@ -class ComplianceControlDecorator < Draper::Decorator -  delegate_all - -  def action_links -    policy = h.policy(object) -    links = [] - -    links << Link.new( -      content: h.t('compliance_control_sets.actions.show'), -      href:  h.compliance_control_set_compliance_control_path(object.compliance_control_set.id, object.id) -    ) +class ComplianceControlDecorator < AF83::Decorator +  with_instance_decorator do |instance_decorator| +    instance_decorator.show_action_link do |l| +      l.content h.t('compliance_control_sets.actions.show') +      l.href do +        h.compliance_control_set_compliance_control_path( +          object.compliance_control_set.id, +          object.id +        ) +      end +    end -    if policy.edit? -      links << Link.new( -        content: h.t('compliance_controls.actions.edit'), -        href:  h.edit_compliance_control_set_compliance_control_path(object.compliance_control_set.id, object.id) -      ) +    instance_decorator.edit_action_link do |l| +      l.href do +        h.edit_compliance_control_set_compliance_control_path( +          object.compliance_control_set_id, +          object.id +        ) +      end      end -    if policy.destroy? -      links << Link.new( -        content: h.destroy_link_content, -        href: h.compliance_control_set_compliance_control_path(object.compliance_control_set.id, object.id), -        method: :delete, -        data: { confirm: h.t('compliance_controls.actions.destroy_confirm') } -      ) +    instance_decorator.destroy_action_link do |l| +      l.content h.destroy_link_content +      l.href do +        h.compliance_control_set_compliance_control_path( +          object.compliance_control_set.id, +          object.id +        ) +      end +      l.data confirm: h.t('compliance_controls.actions.destroy_confirm')      end -    links    end  end diff --git a/app/helpers/compliance_control_sets_helper.rb b/app/helpers/compliance_control_sets_helper.rb index 57e6d9608..448d5c008 100644 --- a/app/helpers/compliance_control_sets_helper.rb +++ b/app/helpers/compliance_control_sets_helper.rb @@ -100,7 +100,8 @@ module ComplianceControlSetsHelper        ],        sortable: true,        cls: 'table has-filter has-search', -      model: ComplianceControl +      model: ComplianceControl, +      action: :index      end      metas = content_tag :div, I18n.t('compliance_control_blocks.metas.control', count: compliance_controls.count), class: 'pull-right'      table + metas diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 5c9da3d36..b123da802 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -94,18 +94,21 @@ module TableBuilderHelper    )      content_tag :table,        thead(collection, columns, sortable, selectable, links.any?, overhead, model || collection.model, action || params[:action]) + -        tbody(collection, columns, selectable, links, overhead, action || params[:action]), +        tbody(collection, columns, selectable, links, overhead, model, action || params[:action]),        class: cls    end -  def self.item_row_class_name collection -    if collection.respond_to?(:model) -      model_name = collection.model.name -    elsif collection.respond_to?(:first) -      model_name = collection.first.class.name -    else -      model_name = "item" -    end +  def self.item_row_class_name collection, model +    model_name = model&.name + +    model_name ||= +      if collection.respond_to?(:model) +        collection.model.name +      elsif collection.respond_to?(:first) +        collection.first.class.name +      else +        "item" +      end      model_name.split("::").last.parameterize    end @@ -286,8 +289,8 @@ module TableBuilderHelper      end    end -  def tbody(collection, columns, selectable, links, overhead, action) -    model_name = TableBuilderHelper.item_row_class_name collection +  def tbody(collection, columns, selectable, links, overhead, model = nil, action) +    model_name = TableBuilderHelper.item_row_class_name collection, model      content_tag :tbody do        collection.map do |item| diff --git a/app/views/compliance_controls/show.html.slim b/app/views/compliance_controls/show.html.slim index 54b07abf1..8a65bb864 100644 --- a/app/views/compliance_controls/show.html.slim +++ b/app/views/compliance_controls/show.html.slim @@ -1,6 +1,4 @@  - breadcrumb :compliance_control, @compliance_control -- content_for :page_header_actions do -  = link_to(t('actions.edit'), edit_compliance_control_set_compliance_control_path(params[:compliance_control_set_id], params[:id]), class: 'btn btn-default')  - page_header_content_for @compliance_control | 
