Sha256: 7efe75594e2fd17487bbe54623b35009c5de04f7122e2a8c612d9b6fec204ced

Contents?: true

Size: 1.56 KB

Versions: 23

Compression:

Stored size: 1.56 KB

Contents

module ActiveRecord
  module Associations
    class AssociationCollection

      include Hobo::Model::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

      # DO NOT call super here - AssociationProxy's version loads the collection, and that's bad.
      # TODO: this really belongs in Rails; migrate it there ASAP
      def respond_to?(*args)
        proxy_respond_to?(*args) || Array.new.respond_to?(*args)
      end

      # TODO: send this patch into Rails. There's no reason to load the collection just to find out it acts like an array.
      def is_a?(klass)
        [].is_a?(klass)
      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

23 entries across 23 versions & 1 rubygems

Version Path
hobo-1.3.0.RC2 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.RC1 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.RC lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre31 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre29 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre28 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre27 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre26 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre25 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre24 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre23 lib/hobo/extensions/active_record/associations/collection.rb
hobo-1.3.0.pre22 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre21 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre20 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre19 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre18 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre16 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre15 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre14 lib/hobo/extensions/active_record/association_collection.rb
hobo-1.3.0.pre13 lib/hobo/extensions/active_record/association_collection.rb