Skip to content

Instantly share code, notes, and snippets.

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

Amol Pujari amolpujari

💭
I may be slow to respond.
  • Pune
View GitHub Profile
#!/bin/bash
while :
do
clear
git --no-pager log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --all
sleep 1
done
@amolpujari
amolpujari / active_admin.rb
Created March 26, 2013 09:47
activeadmin timestamps columns, and other customizations
module ActiveAdmin
module Views
class IndexAsTable < ActiveAdmin::Component
class IndexTableFor < ::ActiveAdmin::Views::TableFor
def timestamps_columns
column :created_at, :sortable => :created_at do |resource|
resource.created_at and resource.created_at.strftime('%b %-d %Y, %l %P')
end
column :updated_at, :sortable => :updated_at do |resource|
@amolpujari
amolpujari / concerns.rb
Last active December 15, 2015 22:48
Different ways of having modular active record models
# config/initializers/concerns.rb
class << ActiveRecord::Base
def concerned_with(*concerns)
concerns.each do |concern|
require_dependency "#{name.underscore}/#{concern}"
klass = concern.to_s.classify.constantize rescue nil
send(:include, klass) if klass.is_a? Module
end
end
end
@amolpujari
amolpujari / config.rb
Created April 8, 2013 08:19
Rails configurations
# config/initializers/config.rb
APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
def APP_CONFIG.[](key)
key = key.to_s
if Rails.env=='production' || Rails.env == 'staging'
p "APP_CONFIG['#{key}'] not defined." unless self.has_key? key
else
raise "APP_CONFIG['#{key}'] not defined." unless self.has_key? key
#test.rb
def mem_usage
`ps -o rss= -p #{Process.pid}`.to_i
end
def random_str(length=1)
(Object.new.hash.to_s << Object.new.hash.to_s)[0..(length-1)].to_s
end
class A
@amolpujari
amolpujari / TrialPlan.py
Created April 18, 2013 05:06
Trial Plan DSL sample
from calc import Calc
from plan import Plan
from datetime import datetime
....
class OurTrialPlan(Plan):
def __init__(self, start_date, end_date, usage, extra=None):
super(OurTrialPlan, self).__init__()
self.calc = Calc(start_date, usage)
self._extra = extra if type(extra)==dict else {}
@amolpujari
amolpujari / default.rb
Last active December 16, 2015 22:29
chef-solo rails deploy git submodule checkout branch sha
# solo.rb
require File.expand_path('../lib/global_constants.rb', __FILE__)
require File.expand_path('../lib/git_addition.rb', __FILE__)
root = File.absolute_path(File.dirname(__FILE__))
file_cache_path root
cookbook_path [ root + '/cookbooks', root + '/custom_cookbooks']
data_bag_path root + '/data_bags'
node_path root + '/nodes'
@amolpujari
amolpujari / ox_parsing.rb
Last active September 22, 2018 17:49
example of parsing large xml files in ruby using ox, define a handler, look up for a particular root element
require "awesome_print"
module XmlParsing
require "ox"
class Reader < ::Ox::Sax
def initialize file_path, target, target_handler
@target_handler = target_handler
@target = target
@file_path = file_path
@amolpujari
amolpujari / modify_emails.html.erb
Last active December 24, 2015 23:29
rails user modifies email
# gem 'ckeditor_rails' # from https://github.com/tsechingho/ckeditor-rails
# allow modifying existing template
<textarea name="modified_email" class="ckeditor">
<%= File.read "#{Rails.root.to_s}/app/views/user_mailer/do_email.html.erb"%>
</textarea>
<%= javascript_include_tag "ckeditor-jquery" %>
<script type="text/javascript">
$('.ckeditor').ckeditor({
@amolpujari
amolpujari / active_admin.js
Created October 23, 2013 04:48
activeadmin tweaks
var admin = {
init: function(){
admin.set_admin_editable_events();
},
set_admin_editable_events: function(){
$(".admin-editable").live("keypress", function(e){
if ( e.keyCode==27 )