Class: CustomActiveRecordObserver::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_active_record_observer/dsl.rb

Overview

A main methods that are supposed to be used within CustomActiveRecordObserver

Author:

  • OnApp Ltd.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ DSL

Returns a new instance of DSL.



10
11
12
13
14
# File 'lib/custom_active_record_observer/dsl.rb', line 10

def initialize(block)
  @actions_and_rules = []

  instance_exec(&block)
end

Instance Attribute Details

#actions_and_rulesObject (readonly)

Returns the value of attribute actions_and_rules.



7
8
9
# File 'lib/custom_active_record_observer/dsl.rb', line 7

def actions_and_rules
  @actions_and_rules
end

Instance Method Details

#on_add(*attributes, &block) ⇒ Object

Executes the code inside a block when the specific attributes are updated from nil to some not nil value

Examples:

on_add :user_group_id do |user|
  # here we know that user_group_id was nil, but now it is not
end


62
63
64
# File 'lib/custom_active_record_observer/dsl.rb', line 62

def on_add(*attributes, &block)
  on_update(*attributes.push([nil, NotNil]), &block)
end

#on_change(*attributes, &block) ⇒ Object

Executes the code inside a block when the specific attributes are updated from some not nil to another not nil

Examples:

on_change :user_group_id do |user|
  # user was assigned to another user group
end


82
83
84
# File 'lib/custom_active_record_observer/dsl.rb', line 82

def on_change(*attributes, &block)
  on_update(*attributes.push([NotNil, NotNil]), &block)
end

#on_create(&block) ⇒ Object

Executes the code inside a block after the record is created



17
18
19
# File 'lib/custom_active_record_observer/dsl.rb', line 17

def on_create(&block)
  store(:create, Rules::CreateRule.new(block))
end

#on_destroy(&block) ⇒ Object

Executes the code inside a block after the record is destroyed



22
23
24
# File 'lib/custom_active_record_observer/dsl.rb', line 22

def on_destroy(&block)
  store(:destroy, Rules::DestroyRule.new(block))
end

#on_remove(*attributes, &block) ⇒ Object

Executes the code inside a block when the specific attributes are updated from some not nil value to nil

Examples:

on_remove :user_group_id do |user|
  # here we know that user_group_id is equal to nil now
end


72
73
74
# File 'lib/custom_active_record_observer/dsl.rb', line 72

def on_remove(*attributes, &block)
  on_update(*attributes.push([NotNil, nil]), &block)
end

#on_update(*attributes, &block) ⇒ Object

Launches code inside a block if an object changes any of attributes according to the pattern which is described in the brackets (value before -> value after)

This uses ‘===` method to compare values. Will be launched only if user_group_id was changed from `nil` to anything but nil. in the array [] there are two values that are compared with the real changes to detect if they maches.

Examples:

Add hook on update any of attributes according to the pattern

on_update :address, :phone_number, [CustomActiveRecordObserver::NotNill,
                                    CustomActiveRecordObserver::NotNill] do |user|
  # notify system that user contact info was changed
end

Track nil -> not nill changes of user_group_id attribute

on_update :user_group_id, [nil, CustomActiveRecordObserver::NotNill] do |user|
  # here we know that some user_group was assigned to user
end

Other examples of usage:

on_update :user_group_id, [1, 2] { |user| ... }
on_update :user_group_id, [1, Integer] { |user| ... }

Parameters:

  • attributes (Symbols)
    • list of attribute names (e.g. :user_id, :user_group_id …)

  • values (Array(2))
    • array of two values (before and after)



48
49
50
51
52
53
54
# File 'lib/custom_active_record_observer/dsl.rb', line 48

def on_update(*attributes, &block)
  pattern = attributes.pop if attributes.last.is_a?(Array)

  attributes.each do |attribute|
    store(:update, Rules::UpdateRule.new(block, attribute: attribute, pattern: pattern))
  end
end