Sha256: e13db74e02578a068234c078405b4026752b75549224235f9eb1e6a45e0339c1

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

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

2 entries across 2 versions & 1 rubygems

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