Skip to content

Instantly share code, notes, and snippets.

View vraravam's full-sized avatar

Vijay Aravamudhan vraravam

  • ThoughtWorks Inc.
  • Chennai, India
  • X @avijayr1
View GitHub Profile
validates_existence_of:
This method can be used for both Active records as well as Active Resources - ie anything that responds to 'exists?'. It has been tested to work with Rails v2.2+ and supports all options that validates_each does (like allow_nil, if, unless, etc) and a couple of additional ones (resource_name => the attribute name that might be based on a domain concept; and resource_class => that could be derived from the resource name, but could be namespaced, etc). Here is the code:
def validates_existence_of(*attributes)
options = {:message => 'does not exist'}.merge(attributes.extract_options!)
validates_each(attributes, options) do |record, attr, value|
resource_name = options[:resource_name]
resource_name ||= attr.to_s.gsub(/_id$/, "") if attr.to_s =~ /_id$/
resource_name || attr.to_s # last resort
@vraravam
vraravam / gist:874724
Created March 17, 2011 17:14
kill local committed branches in git
#!/usr/bin/env ruby
require 'open3'
def system_puts(command)
puts command
system command
end
branches = `git branch`.split("\n")
current_branch = branches.select{|l| l =~ /^\*/}.first.gsub("\* ", "")
@vraravam
vraravam / gist:877198
Created March 19, 2011 03:39
using_constants for rspec tests
module Spec
module Extension
def using_constants(klass, new_constants)
old_constants = {}
begin
new_constants.each_pair do |name, value|
old_constants[name] = klass.__send__(:remove_const, name)
klass.const_set(name, value)
end
@vraravam
vraravam / translator.rb
Created August 5, 2011 05:32
Use Google translate to translate a yml file from one language to generate a new one for a different language
#!/usr/bin/env ruby
if ARGV.size != 2
puts "Usage: #{$0} <from_language> <to_language>"
exit -1
end
require 'rubygems'
require 'ya2yaml' # this gem is needed for this to work!
require 'yaml'
@vraravam
vraravam / gist:1215650
Created September 14, 2011 01:34
brew install ntfs-3g on Mac OSX Snow Leopard fails!
vraravam@vraravam.local ~>brew install -v ntfs-3g
==> Installing ntfs-3g dependency: fuse4x-kext
==> Cloning https://github.com/fuse4x/kext.git
Updating /Users/vraravam/Library/Caches/Homebrew/fuse4x-kext--git
git remote set-url origin https://github.com/fuse4x/kext.git
git fetch origin
git fetch --tags
==> Checking out tag fuse4x_0_8_12
git checkout fuse4x_0_8_12
HEAD is now at 5cd72b1... Use SDK10.7 specific API only when it is defined
@vraravam
vraravam / gist:1224170
Created September 17, 2011 17:40
rails 3.1.1 & i18n gem & number_to_currency
require 'i18n/backend/base'
module I18n
module Backend
module Base
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) unless locale
entry = key && lookup(locale, key, options[:scope], options)
if options.empty?
@vraravam
vraravam / active_resource_filtered_logger.rb
Created October 27, 2011 08:53
ActiveResource filter logging of sensitive params
require 'active_resource/base'
require 'active_resource/log_subscriber'
module ActiveResource
class LogSubscriber < ActiveSupport::LogSubscriber
def request_with_filtered_logging(event)
request_uri = event.payload[:request_uri]
start_of_query_string = request_uri.index("?")
request_uri_without_query_string = request_uri[0, start_of_query_string]
query_string = request_uri[(start_of_query_string + 1)..-1]
@vraravam
vraravam / minify_css_js.js
Last active July 4, 2019 05:12
For phonegap/cordova projects, if you want to minify your home-grown js/css files (and obfuscate them to some extent), you can do it with the following gist. It requires yuicompressor and a couple of other npm modules (please look at the require calls at the top). It will not touch the original files - only those that end up within the platforms…
#!/usr/bin/env node
var isRelease = (process.env.RELEASE && process.env.RELEASE === "1");
// Turn this on only for release
if (isRelease !== true) {
return;
}
var fs = require('fs');
var path = require('path');
@vraravam
vraravam / backup-moz.sh
Last active January 26, 2024 05:00
Backing up Mozilla Firefox and Thunderbird profiles
#!/usr/bin/env bash
check_app() {
proc="$(pgrep "$1")"
if [ "$proc" != "" ]; then
echo "!!! Shutdown '$1' first!"
return 1
fi
}
@vraravam
vraravam / time_splits.rb
Created January 14, 2015 10:08
Ruby module using arel for null-checks and time comparisons
module TimeSplits
extend ActiveSupport::Concern
module ClassMethods
def not_null?(attribute)
where(arel_table[attribute].not_eq(nil))
end
def in_future?(attribute, default_threshold = Time.current)
not_null?(attribute).where(arel_table[attribute].gt(default_threshold))