Skip to content

Instantly share code, notes, and snippets.

@sandeepravi
sandeepravi / like_post.rb
Created January 16, 2014 11:38
Polymorphic with integers
class LikePost < ActiveRecord::Base
INTEGER_TYPE = { 0 => "Post", 1 => "SomethingElse" }
module IntTypes
def association_class
return INT_OBJECT_TYPE_TO_CLASSNAME[restricted_object_type].constantize
end
end
end
@sandeepravi
sandeepravi / .watchr
Created June 27, 2012 13:03
Watchr script for Growl
ENV["WATCHR"] = "1"
system 'clear'
def growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Test Results"
puts message
image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
system %(#{growlnotify} #{options} &)
@sandeepravi
sandeepravi / Capistrano-Deployment-Recipe.rb
Created June 12, 2012 08:38 — forked from mrrooijen/Capistrano-Deployment-Recipe.rb
a "base" Capistrano Rails Deployment Recipe. Use it to deploy your Rails application. It is also easily expandable. So feel free to grab this Recipe and add your own tasks/customization!
# Guide
# Configure the essential configurations below and do the following:
#
# Repository Creation:
# cap deploy:repository:create
# git add .
# git commit -am "initial commit"
# git push origin master
#
# Initial Deployment:
@sandeepravi
sandeepravi / image.rb
Last active October 5, 2015 16:37
A Countable Field for Mongoid
class Image
include Mongoid::Document
field :likes_count, :type => Integer, :default => 0
has_many :likes
end
@sandeepravi
sandeepravi / rspec-syntax-cheat-sheet.rb
Created May 29, 2012 07:09 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@sandeepravi
sandeepravi / attr_accessors
Created May 28, 2012 09:48
dynamic attr_accessors
class SomeThing
# Public: Adding a custom attr_accessor
#
# Returns object with custom accessor set
def add_accessor prop
self.class.__send__(:attr_accessor, "my_#{prop}")
self.__send__("my_#{prop}=", prop)
end
end
@sandeepravi
sandeepravi / Gemfile
Created May 23, 2012 11:57
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin