lib/machinist.rb in machinist-1.0.4 vs lib/machinist.rb in machinist-1.0.5

- old
+ new

@@ -36,11 +36,11 @@ def method_missing(symbol, *args, &block) if attribute_assigned?(symbol) # If we've already assigned the attribute, return that. @object.send(symbol) - elsif @adapter.has_association?(@object, symbol) && !@object.send(symbol).nil? + elsif @adapter.has_association?(@object, symbol) && !nil_or_empty?(@object.send(symbol)) # If the attribute is an association and is already assigned, return that. @object.send(symbol) else # Otherwise generate a value and assign it. assign_attribute(symbol, generate_attribute_value(symbol, *args, &block)) @@ -55,9 +55,13 @@ # (Both of these are deprecated in Ruby 1.8 anyway.) undef_method :id if respond_to?(:id) undef_method :type if respond_to?(:type) private + + def nil_or_empty?(object) + object.respond_to?(:empty?) ? object.empty? : object.nil? + end def assign_attribute(key, value) assigned_attributes[key.to_sym] = value @object.send("#{key}=", value) end