Sha256: de82ae76e84b03de59255c3689fc2e5538b257de9737bd0849a64c21a81baf60

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
module Dynamoid #:nodoc:

  # The belongs_to association. For belongs_to, we reference only a single target instead of multiple records; that target is the
  # object to which the association object is associated.
  module Associations
    class BelongsTo
      include SingleAssociation

      def declaration_field_name
        options[:foreign_key] || "#{name}_ids"
      end

      def declaration_field_type
        if options[:foreign_key]
          target_class.attributes[target_class.hash_key][:type]
        else
          :set
        end
      end

      # Override default implementation
      # to handle case when we store id as scalar value, not as collection
      def associate(hash_key)
        target.send(target_association).disassociate(source.hash_key) if target && target_association

        if options[:foreign_key]
          source.update_attribute(source_attribute, hash_key)
        else
          source.update_attribute(source_attribute, Set[hash_key])
        end
      end

      private

      # Find the target association, either has_many or has_one. Uses either options[:inverse_of] or the source class name and default parsing to
      # return the most likely name for the target association.
      #
      # @since 0.2.0
      def target_association
        has_many_key_name = options[:inverse_of] || source.class.to_s.underscore.pluralize.to_sym
        has_one_key_name = options[:inverse_of] || source.class.to_s.underscore.to_sym
        if !target_class.associations[has_many_key_name].nil?
          return has_many_key_name if target_class.associations[has_many_key_name][:type] == :has_many
        end

        if !target_class.associations[has_one_key_name].nil?
          return has_one_key_name if target_class.associations[has_one_key_name][:type] == :has_one
        end
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dynamoid-2.2.0 lib/dynamoid/associations/belongs_to.rb
dynamoid-2.1.0 lib/dynamoid/associations/belongs_to.rb
dynamoid-2.0.0 lib/dynamoid/associations/belongs_to.rb