Sha256: 9ca4d11e77b24202f5880368d2e31885531f8d365b96960f8a005524f04fda52

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module ActiveRecord::Associations

  class HasManyAssociation
    
    def hobo_has_many?
      Hobo::Model.in?(@owner.class.included_modules)
    end

    def build_with_reverse_reflection(*args)
      res = build_without_reverse_reflection(*args)
      set_reverse_association(res) if hobo_has_many?
      res
    end
    alias_method_chain :build, :reverse_reflection
    

    def new(attributes = {})
      record = @reflection.klass.new(attributes)
      if hobo_has_many?
        set_belongs_to_association_for(record)
        set_reverse_association(record) unless proxy_reflection.options[:as]
      end
      record
    end
    
    
    def member_class
      proxy_reflection.klass
    end
    
    def origin
      proxy_owner
    end

    def origin_attribute
      proxy_reflection.association_name
    end
    
    private
    
    def set_reverse_association(object)
      if @owner.new_record? && (refl = @owner.class.reverse_reflection(@reflection.name))
        bta = ActiveRecord::Associations::BelongsToAssociation.new(object, refl)
        bta.replace(@owner)
        object.instance_variable_set("@#{refl.name}", bta)
      end      
    end
    
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-0.7.3 hobo_files/plugin/lib/active_record/has_many_association.rb