Sha256: 9d8bb71f7742f38a80cfa817091372b7802726717596a2dfd6e38dd57a6de68d

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

Contents

# 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
      define_constructors(model.generated_association_methods, reflection.name) if reflection.constructable?
    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

9 entries across 9 versions & 2 rubygems

Version Path
abaci-0.3.0 vendor/bundle/gems/activerecord-5.0.0/lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.rc2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.racecar1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.beta4 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.beta3 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.0.beta2 lib/active_record/associations/builder/singular_association.rb