Sha256: ea06453b6dec8e04a0382eb2e7c5c49490d64eea8c374d130a94a6e5039e2b6e

Contents?: true

Size: 1.26 KB

Versions: 12

Compression:

Stored size: 1.26 KB

Contents

require 'active_record/callbacks'

module ActiveLdap
  module Callbacks
    def self.append_features(base)
      super

      base.class_eval do
        include ActiveRecord::Callbacks

        unless respond_to?(:instantiate_with_callbacks)
          extend ClassMethods
          class << self
            alias_method_chain :instantiate, :callbacks
          end
          alias_method_chain :initialize, :callbacks
        end

        def callback(method)
          super
        rescue ActiveRecord::ActiveRecordError
          raise Error, $!.message
        end
      end
    end

    module ClassMethods
      def instantiate_with_callbacks(record)
        object = instantiate_without_callbacks(record)

        if object.respond_to_without_attributes?(:after_find)
          object.send(:callback, :after_find)
        end

        if object.respond_to_without_attributes?(:after_initialize)
          object.send(:callback, :after_initialize)
        end

        object
      end
    end

    def initialize_with_callbacks(attributes = nil) #:nodoc:
      initialize_without_callbacks(attributes)
      result = yield self if block_given?
      if respond_to_without_attributes?(:after_initialize)
        callback(:after_initialize)
      end
      result
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
activeldap-1.2.4 lib/active_ldap/callbacks.rb
activeldap-1.2.3 lib/active_ldap/callbacks.rb
activeldap3-1.2.3 lib/active_ldap/callbacks.rb
activeldap-1.2.2 lib/active_ldap/callbacks.rb
activeldap-1.2.1 lib/active_ldap/callbacks.rb
activeldap-1.2.0 lib/active_ldap/callbacks.rb
activeldap-1.1.0 lib/active_ldap/callbacks.rb
activeldap-1.0.0 lib/active_ldap/callbacks.rb
activeldap-0.10.0 lib/active_ldap/callbacks.rb
activeldap-1.0.9 lib/active_ldap/callbacks.rb
activeldap-1.0.2 lib/active_ldap/callbacks.rb
activeldap-1.0.1 lib/active_ldap/callbacks.rb