Override Spree Admin views with Deface

2.2k Views Asked by At

I'm trying to add a few lines to Spree admin.

File I would like to override:

backend/app/views/spree/admin/orders/_shipment.html.erb

I want to add it here:

<tr class="show-tracking total">
        <td colspan="5" class="tracking-value">
          <% if shipment.tracking.present? %>
            <strong><%= Spree.t(:tracking) %>:</strong> <%= shipment.tracking %>
          <% else %>
            <%= Spree.t(:no_tracking_present) %>
          <% end %>
        </td>
      </tr>

My override is:

<!-- insert_after '.tracking-value' -->
</br>
<strong><%= Spree.t(:is_delivered) %>: </strong><%= shipment.is_delivered %></br>
  <% if shipment.date_delivered? %>
    <strong><%= Spree.t(:date_delivered) %>: </strong><%= shipment.date_delivered %></br>
  <% end %>

located in app/overrides/spree/admin/orders/shipment/add_tracking_info.html.erb.deface

Content:

</br>
<strong><%= Spree.t(:is_delivered) %>: </strong><%= shipment.is_delivered %></br>
  <% if shipment.date_delivered? %>
    <strong><%= Spree.t(:date_delivered) %>: </strong><%= shipment.date_delivered %></br>
  <% end %>

I would like to render:

<tr class="show-tracking total">
        <td colspan="5" class="tracking-value">
          <% if shipment.tracking.present? %>
            <strong><%= Spree.t(:tracking) %>:</strong> <%= shipment.tracking %>
            </br>
           <strong><%= Spree.t(:is_delivered) %>: </strong><%= shipment.is_delivered %</br>
          <% if shipment.date_delivered? %>
            <strong><%= Spree.t(:date_delivered) %>: </strong><%= shipment.date_delivered %></br>
          <% end %>
          <% else %>
            <%= Spree.t(:no_tracking_present) %>
          <% end %>
        </td>
      </tr>

any help regarding this issue would be greatly appreciated, thank you so much!

1

There are 1 best solutions below

4
On BEST ANSWER

You are missing a step. In a very abstract fashion you need to tell spree where and what you need to insert.

Deface::Override.new(
  virtual_path: 'spree/admin/shared/_order_summary',    
  name:         'admin_order_custom_details',
  insert_after: 'header#order_tab_summary > dl.additional-info',
  partial:      'spree/admin/shared/admin_order_custom_details'
)

The following that is copied from a project I was working till yesterday. What does is it instructs Deface to add the partial 'spree/admin/shared/admin_order_custom_detailsjust after the dom element identified byheader#order_tab_summary > dl.additional-info`.

So as I see what you need to do is

  1. change html.erb.deface to just html.erb and move it to some where such that it will be like app/views/spree/admin/orders/add_order_detials_to_shipment.html.erb
  2. create a new file in app/overides/ (spoiler alert, make it a meaningful alert or regret later)
  3. Find the identifier (just looking at it I think it is some thing like erb[silent]:contains('if shipment.tracking.present?')
  4. use insert_bottom instead of insert_after

I have not run this, but I believe this is what you are looking for.

Deface::Override.new(
  virtual_path:  'app/views/spree/admin/orders/_shipment',    
  name:          'add_order_detials_to_shipment', # Or a fancy unique name
  insert_bottom: "erb[silent]:contains('if shipment.tracking.present?')",
  partial:       'app/views/spree/admin/orders/add_order_detials_to_shipment'
)

PS

After reading that section of the doc, which I wasn't aware there was a DSL for Deface, I think you need to update the DSL such that

<!-- insert_bottom "erb[silent]:contains('if shipment.tracking.present?')" -->
</br>
<strong><%= Spree.t(:is_delivered) %>: </strong><%= shipment.is_delivered %></br>
<% if shipment.date_delivered? %>
  <strong><%= Spree.t(:date_delivered) %>: </strong><%= shipment.date_delivered %></br>
<% end %>