lib/dejavu.rb in dejavu-0.2.1 vs lib/dejavu.rb in dejavu-0.3.0
- old
+ new
@@ -37,17 +37,24 @@
end
end
module ControllerMethods
def save_for_dejavu(obj, opts = {})
- attrs = obj.attributes
+ attrs = if opts[:only] && opts[:only].is_a?(Array)
+ obj.attributes.slice(*opts[:only].map(&:to_s))
+ else
+ obj.attributes
+ end
- missing_keys = obj.errors.keys.map(&:to_sym).select{|x| obj.respond_to?(x)} - attrs.keys.map(&:to_sym)
+ missing_keys = []
if keys = opts[:nested]
- keys = [keys] unless keys.is_a? Array
+ keys = [keys].flatten
keys.each { |key| attrs = save_nested_for_dejavu(obj, key, attrs) }
- missing_keys -= keys.map(&:to_sym)
+ end
+
+ if virtual = opts[:virtual]
+ missing_keys += [virtual].flatten
end
missing_keys.each do |key|
attrs[key] = obj.send(key)
end