Class: ActiveModel::ObserverArray

Inherits:
Array show all
Defined in:
activemodel/lib/active_model/observer_array.rb

Overview

Stores the enabled/disabled state of individual observers for a particular model class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#as_json, #encode_json, #extract_options!, #fifth, #forty_two, #fourth, #from, #in_groups, #in_groups_of, #sample, #second, #split, #third, #to, #to_formatted_s, #to_param, #to_query, #to_sentence, #to_xml, #uniq_by, #uniq_by!, wrap

Constructor Details

#initialize(model_class, *args) ⇒ ObserverArray

Returns a new instance of ObserverArray


8
9
10
11
# File 'activemodel/lib/active_model/observer_array.rb', line 8

def initialize(model_class, *args)
  @model_class = model_class
  super(*args)
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class


7
8
9
# File 'activemodel/lib/active_model/observer_array.rb', line 7

def model_class
  @model_class
end

Instance Method Details

#disable(*observers, &block) ⇒ Object

Disables one or more observers. This supports multiple forms:

ORM.observers.disable :user_observer
  # => disables the UserObserver

User.observers.disable AuditTrail
  # => disables the AuditTrail observer for User notifications.
  #    Other models will still notify the AuditTrail observer.

ORM.observers.disable :observer_1, :observer_2
  # => disables Observer1 and Observer2 for all models.

ORM.observers.disable :all
  # => disables all observers for all models.

User.observers.disable :all do
  # all user observers are disabled for
  # just the duration of the block
end

37
38
39
# File 'activemodel/lib/active_model/observer_array.rb', line 37

def disable(*observers, &block)
  set_enablement(false, observers, &block)
end

#disabled_for?(observer) ⇒ Boolean

Returns true if the given observer is disabled for the model class.

Returns:

  • (Boolean)

14
15
16
# File 'activemodel/lib/active_model/observer_array.rb', line 14

def disabled_for?(observer)
  disabled_observers.include?(observer.class)
end

#enable(*observers, &block) ⇒ Object

Enables one or more observers. This supports multiple forms:

ORM.observers.enable :user_observer
  # => enables the UserObserver

User.observers.enable AuditTrail
  # => enables the AuditTrail observer for User notifications.
  #    Other models will not be affected (i.e. they will not
  #    trigger notifications to AuditTrail if previously disabled)

ORM.observers.enable :observer_1, :observer_2
  # => enables Observer1 and Observer2 for all models.

ORM.observers.enable :all
  # => enables all observers for all models.

User.observers.enable :all do
  # all user observers are enabled for
  # just the duration of the block
end

Note: all observers are enabled by default. This method is only useful when you have previously disabled one or more observers.


64
65
66
# File 'activemodel/lib/active_model/observer_array.rb', line 64

def enable(*observers, &block)
  set_enablement(true, observers, &block)
end