Sha256: 1c7f09fff99b27bb9598d3cab474fbca9383b6a017dc8dffbae8bdc70fbe0d90

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

module ActiveRecord
  module Associations
    class AssociationCollection

      include Hobo::Scopes::ApplyScopes


      def hobo_association_collection?
        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_association_collection?
        res
      end
      alias_method_chain :build, :reverse_reflection


      def new(attributes = {})
        record = @reflection.klass.new(attributes)
        if hobo_association_collection?
          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 proxy_respond_to_with_automatic_scopes?(method, include_priv = false)
        proxy_respond_to_without_automatic_scopes?(method, include_priv) || create_automatic_scope(method)
      end
      alias_method_chain :proxy_respond_to?, :automatic_scopes


      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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hobo-0.8.1 lib/active_record/association_collection.rb
hobo-0.8.2 lib/active_record/association_collection.rb
hobo-0.8 lib/active_record/association_collection.rb