Skip to content

Instantly share code, notes, and snippets.

View lalitlogical's full-sized avatar
💭
I may be slow to respond.

Lalit Kumar Maury lalitlogical

💭
I may be slow to respond.
  • engineer.ai
  • Gurgaon, Haryana, India
View GitHub Profile
@lalitlogical
lalitlogical / timezone_extracter
Last active November 22, 2019 07:37
extract timezone from https://timezonedb.com/time-zones website
var data = [];
$("#content table tbody tr").each(function() {
timezone = {};
$(this).find('td').each(function(index) {
if (index == 0) {
timezone['countryCode'] = $(this).html();
} else if (index == 1) {
timezone['countryName'] = $(this).html();
} else if (index == 2) {
timezone['zoneName'] = $(this).find('a').html();
@lalitlogical
lalitlogical / list_collection.rb
Created September 23, 2019 16:59
Get MongoDB collection list
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'database name')
client.collections.map {|c| puts c.namespace.gsub('database name.','') }.count
ELASTICSEARCH_URL=https://****
AWS_REGION=****
AWS_ACCESS_KEY_ID=****
AWS_SECRET_ACCESS_KEY=****
@lalitlogical
lalitlogical / formatted_duration.rb
Created February 6, 2019 10:49
how to convert 270921sec into days + hours + minutes + sec ? (ruby)
t = 270921
mm, ss = t.divmod(60) #=> [4515, 21]
hh, mm = mm.divmod(60) #=> [75, 15]
dd, hh = hh.divmod(24) #=> [3, 3]
puts "%d days, %d hours, %d minutes and %d seconds" % [dd, hh, mm, ss]
#=> 3 days, 3 hours, 15 minutes and 21 seconds
@lalitlogical
lalitlogical / admin_generator.rb
Created January 16, 2019 11:33
Migrate RailsAdmin's model to ActiveAdmin's model
class AdminGenerator < Rails::Generators::Base
argument :name, type: :string
source_root File.expand_path('../templates', __FILE__)
desc "Generates required files."
def copy_controller_and_spec_files
@model = name.to_s.constantize
@attributes = @model.attribute_names.reject{|c| c.match(/^_id/)}.map(&:to_sym)
template "resource.rb", "app/admin/#{name.underscore}.rb"
end
@lalitlogical
lalitlogical / customise_devise.rb
Created December 18, 2018 06:19
customise devise for otp or reset password token
module CustomiseDevise
extend ActiveSupport::Concern
included do
attr_accessor :project_name, :mailer_host_name, :confirmation_required, :confirmation_type
after_create :send_confirmation_instructions, if: :confirmation_required?
class_eval do
def confirmation_required?
!!confirmation_required
@lalitlogical
lalitlogical / swagger.rb
Last active December 18, 2018 09:54
swagger configuration
GrapeSwaggerRails.options.url = '/api/v1/apis_doc.json'
GrapeSwaggerRails.options.before_action do
GrapeSwaggerRails.options.app_url = request.protocol + request.host_with_port
end
GrapeSwaggerRails.options.app_name = 'Service Name'
GrapeSwaggerRails.options.api_auth = 'basic' # Or 'bearer' for OAuth
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
GrapeSwaggerRails.options.doc_expansion = 'list'
@lalitlogical
lalitlogical / rails_admin_delete_override.rb
Created December 18, 2018 06:15
rails admin delete override
# config/initializers/rails_admin_delete_override.rb
module RailsAdmin
module Config
module Actions
class Delete < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :visible? do
!bindings[:object].respond_to?(:destroyable?) || bindings[:object].destroyable?
end
@lalitlogical
lalitlogical / searchkick.rb
Created December 18, 2018 06:13
searchkick mongoid
Searchkick.class_eval do
# Your new methods here
def self.load_records(records, ids)
records =
if records.respond_to?(:queryable)
# Mongoid 3+
records.queryable.for_ids(ids)
elsif records.respond_to?(:primary_key)
# ActiveRecord
records.where(records.primary_key => ids) if records.primary_key
@lalitlogical
lalitlogical / sidekiq.rb
Created December 18, 2018 06:12
sidekiq
rails_root = Rails.root || File.dirname(__FILE__) + '/../..'
rails_env = Rails.env || 'development'
redis_config = YAML.load(ERB.new(File.read("#{Rails.root}/config/redis.yml")).result)[rails_env]
redis_config.symbolize_keys!
Sidekiq.configure_server do |config|
config.redis = { url: "redis://#{redis_config[:host]}:#{redis_config[:port]}/12" }
end
Sidekiq.configure_client do |config|