lib/pickle_dupe/adapter.rb in pickle-dupe-0.2.2 vs lib/pickle_dupe/adapter.rb in pickle-dupe-0.3.0
- old
+ new
@@ -24,16 +24,16 @@
class_handler.site = 'http://test.com'
puts "WARNING: #{model.name} model is defined dynamically because there was a dupe definition but no model was defined"
end
@name = model.name.to_s
end
-
+
def create(attrs = {})
duped_object = ::Dupe.create(@name, attrs)
assign_missing_associations(duped_object, attrs) unless attrs.blank?
-
- return duped_object
+
+ return @klass.send(:find, duped_object.id)
end
private
# Since ActiveResource does not have the notion of relationship between models, when associating
@@ -94,24 +94,27 @@
def assign_association(association_object, duped_object)
has_one_association = duped_object.__model__.name #=> :direction_step
has_many_association = has_one_association.to_s.pluralize.to_sym #=> :direction_steps
# The following would check for the existence of the key :direction_steps
- # in the duped recipe's definition
- if association_object.__model__.schema.attribute_templates.keys.include?(has_many_association)
- association_object[has_many_association] << duped_object
+ # in the duped recipe's definition
+ association_duped_object = ::Dupe.find(association_object.class.name.underscore){|d|
+ d.id == association_object.id
+ }
+ if association_duped_object.__model__.schema.attribute_templates.keys.include?(has_many_association)
+ association_duped_object[has_many_association] << duped_object
else #assume it's a has_one association
- association_object[has_one_association] = duped_object
+ association_duped_object[has_one_association] = duped_object
end
end
# Selecting association objects from the attributes hash and return as array. Association objects are of class
# Dupe::Database::Record
# Given :association => <#Duped::Ingredient name="ingredient 1 name" recipes=[] label="ingredient-1-name" id=1> or
# :association => [<#Duped...>] will return [[<#Duped...>,<#Duped...>],<#Duped...>]
def collect_association_objects(attrs)
attrs.select {|k,v|
- v.kind_of?(Array) || v.kind_of?(::Dupe::Database::Record)
+ v.kind_of?(Array) || v.kind_of?(ActiveResource::Base)
}.collect {|i| i[1]}.flatten
end
end
\ No newline at end of file