Module: GdatastoreMapper::Associations

Extended by:
ActiveSupport::Concern
Defined in:
lib/gdatastore_mapper/associations.rb,
lib/gdatastore_mapper/associations/has_many.rb

Defined Under Namespace

Classes: HasMany

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
# File 'lib/gdatastore_mapper/associations.rb', line 9

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

Instance Method Details

#belongs_to(model) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/gdatastore_mapper/associations.rb', line 28

def belongs_to model
  add_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

#belongs_to_modelsObject



39
40
41
# File 'lib/gdatastore_mapper/associations.rb', line 39

def belongs_to_models
  @belongs_to_models
end

#has_many(models) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gdatastore_mapper/associations.rb', line 13

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