redis-timeline icon indicating copy to clipboard operation
redis-timeline copied to clipboard

Tracking more fine-grained changes

Open Iteratix opened this issue 11 years ago • 4 comments

Hi there! We love this gem, and are testing integrating this with our web application. For the most part, it works great for our purposes -- new objects, edited objects, etc. However, we have a few large objects -- for example, an Invoice -- that is edited/updated in pieces rather than entirely.

What's the best way to fire a track event for an :update for, say, a specific method call or change on a model?

Example:

class Invoice < ActiveRecord::Base
  include Timeline::Track

  track :send_invoice,
    on: :update

  def send_invoice
    self.sent = true
    self.save
  end
end

In the above example, we would like to fire :send_invoice when the function is called. We explored using if: but quickly ran into issues, because its the action we want to track, not the state.

For example, suppose we added an if: Proc.new { self.sent == true }, that would trigger the :update event in the future if sent is true, if you were doing other updates to the Invoice model.

Thanks for any thoughts you might have! I think supporting something like this would be very powerful.

Iteratix avatar Dec 04 '13 03:12 Iteratix

+1, please lord.

chadwtaylor avatar Dec 04 '13 03:12 chadwtaylor

Just so I can understand what you're trying to do...

You want to create an activity named send_invoice, which should only get created when the method of the same name is called?

Would being able to call a method to record the activity directly be useful for you?

I'm working on some changes to the library so that it isn't so reliant on callbacks and consequently, ActiveRecord. It would allow you to do something like...

class Invoice < ActiveRecord::Base
  include Timeline::Track

  def send_invoice
    update_attributes sent: true
    track :send_invoice
  end
end

Or if you prefer to use a 'service' based approach, thereby decoupling the tracking from the object itself...

class SendInvoice
  include Timeline::Track

  def initialize(invoice)
    @invoice = invoice
  end

  attr_reader :invoice

  def run
    invoice.update_attributes :sent, true
    track :send_invoice, object: invoice
  end
end

felixclack avatar Dec 04 '13 09:12 felixclack

Great!! Thanks you for the solution :+1:

bauschri avatar Dec 10 '13 00:12 bauschri

Has this been implemented yet? +1

fidgetwidget avatar Jul 29 '14 23:07 fidgetwidget