Posts

Showing posts with the label rails5

lambda proc and block in rails

this all things basically we used in block level coding and for some custom queries in rails model class Main   [ 1 , 2 , 3 ].each do | num |     puts num   end end def block_method   yield end block_method { puts "Test"} def arg_method   yield   yield 1 end arg_method { |i| puts i*1 rescue nil} # explicit block def explicit_block(&block)   block.call #same as yield end explicit_block {puts "This is explicit block"} # lambda lambda_test = -> {puts "This is lambda"} lambda_test.call lambda_test.() lambda_test[] # lambda with args arg_lambda = -> (i){ puts i} arg_lambda.call("test") my_proc = Proc.new { |x| puts x } my_proc.call # Should work my_lambda = -> { return 1 } puts "Lambda result: #{my_lambda.call}" # Should raise exception my_proc = Proc.new { return 1 } puts "Proc result: #{my_proc.call}" def call_proc   puts "Before proc"   my_proc = Proc.n

Data Scrapping in rails

go to your terminal create a directory mkdir ruby_data_scrapping create directory mkdir lib cd lib/ touch scraper.rb gem install nokogiri  // install gems in local gem install httparty gem install byebug Write code in your scraper.rb require "httparty" require "nokogiri" require "byebug" class Scraper   attr_accessor :parse_page   def initialize       doc = HTTParty.get("https://in.tradingview.com/markets/currencies/rates-africa/")       @parse_page ||= Nokogiri::HTML(doc)   end   def get_prices      item_container.first(2).last.children.map{|x| x.text}   end   private     def item_container        parse_page.css(".tv-data-table__tbody").first.css(".tv-screener-table__result-row").css(".tv-screener-table__cell")     end   prices = Scraper.new.get_prices   (0...prices.size).each do |index|       puts "Price: #{prices[index]}"   end end now run this scraper ruby scraper.rb

how to make separate routes for different user in rails

how to make separate routes for different user in rails  Make another routes file in your config folder           config/routes/admin.rb   //i made new file routes/admin.rb inside config folder Now inside your config/routes.rb file paste this           class ActionDispatch::Routing::Mapper             def draw(routes_name)              instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))             end           end        // paste this code above this Rails.application.routes.draw line now side your main route file(config/routes.rb) make new routes for admin file like draw :admin Now config/routes.rb will be look like this Rails.application.routes.draw do   draw :admin end Now you can make routes for admin or admin dashboard separately inside your config/routes/admin.rb file resources :admin

Create worker in rails by sidekiq and redis

Add sidekiq to your Gemfile: Add this to your Gemfile gem 'sidekiq' Run bundle install bundle install Create worker rails g sidekiq:worker Test #will create app/workers/test_worker.rb class TestWorker   include Sidekiq::Worker   def perform(id, name)     # do something   end end Call worker without any time boundation TestWorker.perform_async(5, 'rahul') //use this anywhere from your rails app(controller, model, helpers..) Call worker within particular time period TestWorker.perform_in(5.minutes, 5, 'rahul')  //use this anywhere from your rails app(controller, model, helpers..) Call worker after specific time interval once only TestWorker.perform_at(5.minutes.from_now, 5, 'rahul')  //use this anywhere from your rails app(controller, model, helpers..) Start sidekiq on development bundle exec sidekiq Start sidekiq on production bundle exec sidekiq -d //run this first if you get some error then use second one bundle exec si

How to override method in rails 5 plugin

Override controller method in our plugin rails 5 Here i override user controller method in this plugin you can review this plugin cilck here to download plugin