lib/relaxo/model/properties/composite.rb in relaxo-model-0.5.1 vs lib/relaxo/model/properties/composite.rb in relaxo-model-0.9.0
- old
+ new
@@ -42,31 +42,22 @@
end
@lookup[type]
end
- def convert_to_primative(object)
- unless object.saved?
- object.save
- end
+ def convert_to_primative(document)
+ raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
- [object.type, object.id]
+ document.paths.first
end
- def convert_from_primative(database, reference)
- # Legacy support for old polymorphic types - to remove.
- if Array === reference
- type, id = reference
- else
- id = reference
- end
+ def convert_from_primative(dataset, path)
+ type, _, _ = path.rpartition('/')
- attributes = database.get(id).to_hash
+ klass = lookup(type)
- klass = lookup(attributes['type'])
-
- klass.fetch(database, attributes)
+ klass.fetch(dataset, path)
end
end
class BelongsTo
def self.[] *klasses
@@ -79,43 +70,41 @@
def initialize(klass)
@klass = klass
end
- def convert_to_primative(object)
- unless object.saved?
- object.save
- end
-
- object.id
+ def convert_to_primative(document)
+ raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
+
+ document.paths.first
end
- def convert_from_primative(database, id)
- @klass.fetch(database, id)
+ def convert_from_primative(dataset, path)
+ @klass.fetch(dataset, path)
end
end
-
+
class HasOne < BelongsTo
end
class HasMany < HasOne
- def convert_to_primative(value)
- value.each do |document|
- document.save unless document.saved?
+ def convert_to_primative(documents)
+ documents.each do |document|
+ raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
end
-
- value.collect{|document| document.id}
+
+ documents.collect{|document| document.paths.first}
end
- def convert_from_primative(database, value)
- value.collect{|id| @klass.fetch(database, id)}
+ def convert_from_primative(dataset, value)
+ value.collect{|id| @klass.fetch(dataset, id)}
end
end
# Returns the raw value, typically used for reductions:
module ValueOf
- def self.new(database, value)
+ def self.new(dataset, value)
value
end
end
class ArrayOf
@@ -131,12 +120,12 @@
value.collect do |item|
@klass.convert_to_primative(item)
end
end
- def convert_from_primative(database, value)
+ def convert_from_primative(dataset, value)
value.collect do |item|
- @klass.convert_from_primative(database, item)
+ @klass.convert_from_primative(dataset, item)
end
end
end
end
end