aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/associations/builder/has_one.rb
blob: a17cfcb8058a4e77383bdac6e8111907f2af6ac9 (plain) (tree)
1
2
3
4
5
6
7
8
9

                             
                                                    
                                             
                  

              
 
                                   
                           


                                                                     
 
                                    
                                                                                   
       
 
                                                     
                                               
       






                                                                       

                                                                                                                      

     
# frozen_string_literal: true

module ActiveRecord::Associations::Builder # :nodoc:
  class HasOne < SingularAssociation #:nodoc:
    def self.macro
      :has_one
    end

    def self.valid_options(options)
      valid = super + [:as]
      valid += [:through, :source, :source_type] if options[:through]
      valid
    end

    def self.valid_dependent_options
      [:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
    end

    def self.add_destroy_callbacks(model, reflection)
      super unless reflection.options[:through]
    end

    def self.define_validations(model, reflection)
      super
      if reflection.options[:required]
        model.validates_presence_of reflection.name, message: :required
      end
    end

    private_class_method :macro, :valid_options, :valid_dependent_options, :add_destroy_callbacks, :define_validations
  end
end