module Tantot::Observe::ActiveRecordMethods

Public Instance Methods

_watch_changes() click to toggle source
# File lib/tantot/observe.rb, line 44
def _watch_changes
  Tantot.config.use_after_commit_callbacks ? self.previous_changes : self.changes
end
watch(*args, &block) click to toggle source

watch watcher, :attr, :attr, :attr, option: :value watch :attr, :attr, option: :value, &block watch watcher, option: :value watch option: :value, &block

# File lib/tantot/observe.rb, line 53
def watch(*args, &block)
  options = args.extract_options!

  # Syntax allows for the first argument to be a watcher class, shift
  # it if it is a string or class
  watcher = args.first.is_a?(String) || args.first.is_a?(Class) ? args.shift : nil

  raise ArgumentError.new("Only symbols are allowed as attribute filters") unless args.all? {|arg| arg.is_a?(Symbol)}
  raise ArgumentError.new("Only one of arguments or :only option are valid attribute filters") if args.any? && options.key?(:only)

  only_attributes = Array.wrap(options.fetch(:only, args)).collect(&:to_s)
  always_attributes = Array.wrap(options.fetch(:always, [])).collect(&:to_s)

  # Setup watch
  watch = OpenStruct.new
  watch.model = self
  watch.attributes ={
    only: only_attributes,
    always: always_attributes,
    watched: only_attributes | always_attributes
  }
  watch.options = options
  watch.block = block
  watch.watcher = watcher

  agent = Tantot.agent_registry.register(watch)

  # Setup and register callbacks
  callback_options = {}.tap do |opts|
    opts[:if] = Observe.condition_proc(watch) if watch.attributes[:only].any? || watch.options.key?(:if)
    opts[:on] = watch.options[:on] if watch.options.key?(:on)
  end
  update_proc = Observe.update_proc(watch)
  if Tantot.config.use_after_commit_callbacks
    after_commit(callback_options, &update_proc)
  else
    after_save(callback_options, &update_proc)
    after_destroy(callback_options, &update_proc)
  end

  agent
end