Skip to content

Instantly share code, notes, and snippets.

View wuputah's full-sized avatar
🐘

Jonathan Dance (JD) wuputah

🐘
View GitHub Profile
# All the places I used !! in a very sizable project
# (4 cases in 7600 LOC, about 40% of which is specs)
# ActiveRecord callback - writing to a DB:
def update_has_emailable_accounts!
# !! replaces "condition ? true : false"
self.update_attribute(:has_emailable_accounts, !!accounts.detect { |account| account.emailable? })
end
// invokes +callback+ on elements found by +selector+ that have an href property value
// equal to the current page (pathname). It is recommended to use a selector that selects
// anchor (a) elements.
function currentPage(selector, callback) {
$$(selector).each(function(e) {
if (e.href == document.URL) {
callback.call(null, e);
}
})
}
/*
The shuttle is a 1-5 paged slideshow of elements with pagination and small dots
to indicate what page you are on. The slideshow auto-advances when you're not
using it, and a few other minor features.
Implemented with Prototype and Low Pro.
*/
Shuttle = {};
Shuttle.CurrentPage = 1;
Shuttle.AutoAdvanceTime = 0;
# this works fine, but it uses a local variable with no purpose
factorial = lambda do |n|
return 1 if n <= 1
n * factorial.call(n - 1)
end
puts factorial.call(5)
module ActiveRecord
module NamedScope
# All subclasses of ActiveRecord::Base have two named \scopes:
# * <tt>all</tt> - which is similar to a <tt>find(:all)</tt> query, and
# * <tt>scoped</tt> - which allows for the creation of anonymous \scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
#
# These anonymous \scopes tend to be useful when procedurally generating complex queries, where passing
# intermediate values (scopes) around as first-class objects is convenient.
def self.included(base)
base.class_eval do
#map.invoices 'customers/invoices/:date_from/:date_to.:format', :controller => 'customers', :action => 'invoices', :format => 'html', :date_to => Date.today
map.invoices 'customers/invoices/:date_from', :controller => 'customers', :action => 'invoices', :format => 'html', :date_to => Date.today
map.invoices 'customers/invoices/:date_from/:date_to.:format', :controller => 'customers', :action => 'invoices', :format => 'html'
## OR MAYBE? ##
map.invoices 'customers/invoices/:date_from.:format', :controller => 'customers', :action => 'invoices', :format => 'html', :date_to => Date.today
map.invoices 'customers/invoices/:date_from/:date_to.:format', :controller => 'customers', :action => 'invoices', :format => 'html'
def attributes=(attributes = {})
attributes.each_pair do |k, v|
self.send("#{k}=", attributes.delete(k)) unless self.has_attribute?(k)
end
super(attributes)
end
tmp_file_path('gateway', '/data/nginx_tmp')
module GoogleSearchHelper
def search_box_js
<<-GOOGLE_JS
<!-- Google CSE Search Box Begins -->
<form action="/search" id="cse-search-box">
<input type="hidden" name="cx" value="YOUR CX VALUE" />
<input type="hidden" name="cof" value="YOUR COF VALUE" />
<input type="text" name="q" size="25" />
>> ObjectSpace.each_object { }
=> 12212
>> ObjectSpace.each_object { }
=> 12465
>> ObjectSpace.each_object { }
=> 12718
>> ObjectSpace.each_object { }
=> 12971
>> ObjectSpace.each_object { }
=> 13224