Module: ApiNotify::ActiveRecord::Main

Extended by:
ActiveSupport::Concern
Defined in:
lib/api_notify/active_record/main.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

METHODS =
%w[post get delete put]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

Instance Method Details

#attributes_as_params(method) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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_notifyObject



59
60
61
# File 'lib/api_notify/active_record/main.rb', line 59

def disable_api_notify
  self.skip_api_notify = true
end

#enable_api_notifyObject



63
64
65
# File 'lib/api_notify/active_record/main.rb', line 63

def enable_api_notify
  self.skip_api_notify = false
end