lib/snapshotar/core.rb in snapshotar-0.0.1 vs lib/snapshotar/core.rb in snapshotar-0.0.2
- old
+ new
@@ -41,14 +41,17 @@
json.set! model_name do
json.array! m.first.all do |itm|
m[1..-1].each do |attr|
next unless itm.respond_to?(attr.to_sym)
+ next if itm[attr].nil?
# replace uploads by their url
if itm.send(attr.to_sym).respond_to?(:url)
json.set! "#{attr}_url".to_sym, itm.send(attr.to_sym).url
+ elsif itm.send(attr.to_sym).is_a? ::BSON::ObjectId
+ json.set! attr.to_sym, itm[attr].to_s
else
json.set! attr.to_sym, itm[attr]
end
end
end
@@ -73,18 +76,25 @@
clazz = key.constantize
value.each do |itm|
item_params = {}
itm.each do |itm_key,itm_value|
+
+ next if itm_value.nil?
+
# handle url paths separatley
if itm_key.to_s.end_with?("_url")
- orig_key = itm_key.to_s[0..-5].to_sym
- item_params[orig_key] = File.open(itm_value)
+ if File.exist?(itm_value)
+ orig_key = itm_key.to_s[0..-5].to_sym
+ item_params[orig_key] = File.open(itm_value)
+ else # remote
+ item_params["remote_#{itm_key}"] = itm_value
+ end
else
item_params[itm_key] = itm_value
end
end
- clazz.create(item_params)
+ clazz.new(item_params).save(validate: false)
end
end
end
##