This is perhaps somewhat along the lines of this discussion (another issue on this board) and then again perhaps not - which is why I decided to go with a fresh one.
My issue: part of my partial updates, but one method on the model does not…
I have this fairly simple construct partial with – “the culprit” (resource.nbr_pallets
) sitting right in the middle of it –
<!-- views/stock_items/_stock_item.html.erb -->
<tr id="<%= dom_id resource %>" data-controller="list-item">
<%= render_component "list_checkbox", resource: resource %>
<td class="...">
<%= link_to resource.asset.name, stock_item_path(resource), data: { turbo_frame: "_top" } %>
</td>
---8<---
<td class="...">
<%= resource.nbr_pallets %>
</td>
<td class="...">
<%= resource.quantity %>
</td>
---8<---
<td class="...">
<%= record_state resource.asset %>
</td>
</td>
</tr>
which gets ‘called upon’ when a stock_transaction is processed (and while running its due course the processing updates the stock_item,
# stock_item.rb
def self.subtract_quantity( s, st, parm)
sp = st.stocked_product
sl = st.stock_location
si = self.find_by( stocked_product_id: sp.id, stock_location: sl.id, batch_number: parm["batchnbr"])
if si
si.update_attribute :quantity, (si.quantity - st.quantity)
else
si = self.create_for_stock_transaction( s, sp, sl, parm)
si.update_attribute :quantity, 0
end
si
end
and after the fact broadcasts like this)
def broadcast_update
if self.asset.deleted_at.nil?
broadcast_replace_later_to model_name.plural, partial: self, locals: { resource: self }
broadcast_replace_later_to "stocked_products", partial: "stocked_products/stocked_product", locals: { resource: self.stocked_product }
else
broadcast_remove_to model_name.plural, target: self
broadcast_replace_later_to "stocked_products", partial: "stocked_products/stocked_product", locals: { resource: self.stocked_product }
end
end
eventually getting ‘merged’ into this view
<!-- views/stocked_products/show.html.erb -->
<div
class="..."
data-controller="tabs"
data-action="speicherMessage@window->tabs#handleMessages"
data-tabs-active-tab="..." >
<%= render_component "show_header",
title: resource.name,
breadcrumbs: @breadcrumbs,
current: Current,
tabs: [
--8<--
{id: "stock_items_tab", url: stocked_product_stock_items_url(resource), label: t('.stock_items'), action: "click->tabs#change" },
--8<--
</div>
<div id="stock_items_tab" class="..." data-tabs-target="panel">
<%= turbo_stream_from "stock_items" %>
<%= turbo_frame_tag "stock_items", loading: "lazy", src: stocked_product_stock_items_url(resource) do %>
<%= t('loading') %>
<% end %>
</div>
--8<--
</div>
and like I started out - most of the partial updates the view just perfectly - but the nbr_pallets
function on the stock_item is not executed
AFAICS
- and - yes - the partial is working 'cause when I reload the view all is good
BTW: the method on stock_item.rb that is ‘acting up’ looks like this
def nbr_pallets
q = stock_transactions.pluck :state
p = 0
q.each{ |s| case s when 'RECEIVE'; p+=1 when 'SHIP'; p-=1 end } if q.any?
p
end