Sha256: e286b7ee1c5262b7040e0a8ea1c7de9d56329b83e0936ad78d4fdc96b8c9eac0

Contents?: true

Size: 1.2 KB

Versions: 44

Compression:

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

44 entries across 44 versions & 3 rubygems

Version Path
activerecord-5.1.3.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.2.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.4 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.4.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.3 lib/active_record/associations/builder/singular_association.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.0 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.0.rc2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.0.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.2 lib/active_record/associations/builder/singular_association.rb
activerecord-5.0.2.rc1 lib/active_record/associations/builder/singular_association.rb
activerecord-5.1.0.beta1 lib/active_record/associations/builder/singular_association.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/associations/builder/singular_association.rb