Wednesday 11 December 2019

Generate PDF with rake task



add these gems  in your gem file

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.1'
gem "combine_pdf"

and run bundle

In views create directory pdf

create a partial file  _index.pdf.erb
<h1>Hello <%=  name %></h1>

in rake task write below-given code

    ac = ActionController::Base.new()
    header_html = ac.render_to_string(partial: 'pdf/_index.pdf.erb', locals: { name: 'Emissions Export'} )
    pdf = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })

  save_path = Rails.root.join('public','mytest.pdf')
    File.open(save_path, 'wb') do |file|
      file << pdf
    end

------------------------------------------------------------------------
if you want to combine two pdf file in one pdf using below given code

pdf = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })
    pdf1 = WickedPdf.new.pdf_from_string(header_html, orientation: 'Landscape', margin: { bottom: 20, top: 30 })
    #
    final_pdf = CombinePDF.new
    final_pdf << CombinePDF.parse(pdf)
    final_pdf << CombinePDF.parse(pdf1)

    save_path = Rails.root.join('public','mytest.pdf')
    File.open(save_path, 'wb') do |file|
      file << final_pdf.to_pdf
    end




Tuesday 12 November 2019

Add Swap memory to ec2 micro instance.


Remember one thing If ec2 instance memory is 1 GB then add 512 MB swap. It should be half of the instance memory.
To add this extra space to your instance you type:
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo chmod 600 /var/swap.1
sudo /sbin/swapon /var/swap.1
If you need more than 1024 then change that to something higher.
To enable it by default after reboot, add this line to /etc/fstab:
/var/swap.1   swap    swap    defaults        0   0

Set up mongodb and create user



$> sudo apt-get install -y mongodb
$> mongo
$> use admin

Please note: only change user and password not change to role and db

$> db.createUser({ user: "admin", pwd: "pwd@123", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
To check user auth

$> db.auth("admin", "hate2wait@123")
$> 1

create normal user

$> use db_production
$> db.createUser({ user: "myname", pwd: "anypassword", roles: [{ role: "dbOwner", db: "db_production" }] })
$> db.auth("myname", "anypassword")
$>

$> sudo nano /etc/mongodb.conf
# Turn on/off security.  Off is currently the default
#noauth = true

auth = true

Uncomment auth = true
ctrl + x



Friday 8 November 2019

Change Id to uuid in rails postgresql


Before run this command if you have migration file in migrate folder please take backup all migration file then run this command and migrate it will work.

rails g migration enable_uuid_extension
endclass EnableUuidExtension < ActiveRecord::Migration
  def change
     enable_extension "uuid-ossp"
    enable_extension "pgcrypto"  
   end 
end

Sunday 22 September 2019

MySql command

mysql                         mysql_config_editor        mysqldumpslow              mysqlprocgrep              mysqlserverclone
mysqladmin                 mysqld                     mysql_embedded             mysqlpump                  mysqlserverinfo
mysqlanalyze               mysqldbcompare             mysqlfailover              mysqlrepair                mysqlshow
mysqlauditadmin            mysqldbcopy                mysqlfrm                   mysqlreplicate             mysqlslap
mysqlauditgrep             mysqldbexport              mysqlgrants                mysqlreport                mysqlslavetrx
mysqlbinlog                mysqldbimport              mysqlimport                mysqlrpladmin              mysql_ssl_rsa_setup
mysqlbinlogmove            mysqldiff                  mysqlindexcheck            mysqlrplcheck              mysql_tzinfo_to_sql
mysqlbinlogpurge           mysqldiskusage             mysql_install_db           mysqlrplms                 mysqluc
mysqlbinlogrotate          mysqld_multi               mysqlmetagrep              mysqlrplshow               mysql_upgrade
mysqlcheck                 mysqld_safe                mysqloptimize              mysqlrplsync               mysqluserclone
mysql_config               mysqldump                  mysql_plugin               mysql_secure_installation  mysql-workbench

Monday 9 September 2019

Create user in mysql 

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON my_db.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Monday 5 August 2019

Upload image using ajax request in rails

          <%=  file_field_tag :customer_profile, id: "customer_profile"  %>           

          var formData = new FormData();
          custProfile  =$("#customer_profile")[0].files[0]
          formData.append("device_img", custProfile);
          $.ajax({
          url: "<%=  upload_image_customer_path(id: params[:id]) %>",
          type: 'POST',
          dataType: 'json',
          data: formData,
          contentType: false,
          processData: false,
          cache : false,
          success: function(data){
            if(data["msg"] == true){
              alert(success)
             }
            },
           error: function(data){
             console.log(data); 
            }
          });

Friday 2 August 2019

Image convert in base64 in rails

require "base64"

file_url = "https://s3.ap-south-1.amazonaws.com/on-store-billing/uploads/insurance/servify_device_image/58/1__2_.jpeg"

encoded_string = Base64.encode64(open(file_url) { |io| io.read })

decode_img= Base64.decode64(encoded_string)

below given method write/create image with name image.png in directory

File.open(‘image.png’, ‘wb’) { |f| f.write(decode_img) }

Tuesday 30 July 2019

Login Postgresql with specific database

$  psql -d db_name -U username

e.g. $  psql -d on_store_development -U postgres

then it will ask for password default is root or postgres. Or check your database.yml for a password that password you have to type here.

Tuesday 25 June 2019

Important commands in rails and rake

Rails:
  console
  credentials:edit
  credentials:show
  dbconsole
  destroy
  encrypted:edit
  encrypted:show
  generate
  new
  runner
  secrets:edit
  secrets:setup
  secrets:show
  server
  test
  version

Rake:
  about
  active_storage:install
  app:template
  app:update
  assets:clean[keep]
  assets:clobber
  assets:environment
  assets:precompile
  cache_digests:dependencies
  cache_digests:nested_dependencies
  db:create
  db:drop
  db:environment:set
  db:fixtures:load
  db:load_config
  db:migrate
  db:migrate:status
  db:rollback
  db:schema:cache:clear
  db:schema:cache:dump
  db:schema:dump
  db:schema:load
  db:seed
  db:setup
  db:structure:dump
  db:structure:load
  db:version
  dev:cache
  initializers
  log:clear
  middleware
  notes
  notes:custom
  restart
  routes
  secret
  stats
  test
  test:db
  test:system
  time:zones[country_or_offset]
  tmp:clear
  tmp:create
  yarn:install

Friday 24 May 2019

Copy Dump from server to local machine


~/Desktop$ scp -i ~/Desktop/permission/h2w/postgres-dump.pem ubuntu@0.0.0.0:./dumpfile_name ./

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