Sha256: f93d9ec511d127e54ff8e7f7cdf2867d101419b79ffdc4b4d4b84331c3678b3b

Contents?: true

Size: 1.68 KB

Versions: 36

Compression:

Stored size: 1.68 KB

Contents

# 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, :touch]
      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.define_callbacks(model, reflection)
      super
      add_touch_callbacks(model, reflection) if reflection.options[:touch]
    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

    def self.touch_record(o, name, touch)
      record = o.send name

      return unless record && record.persisted?

      if touch != true
        record.touch(touch)
      else
        record.touch
      end
    end

    def self.add_touch_callbacks(model, reflection)
      name  = reflection.name
      touch = reflection.options[:touch]

      callback = lambda { |record|
        HasOne.touch_record(record, name, touch)
      }

      model.after_create callback, if: :saved_changes?
      model.after_update callback, if: :saved_changes?
      model.after_destroy callback
      model.after_touch callback
    end

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

Version data entries

36 entries across 36 versions & 4 rubygems

Version Path
activerecord-6.0.6.1 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.6 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.5.1 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.5 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.8 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.7 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.6 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.5 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.4 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.3 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.2 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4.1 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.4 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.7 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.6 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.5 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.4 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.3 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.2 lib/active_record/associations/builder/has_one.rb
activerecord-6.0.3.1 lib/active_record/associations/builder/has_one.rb