Class: ActiveFedora::Associations::Builder::Association

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

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Constant Summary collapse

VALID_OPTIONS =
[:class_name, :predicate, :type_validator].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.extensionsObject

Returns the value of attribute extensions.



4
5
6
# File 'lib/active_fedora/associations/builder/association.rb', line 4

def extensions
  @extensions
end

Class Method Details

.add_destroy_callbacks(model, reflection) ⇒ Object



139
140
141
142
# File 'lib/active_fedora/associations/builder/association.rb', line 139

def self.add_destroy_callbacks(model, reflection)
  name = reflection.name
  model.before_destroy ->(o) { o.association(name).handle_dependency }
end

.better_name(name) ⇒ Object



66
67
68
# File 'lib/active_fedora/associations/builder/association.rb', line 66

def self.better_name(name)
  name
end

.build(model, name, options, &block) ⇒ Object

configure_dependency



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_fedora/associations/builder/association.rb', line 11

def self.build(model, name, options, &block)
  if model.dangerous_attribute_method?(name)
    Deprecation.warn(ActiveFedora::Base, "You tried to define an association named #{name} on the model #{model.name}, but " \
                         "this will conflict with a method #{name} already defined by ActiveFedora. " \
                         "Please choose a different association name.")
  end

  extension = define_extensions model, name, &block
  reflection = create_reflection model, name, nil, options, extension
  define_accessors(model, reflection)
  define_callbacks(model, reflection)
  define_validations model, reflection
  reflection
end

.build_scope(scope, extension) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/active_fedora/associations/builder/association.rb', line 40

def self.build_scope(scope, extension)
  new_scope = scope

  new_scope = proc { instance_exec(&scope) } if scope && scope.arity == 0

  new_scope = wrap_scope new_scope, extension if extension

  new_scope
end

.check_dependent_options(dependent) ⇒ Object



133
134
135
136
137
# File 'lib/active_fedora/associations/builder/association.rb', line 133

def self.check_dependent_options(dependent)
  unless valid_dependent_options.include? dependent
    raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{dependent}"
  end
end

.create_reflection(model, name, scope, options, extension = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_fedora/associations/builder/association.rb', line 26

def self.create_reflection(model, name, scope, options, extension = nil)
  unless name.is_a?(Symbol)
    name = name.to_sym
    Deprecation.warn(ActiveFedora::Base, "association names must be a Symbol")
  end
  validate_options(options)
  translate_property_to_predicate(options)

  scope = build_scope(scope, extension)
  name = better_name(name)

  ActiveFedora::Reflection.create(macro, name, scope, options, model)
end

.define_accessors(model, reflection) ⇒ Object

Defines the setter and getter methods for the association class Post < ActiveRecord::Base

has_many :comments

end

Post.first.comments and Post.first.comments= methods are defined by this method…



102
103
104
105
106
107
# File 'lib/active_fedora/associations/builder/association.rb', line 102

def self.define_accessors(model, reflection)
  mixin = model.generated_association_methods
  name = reflection.name
  define_readers(mixin, name)
  define_writers(mixin, name)
end

.define_callbacks(model, reflection) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/active_fedora/associations/builder/association.rb', line 85

def self.define_callbacks(model, reflection)
  if dependent = reflection.options[:dependent]
    check_dependent_options(dependent)
    add_destroy_callbacks(model, reflection)
  end

  Association.extensions.each do |extension|
    extension.build model, reflection
  end
end

.define_extensions(_model, _name) ⇒ Object



82
83
# File 'lib/active_fedora/associations/builder/association.rb', line 82

def self.define_extensions(_model, _name)
end

.define_readers(mixin, name) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/active_fedora/associations/builder/association.rb', line 109

def self.define_readers(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}(*args)
      association(:#{name}).reader(*args)
    end
  CODE
end

.define_validations(_model, _reflection) ⇒ Object



125
126
127
# File 'lib/active_fedora/associations/builder/association.rb', line 125

def self.define_validations(_model, _reflection)
  # noop
end

.define_writers(mixin, name) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/active_fedora/associations/builder/association.rb', line 117

def self.define_writers(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      association(:#{name}).writer(value)
    end
  CODE
end

.macroObject

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/active_fedora/associations/builder/association.rb', line 54

def self.macro
  raise NotImplementedError
end

.predicate(property) ⇒ Object

Returns the RDF predicate as defined by the :property attribute



77
78
79
80
# File 'lib/active_fedora/associations/builder/association.rb', line 77

def self.predicate(property)
  return property if property.is_a? RDF::URI
  ActiveFedora::Predicates.find_graph_predicate(property)
end

.translate_property_to_predicate(options) ⇒ Object



70
71
72
73
74
# File 'lib/active_fedora/associations/builder/association.rb', line 70

def self.translate_property_to_predicate(options)
  return unless options[:property]
  Deprecation.warn Association, "the :property option to `#{model}.#{macro} :#{name}' is deprecated and will be removed in active-fedora 10.0. Use :predicate instead", caller(5)
  options[:predicate] = predicate(options.delete(:property))
end

.valid_dependent_optionsObject

Raises:

  • (NotImplementedError)


129
130
131
# File 'lib/active_fedora/associations/builder/association.rb', line 129

def self.valid_dependent_options
  raise NotImplementedError
end

.valid_options(_options) ⇒ Object



58
59
60
# File 'lib/active_fedora/associations/builder/association.rb', line 58

def self.valid_options(_options)
  VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
end

.validate_options(options) ⇒ Object



62
63
64
# File 'lib/active_fedora/associations/builder/association.rb', line 62

def self.validate_options(options)
  options.assert_valid_keys(valid_options(options))
end

.wrap_scope(scope, _extension) ⇒ Object



50
51
52
# File 'lib/active_fedora/associations/builder/association.rb', line 50

def self.wrap_scope(scope, _extension)
  scope
end