Skip to content

Instantly share code, notes, and snippets.

View gravis's full-sized avatar

Philippe Lafoucrière gravis

View GitHub Profile
@gravis
gravis / gist:55081
Created January 30, 2009 14:12
Convert keys and values to int (to_i) for a Hash
# convert a hash composed of strings to integers
# ex: h = {"1" => "2", "3" => "4"}
# I want {1 => 2, 3 => 4}
string_hash = {"1" => "2", "3" => "4"}
# => {"1"=>"2", "3"=>"4"}
int_hash = Hash[*string_hash.to_a.flatten.map(&:to_i)]
# => {1=>2, 3=>4}
@gravis
gravis / gist:55084
Created January 30, 2009 14:23
Calculate sum of a Hashed cart
# make sure cart is composed of integers first.
# Without AR finding :
items = {1=>2, 3=>4}
price = 1.0
items.to_a.inject(0) {|sum,cart_item| sum + price * cart_item[1]}
# => 6.0
items.to_a.inject(0) {|sum,cart_item| sum + Product.find(@items[cart_item[0]],:select => :price).price * cart_item[1]}
# => 6.0
# if prices are all eq to 1.0
@gravis
gravis / README.txt
Created February 22, 2009 21:24 — forked from rmm5t/README.md
You can represent time statements in most western languages where
a prefix and/or suffix is used.
The default case is to use suffix only (as in English), which you
do by providing the `suffixAgo` and `suffixFromNow` settings in
the strings hash (earlier versions of timeago used the deprecated
`ago` and `fromNow` options). If present, they are used.
2 minutes [suffixAgo]
2 minutes [suffixFromNow]
# Taken from Thoughtbot-shoulda
# Put me in RAILS_ROOT/test/shoulda_macros/xml.rb
# Macro that creates a test asserting that the controller responded with an XML content-type
# and that the XML contains +<name/>+ as the root element.
def should_respond_with_xml_for(name = nil)
should "have ContentType set to 'application/xml'" do
assert_xml_response
end
%script{ :src => "http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php", :type => "text/javascript" }
:javascript
//<![CDATA[
$(function(){
FB_RequireFeatures(["XFBML"], function() {
FB.Facebook.init('key','/xd_receiver.html');
});
});
//]]>
# Fix the issue with domain cookie sessions in rails 2.3.
# Put this in your environment file (config/environment/staging.rb for me)
Rails.configuration.action_controller.session[:domain] = '.mydomain.net'
# instead of :
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update(:domain => ".mydomain.net")
# or
ActionController::Base.session_options[:domain] = ".mydomain.net"
# Render a timestamp suitable for javascript substitution (jquery.timeago)
# Sorry for copyright, I can't remember where I found this piece of code ! Do not hesitate to claim it
def timeago(time, options = {})
options[:class] ||= "timeago"
content_tag(:abbr, time.to_s, options.merge(:title => time.getutc.iso8601)) if time
end
# don't forget to add this to substitute times (HAML) :
# :javascript
# Open in the default browser the page being "seen" by webrat, when a cucumber feature fails:
# Credits go to Ben Mabey (cf http://groups.google.com/group/cukes/browse_thread/thread/c5c3c73e469382d6)
After do |scenario|
if scenario.failed? && scenario.exception.is_a?(Webrat::NotFoundError)
save_and_open_page
end
end
$ cat .gemrc
---
:verbose: true
:benchmark: false
:sources:
- http://gems.rubyforge.org/
- http://gems.github.com
:update_sources: true
:backtrace: false
:bulk_threshold: 1000
$ sudo gem list hanna
*** LOCAL GEMS ***
hanna (0.1.9)
$ sudo gem install justinfrench-formtastic
Successfully installed justinfrench-formtastic-0.2.4
1 gem installed
Installing ri documentation for justinfrench-formtastic-0.2.4...