This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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'); | |
}); | |
}); | |
//]]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat .gemrc | |
--- | |
:verbose: true | |
:benchmark: false | |
:sources: | |
- http://gems.rubyforge.org/ | |
- http://gems.github.com | |
:update_sources: true | |
:backtrace: false | |
:bulk_threshold: 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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... |
OlderNewer