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.8.6 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.8.5 lib/active_record/associations/builder/singular_association.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/associations/builder/singular_association.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/singular_association.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/singular_association.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.8.4 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.8.1 lib/active_record/associations/builder/singular_association.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/builder/singular_association.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.8 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.7.2 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.7.1 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.7 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.6 lib/active_record/associations/builder/singular_association.rb
activerecord-7.0.5.1 lib/active_record/associations/builder/singular_association.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/associations/builder/singular_association.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/associations/builder/singular_association.rb
rubypitaya-3.12.5 ./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.5 lib/active_record/associations/builder/singular_association.rb