Class: ActiveFedora::Associations::Builder::CollectionAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/active_fedora/associations/builder/collection_association.rb

Overview

:nodoc:

Direct Known Subclasses

BasicContains, Filter, HasAndBelongsToMany, HasMany, Orders

Constant Summary collapse

CALLBACKS =
[:before_add, :after_add, :before_remove, :after_remove].freeze

Constants inherited from Association

Association::VALID_OPTIONS

Class Method Summary collapse

Methods inherited from Association

add_destroy_callbacks, better_name, build, build_scope, check_dependent_options, create_reflection, define_accessors, define_readers, define_validations, define_writers, macro, valid_dependent_options, validate_options

Class Method Details

.define_callback(model, callback_name, name, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_fedora/associations/builder/collection_association.rb', line 25

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

.define_callbacks(model, reflection) ⇒ Object



10
11
12
13
14
15
# File 'lib/active_fedora/associations/builder/collection_association.rb', line 10

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

.define_extensions(model, name) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_fedora/associations/builder/collection_association.rb', line 17

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

.valid_options(options) ⇒ Object



6
7
8
# File 'lib/active_fedora/associations/builder/collection_association.rb', line 6

def self.valid_options(options)
  super + CALLBACKS
end

.wrap_scope(scope, mod) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_fedora/associations/builder/collection_association.rb', line 45

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