Decorating Ruby on Rails model with Draper inside callback

33 Views Asked by At

I am using Cable Ready and draper gem in my rails project. I am having issues decorating inside a callback. This is what I have in the AppPayment's model callback:

  after_save do
    cable_ready[SplitsChannel].text_content(
      selector: "#payment-amount",
      text: payment.decorate.amount_f
    ).text_content(
      selector: "#left-to-pay",
      text: table_session.decorate.left_to_pay_f
    ).broadcast_to(split)
  end

When the callback is executed, I get in the server logs:

ActiveSupport::Notifications::InstrumentationSubscriberError (Exception(s) occurred within instrumentation subscribers: NoMethodError, NoMethodError):

This is the definition of amount_f in the PaymentDecorator:

  def amount_f
    h.to_brl(amount)
  end

left_to_pay_f is defined similarly. This is the definition of to_brl in ApplicationHelper:

  def to_brl(centavos)
    number_to_currency(centavos.to_f / 100, unit: "R$", separator: ",", delimiter: ".")
  end

Running payment.decorate.amount_f and table_session.decorate.left_to_pay_f on an AppPayment instance works as expected in rails c. However, when the callback is executed, I get the error above.

Using the unformatted payment.amount and table_session.left_to_pay values in the cable ready operations also works, but sets the inner text of the targetted DOM elements to the unformatted values. Therefore the problem seems to come from decorating the associated records inside the callback.

0

There are 0 best solutions below