Monday, 12 March 2018

Generate Custom string

small_rand = [*('a'..'z')].shuffle
number_rand = [*('0'..'9')].shuffle
cap_rand = [*('A'..'Z')].shuffle
password_rand = small_rand[0..1].join + number_rand[0..6].join + cap_rand[0]

Sunday, 11 March 2018

testing configration in application.rb

config.generators do |g|
  g.test_framework :rspec,
    :fixtures => true,
    :view_specs => false,
    :helper_specs => false,
    :routing_specs => false,
    :controller_specs => true,
    :request_specs => true
  g.fixture_replacement :factory_girl, :dir => "spec/factories"
end

Sunday, 18 February 2018

FInd months and weeks between two date range OR Add array index to each other

require 'date'

months = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).group_by(&:month).map { |_,v| v.first.end_of_month.to_s } 
# => ["2014-01-31", "2014-02-28", "2014-03-31"]


weeks = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).select(&:sunday?).map(&:to_s) 






Add Array index with Value

 [[1,2,3], [4,5,6]].transpose.map {|x| x.reduce(:+)}

Saturday, 18 November 2017

Send email using active jobs in rails with sendgrid credentials


step 1. rails g scaffold user name:string email:string

step 2. rails generate mailer UserMailer

step 3. put method in user mailer
   
      def welcome_email(user)
        @user = user
        mail(to: @user.email, subject: 'Welcome to My Awesome Site')
      end
step 4. need to set SMTP setting in development.rb and production.rb file
    I am sending email using sendgrid credentials.
   
    config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
      :user_name => 'sendgrid username',
      :password => 'sendgrid password',
      :domain => 'localhost',
      :address => 'smtp.sendgrid.net',
      :port => 587,
      :authentication => :plain,
      :enable_starttls_auto => true
     }
step 5. rails g job user

step 6. copy and paste this line or you can define according to your class mailer and method name.
   
    class UserJob < ApplicationJob
      queue_as :default

      def perform(user)
          user_email = user
        ProductMailer.welcome_email(user_email).deliver_now
      end
    end
step 7. copy and paste below line in you controller method or model method where you want to use.
   
    UserJob.set(wait: 2.minutes).perform_later(current_user)

Thursday, 16 November 2017

How to use multiple query in one way

 isp_condition = []
  report_date_condition = []
  other_conditions = []
 
if params[:q].present?
            # Filttering record from date wise.
            if params[:q][:report_date_gteq].present? && params[:q][:report_date_lteq].present?
              report_date_condition = ['report_date >= ? and report_date <= ?', params[:q][:report_date_gteq], params[:q][:report_date_lteq]]
            end

              # Filters for isp and other controls for fetching record.
              params[:q].each do |k,v|
                next if k.include?("report_date")
                if k.include?("isp")
                  condition = k.split("_").last
                  case condition
                  when "contains"
                    isp_condition = ['isp like ?', "%"+v+"%"]
                  when "equals"
                    isp_condition = ['isp = ?', v]
                  when "starts_with"
                    isp_condition = ['isp like ?', v+"%"]
                  when "ends_with"
                    isp_condition = ['isp like ?', "%"+v]
                  end
                  next
                end
             
              if k.include?("equals")
                param_name = k.split('_equals').first
                other_conditions << ["#{param_name} = ?", v]
              elsif k.include?("greater_than")
                param_name = k.split('_greater_than').first
                other_conditions << ["#{param_name} > ?", v]
              elsif k.include?("less_than")
                param_name = k.split('_less_than').first
                other_conditions << ["#{param_name} < ?", v]
              end
            end
          end

ActiveAdmin custom csv file method


collection_action :csv_isp_report, method: :get do
      @data = []
      @isp_manuall = $record_arr
      @isp_manuall.map{|isp_manuall| isp_manuall.map { |isp| @data << isp}}
      @data_records = @data
      csv_string = CSV.generate do |csv|
        csv << IspReport.attribute_names
        @data_records.each do |ispr|
          csv << ispr.attributes.values
        end
      end
      send_data csv_string 
  end

in View

= link_to "CSV", csv_isp_report_admin_isp_reports_path(format: "csv")

Wednesday, 19 July 2017

All kind of form validation and request

1. Please visit this link which is given below. you will find awesome solution.

http://formvalidation.io/examples/

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...