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

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...