Sha256: 63b1739f8d77b39f44508b4b5579817ab1351bd07c5e1d0a48fc5602e48c4795

Contents?: true

Size: 1.35 KB

Versions: 17

Compression:

Stored size: 1.35 KB

Contents

module CouchFoo
  module Associations
    class BelongsToPolymorphicAssociation < AssociationProxy #:nodoc:
      def replace(record)
        if record.nil?
          @target = @owner[@reflection.primary_key_name] = @owner[@reflection.options[:foreign_type]] = nil
        else
          @target = (AssociationProxy === record ? record.target : record)

          @owner[@reflection.primary_key_name] = record.id
          @owner[@reflection.options[:foreign_type]] = record.class.name.to_s

          @updated = true
        end

        loaded
        record
      end

      def updated?
        @updated
      end

      private
        def find_target
          return nil if association_class.nil?

          if @reflection.options[:conditions]
            association_class.find(
              @owner[@reflection.primary_key_name],
              :conditions => conditions,
              :include    => @reflection.options[:include]
            )
          else
            association_class.find(@owner[@reflection.primary_key_name], :include => @reflection.options[:include])
          end
        end

        def foreign_key_present
          !@owner[@reflection.primary_key_name].nil?
        end

        def association_class
          @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
        end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
georgepalmer-couch_foo-0.7.1 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.10 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.11 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.12 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.13 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.14 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.15 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.2 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.3 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.4 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.7 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.8 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.7.9 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.8.0 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.8.1 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.8.2 lib/couch_foo/associations/belongs_to_polymorphic_association.rb
georgepalmer-couch_foo-0.8.3 lib/couch_foo/associations/belongs_to_polymorphic_association.rb