Class: ActiveFedora::Associations::Builder::BelongsTo

Inherits:
SingularAssociation show all
Defined in:
lib/active_fedora/associations/builder/belongs_to.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Association

Association::VALID_OPTIONS

Class Method Summary collapse

Methods inherited from SingularAssociation

define_accessors, define_constructors

Methods inherited from Association

add_destroy_callbacks, better_name, build, build_scope, check_dependent_options, create_reflection, define_accessors, define_callbacks, define_extensions, define_readers, define_writers, wrap_scope

Class Method Details

.define_validations(model, reflection) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_fedora/associations/builder/belongs_to.rb', line 21

def self.define_validations(model, reflection)
  if reflection.options.key?(:required)
    reflection.options[:optional] = !reflection.options.delete(:required)
  end

  required = if reflection.options[:optional].nil?
               model.belongs_to_required_by_default
             else
               !reflection.options[:optional]
             end

  super

  if required
    model.validates_presence_of reflection.name, message: :required
  end
end

.macroObject



3
4
5
# File 'lib/active_fedora/associations/builder/belongs_to.rb', line 3

def self.macro
  :belongs_to
end

.valid_dependent_optionsObject



11
12
13
# File 'lib/active_fedora/associations/builder/belongs_to.rb', line 11

def self.valid_dependent_options
  [:destroy, :delete]
end

.valid_options(options) ⇒ Object



7
8
9
# File 'lib/active_fedora/associations/builder/belongs_to.rb', line 7

def self.valid_options(options)
  super + [:optional]
end

.validate_options(options) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/active_fedora/associations/builder/belongs_to.rb', line 15

def self.validate_options(options)
  super
  raise "You must specify a predicate for #{name}" unless options[:predicate]
  raise ArgumentError, "Predicate must be a kind of RDF::URI" unless options[:predicate].is_a?(RDF::URI)
end