lib/loaders/loader_base.rb in datashift-0.12.0 vs lib/loaders/loader_base.rb in datashift-0.12.1

- old
+ new

@@ -43,10 +43,11 @@ # # Options # # :reload : Force load of the method dictionary for object_class even if already loaded # :instance_methods : Include setter/delegate style instance methods for assignment, as well as AR columns + # :verbose : Verboise logging and to STDOUT # def initialize(object_class, find_operators = true, object = nil, options = {}) @load_object_class = object_class # Gather names of all possible 'setter' methods on AR class (instance variables and associations) @@ -61,10 +62,12 @@ @method_mapper = DataShift::MethodMapper.new @config = options.dup # clone can cause issues like 'can't modify frozen hash' @verbose = @config[:verbose] + + puts "Verbose Mode" if(verbose) @headers = [] @default_data_objects ||= {} @default_values = {} @@ -405,11 +408,11 @@ end def save return unless( @load_object ) - #puts "DEBUG: SAVING #{@load_object.class} : #{@load_object.inspect}" if(@verbose) + puts "DEBUG: SAVING #{@load_object.class} : #{@load_object.inspect}" if(verbose) begin return @load_object.save rescue => e failure puts "Error saving #{@load_object.class} : #{e.inspect}" @@ -513,12 +516,22 @@ current_value.to_s.split( Delimiters::multi_assoc_delim ) end private + # This method usually called during processing to avoid errors with associations like + # <ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved> + # If the object is still invalid at this point probably indicates compulsory + # columns on model have not been processed before associations on that model + # TODO smart ordering of columns dynamically ourselves rather than relying on incoming data order def save_if_new - #puts "SAVE", load_object.inspect - save if(load_object.valid? && load_object.new_record?) + return unless(load_object.new_record?) + + if(load_object.valid?) + save + else + puts "Cannot Save - Invalid #{load_object.class} - #{load_object.errors.full_messages}" if(verbose) + end end end end \ No newline at end of file