Sha256: e707d3f6e8e8f962a10f5bca6a2cae0e21eb218c7123da332fd0a9359d1f0e52

Contents?: true

Size: 1.92 KB

Versions: 60

Compression:

Stored size: 1.92 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
      valid += [:as, :foreign_type] if options[:as]
      valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
      valid += [:through, :source, :source_type] if options[:through]
      valid += [:disable_joins] if options[:disable_joins] && options[:through]
      valid
    end

    def self.valid_dependent_options
      [:destroy, :destroy_async, :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(record, name, touch)
      instance = record.send(name)

      if instance&.persisted?
        touch != true ?
          instance.touch(touch) : instance.touch
      end
    end

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

      callback = -> (record) { HasOne.touch_record(record, name, touch) }
      model.after_create callback, if: :saved_changes?
      model.after_create_commit { association(name).reset_negative_cache }
      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

60 entries across 57 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/has_one.rb
activerecord-7.1.5.1 lib/active_record/associations/builder/has_one.rb
activerecord-7.0.8.7 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.5 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.4.2 lib/active_record/associations/builder/has_one.rb
activerecord-7.0.8.6 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.4.1 lib/active_record/associations/builder/has_one.rb
activerecord-7.0.8.5 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.4 lib/active_record/associations/builder/has_one.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/has_one.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/associations/builder/has_one.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/has_one.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/has_one.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/has_one.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/has_one.rb
activerecord-7.1.3.4 lib/active_record/associations/builder/has_one.rb
activerecord-7.0.8.4 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.3.2 lib/active_record/associations/builder/has_one.rb
activerecord-7.1.3.1 lib/active_record/associations/builder/has_one.rb
activerecord-7.0.8.1 lib/active_record/associations/builder/has_one.rb