module ApiNotify::ActiveRecord::Main

Constants

METHODS

Public Instance Methods

attributes_as_params(method) click to toggle source

Check if any watched attribute changed Unless any attribute changed than dont send any request if is_synchronized defined than check if its true, unless its true synchronize it

# File lib/api_notify/active_record/main.rb, line 72
def attributes_as_params(method)
  _fields = {}
  must_sync = false
  if defined? self.class.is_synchronized
    ApiNotify::LOGGER.info "Is synchronized: #{self.class.is_synchronized}"
    must_sync = !send(self.class.is_synchronized)
  end

  notify_attributes.each do |field|
    if send("#{field.to_s}_changed?") || must_sync
      _fields[field] = self.send(field)
    end
  end

  return _fields if _fields.empty? && method != "delete"

  identificators.each_pair do |key, value|
    _fields[key] = self.send(value)
  end

  ApiNotify::LOGGER.info "fields: #{_fields}"
  _fields
end
disable_api_notify() click to toggle source
# File lib/api_notify/active_record/main.rb, line 59
def disable_api_notify
  self.skip_api_notify = true
end
enable_api_notify() click to toggle source
# File lib/api_notify/active_record/main.rb, line 63
def enable_api_notify
  self.skip_api_notify = false
end
method_missing(m, *args) click to toggle source
Calls superclass method
# File lib/api_notify/active_record/main.rb, line 96
def method_missing(m, *args)
  vars = m.to_s.split(/_/, 2)
  if METHODS.include?(vars.first) && vars.last == "via_api"
    ApiNotify::LOGGER.info "called method: #{m}"
    return if skip_api_notify || attributes_as_params(vars.first).empty?
    synchronizer = self.class.synchronizer
    synchronizer.set_params(attributes_as_params(vars.first))
    synchronizer.send_request(vars.first.upcase)

    disable_api_notify

    if synchronizer.success?
      send("api_notify_#{vars.first}_success", synchronizer.response)
    else
      send("api_notify_#{vars.first}_failed", synchronizer.response)
    end

    enable_api_notify
  else
    super
  end
end