Module: EmbedUtils
- Defined in:
- lib/mongoid_fixtures/embed_utils.rb
Class Method Summary collapse
- .create_embedded_instance(clazz, hash, instance) ⇒ Object
- .find_embed_parent_class(embed) ⇒ Object
- .insert_embedded_ids(instance) ⇒ Object
Class Method Details
.create_embedded_instance(clazz, hash, instance) ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/mongoid_fixtures/embed_utils.rb', line 2 def self.(clazz, hash, instance) = clazz.new hash.each do |key, value| .send("#{key}=", value) end .send("#{find_embed_parent_class(embed)}=", instance) end |
.find_embed_parent_class(embed) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mongoid_fixtures/embed_utils.rb', line 11 def self.() relations = .relations relations.each do |name, relation| if relation.relation.eql? Mongoid::Relations::Embedded::In return name end end raise 'Unable to find parent class' end |
.insert_embedded_ids(instance) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mongoid_fixtures/embed_utils.rb', line 22 def self.(instance) attributes = instance.attributes.select { |key, value| !key.to_s.eql?('_id') } attributes.each do |key, value| if attributes[key].is_a? Hash unless instance.send(key)._id.nil? attributes[key]['_id'] = instance.send(key)._id end else attributes[key] = value end end attributes end |