Class: Multiinsert::SaveRecords

Inherits:
Object
  • Object
show all
Defined in:
lib/multiinsert.rb

Instance Method Summary collapse

Instance Method Details

#instantiate_model(model) ⇒ Object



31
32
33
# File 'lib/multiinsert.rb', line 31

def instantiate_model model
  model.camelize.constantize.new
end

#save(params, name_of_model, mode) ⇒ Object

prepend the name of the model with a hyphen and append the id with a hyphen like book-author-1



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/multiinsert.rb', line 8

def save(params,name_of_model,mode)
  model_records = params.select{|param| param.match(/^#{name_of_model}/)}
  
    value_ids = model_records.keys.collect{|rec| rec.split("-")[2]}.uniq!
    @mod = instantiate_model(name_of_model)
    @saved = false
    
      value_ids.each do |item_id|
        @mod = @mod.class.find(item_id) if mode == "update"
        model_records.each do |the_model|
          if the_model.first.split("-")[2] == item_id
            @mod[the_model.first.split("-")[1]] = the_model.last.split.join
          else
            break if !@saved
          end        
        end
      saved = @mod.save
      @mod = instantiate_model(name_of_model)
      @saved = true
      saved
    end
end