Sha256: bff1173f664d9ababe9eda81262f97afc66793e44a1c54b645b9fd2d4f8e6f54

Contents?: true

Size: 1.27 KB

Versions: 41

Compression:

Stored size: 1.27 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 + [:required, :touch]
    end

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

      define_constructors(mixin, name) unless reflection.polymorphic?

      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

41 entries across 39 versions & 5 rubygems

Version Path
activerecord-7.0.4.3 lib/active_record/associations/builder/singular_association.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.4.2 lib/active_record/associations/builder/singular_association.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.4.1 lib/active_record/associations/builder/singular_association.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.4 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.3.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.3 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.2.4 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.2.3 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.2.2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.2.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.0 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.0.rc3 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.0.rc2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.0.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.0.alpha2 lib/active_record/associations/builder/singular_association.rb