3

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!

neo
  • 4,078
  • 4
  • 25
  • 41

1 Answers1

2

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 %>
Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71
  • Thank you Ziyan, the project I'm working on uses a different style of deface... Your method works me for me as well... Check out: http://heridev.com.mx/ruby/overriding-an-admin-view-with-deface-in-spree-using-complex-selectors/ I'll probably go with your one though – neo Dec 06 '14 at 15:38
  • I updated the answer because I can't add code here. I think what you are needing is to change update the first line of the .deface file. Its good to keep consistency in the code. So if you have been using the DSL its good to fix it instead of go for the other. – Ziyan Junaideen Dec 06 '14 at 16:02
  • Thanks for the hint, some thing new I learnt today. I probably was too lazy that day to read the whole wiki. – Ziyan Junaideen Dec 06 '14 at 16:03
  • 1
    Thanks I'll test it out soon, this method is better for large projects, they don't just all go in overrides folder, a little more organized I find. – neo Dec 06 '14 at 16:48