Sha256: 22dd20489af28a3290d4bb13c83393d16a76d555764032467fd92793a8e2970b

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 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 new_candidate(attributes = {})
        record = new
        @target.delete record
        set_reverse_association(record) if hobo_association_collection?
        record
      end


      def user_new_candidate(user, attributes = {})
        record = user_new(user, attributes)
        @target.delete record
        set_reverse_association(record) if hobo_association_collection?
        record
      end

      def member_class
        proxy_reflection.klass
      end

      private

        def set_reverse_association(object)
          if @owner.new_record? &&
              (refl = @owner.class.reverse_reflection(@reflection.name)) &&
              refl.macro == :belongs_to
            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

1 entries across 1 versions & 1 rubygems

Version Path
hobo-1.1.0.pre3 lib/active_record/association_collection.rb