felixclack / redis-timeline

Redis-backed timeline for activity feeds
http://felixclack.github.com/redis-timeline
MIT License
92 stars 30 forks source link

Tracking more fine-grained changes #10

Open Iteratix opened 10 years ago

Iteratix commented 10 years ago

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.

chadwtaylor commented 10 years ago

+1, please lord.

felixclack commented 10 years ago

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
bauschri commented 10 years ago

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

fidgetwidget commented 10 years ago

Has this been implemented yet? +1