Friday, 3 April 2020

Interactor in Rails

What is interactor?


Interactor provides a common interface for performing complex user interactions

An interactor is a simple, single-purpose object.

Interactors are used to encapsulate your application's business logic. Each interactor represents one thing that your application does.

Context

An interactor is given a context. The context contains everything the interactor needs to do its work.

When an interactor does its single purpose, it affects its given context.
Adding context

context.user = user


Example of interactor

class AuthenticateUser
  include Interactor

  def call
    if user = User.authenticate(context.email, context.password)
      context.user = user
      context.token = user.secret_token
    else
      context.fail!(message: "authenticate_user.failure")
    end
  end
end

Monday, 2 March 2020

RuboCop In Rails

RuboCop

A RuboCop is used in Rails best practices and coding conventions. It will make your code in good standard format. it will suggest you the best option to refactor your code in your rails application.


Installation

gem install rubocop-rails


Usage

You can run rubocop in your terminal. You can see the changes in yours console.

$ rubocop

Sunday, 9 February 2020

Create React App in linux system

Creating React Application

To install react run
$  sudo npm install -g create-react-app

Install nde dependencies
$  sudo npm i -g npx
$  sudo apt install nodejs-legacy
$  sudo npm install

Check the below items are installed in your system
$ node -v
$  npx -v
$  npm -v

Install latest version of node
$ sudo npm install -g n
$ sudo n stable

Create the react app
$ npx create-react-app rajfirstreact

Go to the app folder
$ cd rajfirstreact

Run  the react application
$ npm start

You can see your react application running in port 3000
http://localhost:3000

Thursday, 30 January 2020

MailCatcher for Development mail checking in Rails

MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that's arrived so far.
  1. gem install mailcatcher
  2. mailcatcher
  3. Click on http://127.0.0.1:1080
Add the below code in environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
You can trigger any mail then it will display immediately.

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"]

Monday, 4 November 2019

How to use module as Namespace in ruby?

 Module or Mix-in used as a Namespace in ruby. It is used to avoid collision between two classes.


module Rajkumar
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email is #{@email}"
        end
    end
end

module Somu
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email 2 is #{@email}"
        end
    end
end

puts raj = Rajkumar::Uthayaa.new
puts raj.email='rajutaya@gmail.com'
puts raj.display_email
puts raj = Somu::Uthayaa.new
puts raj.email='lalalslsls@gmail.com'
puts raj.display_email

Interactor in Rails

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