Sha256: 1c82c4fc4f087d25a00788db3704af86771d55985babae4a19ba96a05d236319

Contents?: true

Size: 1.34 KB

Versions: 41

Compression:

Stored size: 1.34 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

        def reset_#{name}
          association(:#{name}).reset
        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 41 versions & 5 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/singular_association.rb
activerecord-8.0.1 lib/active_record/associations/builder/singular_association.rb
activerecord-8.0.0.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.2.2.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.1.5.1 lib/active_record/associations/builder/singular_association.rb
activerecord-8.0.0 lib/active_record/associations/builder/singular_association.rb
activerecord-7.2.2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.1.5 lib/active_record/associations/builder/singular_association.rb
activerecord-8.0.0.rc2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.2.1.2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.1.4.2 lib/active_record/associations/builder/singular_association.rb
activerecord-8.0.0.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.2.1.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.1.4.1 lib/active_record/associations/builder/singular_association.rb
activerecord-8.0.0.beta1 lib/active_record/associations/builder/singular_association.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/associations/builder/singular_association.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/associations/builder/singular_association.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/associations/builder/singular_association.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/associations/builder/singular_association.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/associations/builder/singular_association.rb