Class: ForestAdminDatasourceMongoid::Utils::MongoidSerializer
- Inherits:
-
Struct
- Object
- Struct
- ForestAdminDatasourceMongoid::Utils::MongoidSerializer
- Defined in:
- lib/forest_admin_datasource_mongoid/utils/mongoid_serializer.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #each_association_collection(object, projection) ⇒ Object
- #hash_object(object, projection, with_associations: true) ⇒ Object
- #to_hash(projection) ⇒ Object
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object
3 4 5 |
# File 'lib/forest_admin_datasource_mongoid/utils/mongoid_serializer.rb', line 3 def object @object end |
Instance Method Details
#each_association_collection(object, projection) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/forest_admin_datasource_mongoid/utils/mongoid_serializer.rb', line 30 def each_association_collection(object, projection) one_associations = [Mongoid::Association::Referenced::HasOne, Mongoid::Association::Referenced::BelongsTo] object.class.reflect_on_all_associations .filter { |a| one_associations.include?(a.class) && projection.relations.key?(a.name.to_s) } .each { |association| yield(association.name.to_s, object.send(association.name.to_s)) } end |
#hash_object(object, projection, with_associations: true) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/forest_admin_datasource_mongoid/utils/mongoid_serializer.rb', line 8 def hash_object(object, projection, with_associations: true) hash = {} return if object.nil? object.attributes.slice(*projection.columns).each do |key, value| hash[key] = value end if with_associations each_association_collection(object, projection) do |association_name, item| hash[association_name] = hash_object( item, projection.relations[association_name], with_associations: projection.relations.key?(association_name) ) end end hash end |
#to_hash(projection) ⇒ Object
4 5 6 |
# File 'lib/forest_admin_datasource_mongoid/utils/mongoid_serializer.rb', line 4 def to_hash(projection) hash_object(object, projection) end |