Sha256: 38f06e8a3e53e4367e88d9b974e231716052a1a43d9a4a197ddd9596892f8994

Contents?: true

Size: 1.23 KB

Versions: 37

Compression:

Stored size: 1.23 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
  end
end

Version data entries

37 entries across 37 versions & 4 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.8 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.7.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.7 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.6.3 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.6.2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.6.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.6 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.6 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.5 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.5 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.4 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.3 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4 lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.4.rc1 lib/active_record/associations/builder/singular_association.rb
spiral_form-0.1.1 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/associations/builder/singular_association.rb
spiral_form-0.1.0 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/associations/builder/singular_association.rb
activerecord-5.2.3 lib/active_record/associations/builder/singular_association.rb