Skip to content

Instantly share code, notes, and snippets.

View rubiety's full-sized avatar

Ben Hughes rubiety

View GitHub Profile
@rubiety
rubiety / gist:5426
Created August 14, 2008 13:27 — forked from qrush/gist:5425
#############################################################
# Application
#############################################################
set :application, "" #your app's name here
set :server_ip, "" #your server's ip here
set :deploy_to, "/home/rails/#{application}" #the path to your rails app here
#############################################################
# Servers
named_scope :with_keywords, lambda {|q|
fields = %w(title information items.sku)
tokens = q.split.collect {|c| "%#{c.downcase}%"}
clause = (["(" + fields.map {|f| "#{f} LIKE ?" }.join(" OR ") + ")"] * tokens.size).join(" AND ")
{ :include => [:items], :conditions => [clause, *(tokens * fields.size).sort] }
}
@rubiety
rubiety / gist:29404
Created November 26, 2008 14:35 — forked from qrush/gist:29315
# If you ever needed an array of all ActiveRecord models in your Rails app (+ Session):
Dir["app/models/**/*.rb"].each {|r| require r}
subclasses_of(ActiveRecord::Base).map(&:class_name)
# Or *all* models:
ActiveRecord::Base.connection # Preloads driver constants
constants_before = Object.constants
Dir["app/models/**/*.rb"].each {|r| require r}
Object.constants - constants_before
class ActivityEntry < ActiveRecord::Base
belongs_to :target, :polymorphic => true
belongs_to :user
# Target Scopes
named_scope :for, lambda {|obj| {:conditions => {:target_id => obj.id, :target_type => obj.class.name}} }
named_scope :for_listings, {:conditions => {:target_type => 'Listing'} }
named_scope :manageable_by, lambda {|user|
case
when user.can_manage_company? then {}
indigosun:vim-fu bhughes$ rake preinstall
(in /Users/bhughes/Workspace/Source/vim-fu)
Usage: /usr/local/git/bin/git-submodule [--quiet] [--cached] [add <repo> [-b branch]|status|init|update|summary [-n|--summary-limit <n>] [<commit>]] [--] [<path>...]
# Bad:
1.upto(10) do |i|
puts "X" * i
end
# Good:
((nil.object_id - 3)..(2**3 + 2)).map do |i|
lambda { "X" * i }
end.collect {|i| i.send(:call)}.tap do |a|
puts a.class.instance_method(:join).send(:bind, a).call("\n")
module ApplicationHelper
def tree_options_from_collection_for_select(objects, value = :id, name = :name, selected = nil, options = {})
options[:indent_with] ||= '- '
options[:level] ||= 0
objects.inject([]) do |entries, object|
entries << [options[:indent_with] * options[:level] + object.send(name), object.send(value)]
entries += tree_options_from_collection_for_select(object.children, value, name, selected, options.merge(:level => options[:level] + 1))
entries
end
### Don't you wish you could use this if-nil idiom?
first_name || "Joe"
# Well you can't if you expect first_name to be blank instead of nil, so Ruby will short-circuit.
# Enter deblank:
class Object
def deblank
self.blank? ? nil : self
end
### Don't you wish you could use this if-nil idiom?
first_name || "Joe"
# Well you can't if you expect first_name to be blank instead of nil, so Ruby will short-circuit.
# Enter deblank:
class Object
def deblank
self.blank? ? nil : self
end
## Concatenates a subset of columns in Excel amongst multiple sheets into one dump
require "rubygems"
require "active_support"
require "roo"
require "faster_csv"
COLUMNS = ["A", "B"] # Column names of whatever you want
[].tap do |o|