Sha256: d18423f6da26e9604f0b84207d76e62ac4ba4bc79d1f4665533a7570c3fe0768

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

class Syncano
  module ActiveRecord
    # Module with callbacks functionality for Syncano::ActiveRecord
    module Callbacks
      extend ActiveSupport::Concern

      included do
        # Defines chains for all types of callbacks
        [:validation, :save, :create, :update, :destroy].each do |action|
          [:before, :after].each do |type|
            chain_name = "#{type}_#{action}_callbacks"
            class_attribute chain_name
            send("#{chain_name}=", [])
          end
        end
      end

      # Class methods for Syncano::ActiveRecord::Callbacks module
      module ClassMethods
        private

        [:validation, :save, :create, :update, :destroy].each do |action|
          [:before, :after].each do |type|
            define_method("prepend_#{type}_#{action}") do |argument|
              send("#{type}_#{action}_callbacks").unshift(argument)
            end

            define_method("#{type}_#{action}") do |argument|
              send("#{type}_#{action}_callbacks") << argument
            end
          end
        end
      end

      # Processes callbacks with specified type
      # @param [Symbol, String] type
      def process_callbacks(type)
        if respond_to?("#{type}_callbacks")
          send("#{type}_callbacks").each do |callback_name|
            send(callback_name)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
syncano-3.1.4 lib/syncano/active_record/callbacks.rb
syncano-3.1.3 lib/syncano/active_record/callbacks.rb
syncano-3.1.2 lib/syncano/active_record/callbacks.rb
syncano-3.1.1 lib/syncano/active_record/callbacks.rb