Sha256: d42fde1928913359c5a7c2c8b7fb2ded67cc93da425097c924775f96c65af7f3

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8
require "google/cloud"
require "gdatastore_mapper/relation"
require "gdatastore_mapper/associations/has_many"

module GdatastoreMapper
  module Associations
    extend ActiveSupport::Concern

    def self.included(klass)
      klass.include Associations
    end

    def has_many models
      self.class_eval("attr_accessor :#{models.to_s + '_id'}")

      define_method models do
        association = GdatastoreMapper::Associations::HasMany.new(self, models)
        belongings = GdatastoreMapper::Relation.new(self, association)
        if self.attributes.include?(id_ models) && !self.send(id_ models).nil?
          self.send(id_ models).each do |id|
            belongings << (models.to_s.classify.constantize.find id)
          end
        end
        belongings
      end
    end

    def belongs_to model
      self.class_eval("attr_accessor :#{model.to_s + '_id'}")

      define_method model do
        if self.attributes.include?(id_ model)
          model.to_s.classify.constantize.find self.send(id_ model)
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.2 lib/gdatastore_mapper/associations.rb
gdatastore_mapper-0.1.2bata lib/gdatastore_mapper/associations.rb