Sha256: 53bd6d37e2583c4078380fe34a8ca93c07cc96145db38b7437aab26bb282c751

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

module ActiveFedora::Associations::Builder
  class BelongsTo < SingularAssociation # :nodoc:
    def self.macro
      :belongs_to
    end

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

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

    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

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

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

      super

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active-fedora-15.0.1 lib/active_fedora/associations/builder/belongs_to.rb
active-fedora-15.0.0 lib/active_fedora/associations/builder/belongs_to.rb
active-fedora-14.0.1 lib/active_fedora/associations/builder/belongs_to.rb
active-fedora-14.0.0 lib/active_fedora/associations/builder/belongs_to.rb