Class: ActiveModel::ObserverArray

### Home Classes Methods

### In Files

- lib/active_model/observer_array.rb

### Parent

Array

### Methods

- ::new
- #disable
- #disabled_for?
- #disabled_observer_stack
- #disabled_observers
- #each_subclass_array
- #enable
- #end_transaction
- #observer_class_for
- #set_enablement
- #start_transaction
- #transaction

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::ObserverArray

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

### Attributes

model_class[R]

### Public Class Methods

new(model_class, *args) click to toggle source

    # File lib/active_model/observer_array.rb, line 8
    def initialize(model_class, *args)
      @model_class = model_class
      super(*args)
    end

### Public Instance Methods

disable(*observers, &block) click to toggle source

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

    # File lib/active_model/observer_array.rb, line 37
    def disable(*observers, &block)
      set_enablement(false, observers, &block)
    end

disabled_for?(observer) click to toggle source

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

    # File lib/active_model/observer_array.rb, line 14
    def disabled_for?(observer)
      disabled_observers.include?(observer.class)
    end

enable(*observers, &block) click to toggle source

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.

    # File lib/active_model/observer_array.rb, line 64
    def enable(*observers, &block)
      set_enablement(true, observers, &block)
    end

### Protected Instance Methods

disabled_observer_stack() click to toggle source

    # File lib/active_model/observer_array.rb, line 92
    def disabled_observer_stack
      @disabled_observer_stack ||= []
    end

disabled_observers() click to toggle source

    # File lib/active_model/observer_array.rb, line 70
    def disabled_observers
      @disabled_observers ||= Set.new
    end

each_subclass_array() click to toggle source

    # File lib/active_model/observer_array.rb, line 113
    def each_subclass_array
      model_class.descendants.each do |subclass|
        yield subclass.observers
      end
    end

end_transaction() click to toggle source

    # File lib/active_model/observer_array.rb, line 96
    def end_transaction
      @disabled_observers = disabled_observer_stack.pop
      each_subclass_array do |array|
        array.end_transaction
      end
    end

observer_class_for(observer) click to toggle source

    # File lib/active_model/observer_array.rb, line 74
    def observer_class_for(observer)
      return observer if observer.is_a?(Class)

      if observer.respond_to?(:to_sym) # string/symbol
        observer.to_s.camelize.constantize
      else
        raise ArgumentError, "#{observer} was not a class or a " +
          "lowercase, underscored class name as expected."
      end
    end

set_enablement(enabled, observers) click to toggle source

    # File lib/active_model/observer_array.rb, line 119
    def set_enablement(enabled, observers)
      if block_given?
        transaction do
          set_enablement(enabled, observers)
          yield
        end
      else
        observers = ActiveModel::Observer.descendants if observers == [:all]
        observers.each do |obs|
          klass = observer_class_for(obs)

          unless klass < ActiveModel::Observer
            raise ArgumentError.new("#{obs} does not refer to a valid observer")
          end

          if enabled
            disabled_observers.delete(klass)
          else
            disabled_observers << klass
          end
        end

        each_subclass_array do |array|
          array.set_enablement(enabled, observers)
        end
      end
    end

start_transaction() click to toggle source

    # File lib/active_model/observer_array.rb, line 85
    def start_transaction
      disabled_observer_stack.push(disabled_observers.dup)
      each_subclass_array do |array|
        array.start_transaction
      end
    end

transaction() click to toggle source

    # File lib/active_model/observer_array.rb, line 103
    def transaction
      start_transaction

      begin
        yield
      ensure
        end_transaction
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.