Sha256: 33d2aa327443e6d23f8505f7e0a11392ed01ca9ee44bf8c102e6ddcb1aa5c104

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

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

  # The BelongsTo association.
  module Associations
    class BelongsTo
      include Dynamoid::Associations::Association
      
      def ==(other)
        target == other
      end
      
      private
      
      def target
        records.first
      end
      
      def target_association
        has_many_key_name = source.class.to_s.pluralize.downcase.to_sym
        has_one_key_name = source.class.to_s.downcase.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
        elsif !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
      
      def associate_target(object)
        object.update_attribute(target_attribute, target_ids.merge(Array(source.id)))
      end
      
      def disassociate_target(object)
        source.update_attribute(source_attribute, target_ids - Array(source.id))
      end
    end
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dynamoid-0.1.1 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.1.0 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.7 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.6 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.5 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.4 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.3 lib/dynamoid/associations/belongs_to.rb
dynamoid-0.0.2 lib/dynamoid/associations/belongs_to.rb