Sha256: 2c8de8ae4921f6d0738ec79db97f1a45bfc6152b901ab8e88aaafca5496f17be

Contents?: true

Size: 1.89 KB

Versions: 18

Compression:

Stored size: 1.89 KB

Contents

require 'active_fedora/associations'
module ActiveFedora::Associations::Builder
  class CollectionAssociation < Association #:nodoc:
    CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove].freeze

    def self.valid_options(options)
      super + CALLBACKS
    end

    def self.define_callbacks(model, reflection)
      super
      name = reflection.name
      options = reflection.options
      CALLBACKS.each { |callback_name| define_callback(model, callback_name, name, options) }
    end

    def self.define_extensions(model, name)
      if block_given?
        extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
        extension = Module.new(&Proc.new)
        model.parent.const_set(extension_module_name, extension)
      end
    end

    def self.define_callback(model, callback_name, name, options)
      full_callback_name = "#{callback_name}_for_#{name}"

      # TODO : why do i need method_defined? I think its because of the inheritance chain
      # See https://github.com/samvera/active_fedora/issues/1333
      model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)

      callbacks = Array(options[callback_name.to_sym]).map do |callback|
        case callback
        when Symbol
          ->(_method, owner, record) { owner.send(callback, record) }
        when Proc
          ->(_method, owner, record) { callback.call(owner, record) }
        else
          ->(method, owner, record) { callback.send(method, owner, record) }
        end
      end
      model.send("#{full_callback_name}=", callbacks)
    end

    def self.wrap_scope(scope, mod)
      if scope
        if scope.arity > 0
          proc { |owner| instance_exec(owner, &scope).extending(mod) }
        else
          proc { instance_exec(&scope).extending(mod) }
        end
      else
        proc { extending(mod) }
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
active-fedora-13.3.0 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.2.4 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.7 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.5 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.4 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.2.3 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.3 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.2 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.2.0 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.1.3 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.2.2 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.2.1 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.1.2 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.1.1 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.1.0 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-13.0.0 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.1.1 lib/active_fedora/associations/builder/collection_association.rb
active-fedora-12.1.0 lib/active_fedora/associations/builder/collection_association.rb