Sha256: db482d09da0ec4c37ff72d49c930cbb92dae460c69fbf37dac4800495a4efae9

Contents?: true

Size: 1.31 KB

Versions: 36

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

# This class is inherited by the has_one and belongs_to association classes

module ActiveRecord::Associations::Builder # :nodoc:
  class SingularAssociation < Association #:nodoc:
    def self.valid_options(options)
      super + [:foreign_type, :dependent, :primary_key, :inverse_of, :required]
    end

    def self.define_accessors(model, reflection)
      super
      mixin = model.generated_association_methods
      name = reflection.name

      define_constructors(mixin, name) if reflection.constructable?

      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def reload_#{name}
          association(:#{name}).force_reload_reader
        end
      CODE
    end

    # Defines the (build|create)_association methods for belongs_to or has_one association
    def self.define_constructors(mixin, name)
      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def build_#{name}(*args, &block)
          association(:#{name}).build(*args, &block)
        end

        def create_#{name}(*args, &block)
          association(:#{name}).create(*args, &block)
        end

        def create_#{name}!(*args, &block)
          association(:#{name}).create!(*args, &block)
        end
      CODE
    end

    private_class_method :valid_options, :define_accessors, :define_constructors
  end
end

Version data entries

36 entries across 36 versions & 4 rubygems

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