aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/associations/builder/singular_association.rb
blob: 6a1a0073e60d3dfb3e16e79b6328d4a2703b3ddf (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                           
 

                                                  
                                         
                                                              
       
 
                                                
           
                                                                                                            

       
                                                                                          
                                             



                                                      
 


                                                     
 



                                                      
       

     
# This class is inherited by the has_one and belongs_to association classes

module ActiveRecord::Associations::Builder
  class SingularAssociation < Association #:nodoc:
    def self.build_valid_options(options)
      super + [:remote, :dependent, :primary_key, :inverse_of]
    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