Skip to content

Instantly share code, notes, and snippets.

View mohitjain's full-sized avatar
🏠
Working from home

Mohit Jain mohitjain

🏠
Working from home
View GitHub Profile
@mohitjain
mohitjain / nginx_configuration
Created April 6, 2018 18:58 — forked from luugiathuy/nginx_configuration
nginx configuration
upstream app_name {
server unix:///tmp/app_name.sock;
}
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
@mohitjain
mohitjain / csv_creation_memory_leak.rb
Last active May 9, 2017 02:53
Memory issues with creating a csv
### A Simple code which creates a two dimentional array and then creates a CSV from it.
require "benchmark"
num_rows = 100000
num_cols = 10
data = Array.new(num_rows) { Array.new(num_cols) { "x"*1000 } }
puts "%d MB" % (`ps -o rss= -p #{Process.pid}`.to_i/1024)
time = Benchmark.realtime do
csv = data.map do |row|
row.join(",")
@mohitjain
mohitjain / rspec_model_testing_template.rb
Created October 27, 2015 12:43
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@mohitjain
mohitjain / url_validator.rb
Last active August 31, 2015 20:06 — forked from bluemont/url_validator.rb
ActiveModel URL Validator
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
valid = begin
URI.parse(value).kind_of?(URI::HTTP)
rescue URI::InvalidURIError
false
end
unless valid
record.errors[attribute] << (options[:message] || "is an invalid URL")
@mohitjain
mohitjain / loc.sh
Created May 27, 2015 12:43
Number of lines of code in rails project
#!/bin/bash
find . \( -iname '*.rb' -o -iname '*.css' -o -iname '*.js' -o -iname '*.erb' -o -iname '*.html' -o -iname '*.haml' -o -iname '*.scss' -o -iname '*.coffee' \) -exec wc -l {} + | sort -n
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :api
draw :account
draw :session
@mohitjain
mohitjain / Gemfile
Created April 22, 2014 19:03 — forked from bbonamin/Gemfile
Active Admin Auto complete
source 'http://rubygems.org'
gem 'rails3-jquery-autocomplete'
# Usage:
# class MyClass
# extend Sidekiq::Delayable
# def a_method(arg1, arg2)
# #...
# end
# delayed :a_method
# end
module Sidekiq
### blogged @ http://dojo4.com/blog/easy-cheasy-realtime-log-tailing-in-a-rails-admin-view
### the su controller action
def logs
log = File.join(Rails.root, "log", "#{ Rails.env }.log")
@lines = `tail -1024 #{ log }`.split(/\n/).reverse
ActiveAdmin::Dashboards.build do
# Add this section in your dashboard...
section "Background Jobs" do
now = Time.now.getgm
ul do
li do
jobs = Delayed::Job.where('failed_at is not null').count(:id)
link_to "#{jobs} failing jobs", admin_jobs_path(q: {failed_at_is_not_null: true}), style: 'color: red'
end