Thursday, 26 December 2019

Importing large data from Xls sheet with background job in rails

In controller need to add the import method with background job.

def import  
new_filename = "#{File.basename(params[:file].original_filename)}_new"
new_filepath = File.join(Dir.tmpdir, new_filename)
FileUtils.cp(params[:file].path, new_filepath)
BackJob.perform_later(new_filepath, 
File.extname(params[:file].original_filename))
redirect_to root_url, notice: 'It take sometime to import'
rescue StandardError => e  puts e.inspect
end

In view index.html.erb add the import button with files.

<%= form_tag import_lists_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>


In BackJob.rb add the import method for storing the data from xls

class BackJob < ApplicationJob
queue_as :default

def perform(fpath, file)
import(fpath, file)
end

def import(filepath, extname)
   spreadsheet = open_spreadsheet(filepath, extname)
    columns = %w[column_one column_tow ]
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[db_columns, spreadsheet.row(i)].transpose]
      obj = ModelName.new
      obj.attributes = row.to_hash.slice(*columns)
      obj.save
   end
end


def open_spreadsheet(filepath, extname)
case extname    
when '.csv'      Roo::Csv.new(filepath)
when '.xls'      Roo::Excel.new(filepath)
when '.xlsx'      Roo::Excelx.new(filepath, file_warning: :ignore)
else raise 'Unknown file type: extname'
end
end
end

Monday, 16 December 2019

How to sort the array of string by alphabetical order with domain based components in ruby?


Input:

array = ["analytics.ramo.com", "ccc.ramo.com", "ccc.test.ramo", "ddd.kumar.ramo.com", "ec2.new.ramo.com", "ramo.com", "test-new.ramo.com", "top-test.ramo.com"] 

Solution:

array.sort_by do |domain|
  parts = domain.split('.').reverse
  parts.unshift(parts.count)
end


Output:

["ramo.com",
 "analytics.ramo.com",
 "ccc.ramo.com",
 "test-new.ramo.com",
 "top-test.ramo.com",
 "ccc.test.ramo",
 "ddd.kumar.ramo.com",
 "ec2.new.ramo.com"]

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...