module PorRelationCallbacks

Constants

VERSION

Attributes

object[RW]
prms[RW]

Public Class Methods

new(object, prms) click to toggle source
# File lib/por_relation_callbacks.rb, line 27
def initialize(object, prms)
  @object = object
  @prms = prms

  self._after_add    ||= []
  self._after_remove ||= []
end

Public Instance Methods

save() click to toggle source
# File lib/por_relation_callbacks.rb, line 35
def save
  return unless object
  ActiveRecord::Base.transaction do
    @existing_relation_state = object.send(_watched_relation).to_a
    object.update_attributes(prms)
    object.reload
    @updated_relation_state = object.send(_watched_relation).to_a
    hooks
  end
  true
end

Private Instance Methods

after_add() click to toggle source
# File lib/por_relation_callbacks.rb, line 54
def after_add
  return true if _after_add.empty?
  trigger_callbacks (@updated_relation_state - @existing_relation_state),
    _after_add
end
after_remove() click to toggle source
# File lib/por_relation_callbacks.rb, line 60
def after_remove
  return true if _after_remove.empty?
  trigger_callbacks (@existing_relation_state - @updated_relation_state),
    _after_remove
end
hooks() click to toggle source
# File lib/por_relation_callbacks.rb, line 49
def hooks
  after_add
  after_remove
end
trigger_callbacks(records, callbacks) click to toggle source
# File lib/por_relation_callbacks.rb, line 66
def trigger_callbacks(records, callbacks)
  records.each do |record|
    callbacks.each do |method|
      send(method, record)
    end
  end
end