Sha256: 4e3bd6e09530b8c5a184b60c0eb10abd2b40453a75568cb86f027a47230b4301

Contents?: true

Size: 1.43 KB

Versions: 37

Compression:

Stored size: 1.43 KB

Contents

def parse_save_from_excel
    test_file = params[:excel_file]
    file = FileUploader.new
    if file.store!(test_file)
      book = Spreadsheet.open "#{file.store_path}"
      sheet1 = book.worksheet 0

       @<%= instance_name %>  = []
      @errors = Hash.new
      @counter = 0

      sheet1.each 1 do |row|
        @counter+=1
        <%- final_query="find_by_" -%>
        <%- final_arguments="" -%>
        <%- a="_and_" -%>
        p = <%= class_name %>.new
        <%- model_attributes.each_with_index do |attribute,index| -%>
        <%- final_query=final_query+ attribute.name %>
        <%- final_arguments=final_arguments+"p."+attribute.name -%>
        <%-if index < model_attributes.length-1 -%>
        <%- final_query=final_query+ a %>
        <%- final_arguments=final_arguments+ "," %>
        <%- end -%>
         p.<%=attribute.name %> = row[<%=index%>]
        <%- end -%>
        unless <%= class_name %>.<%=final_query%>(<%= final_arguments %>)
          if p.valid?
            p.save
          else
            @errors["#{@counter+1}"] = p.errors
          end
        end
      end
      file.remove!
      if @errors.empty?
        redirect_to <%= item_path %>, notice: 'All Dummy data were successfully uploaded.'
      else
        redirect_to <%= item_path %>, notice: 'There were some errors in your upload'
      end

    else
      redirect_to <%= item_path %>, notice: 'Dummy datum could not be successfully uploaded.'
    end
  end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
thesilverspoon-1.0.2 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-1.0.1 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-1.0.0 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.35 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.34 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.33 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.32 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.31 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.30 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.29 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.28 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.27 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.26 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.25 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.24 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.23 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.22 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.21 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.20 lib/templates/actions/parse_save_from_excel.rb
thesilverspoon-0.0.19 lib/templates/actions/parse_save_from_excel.rb