Skip to content

Instantly share code, notes, and snippets.

class String
#
# http://rors.org/2008/7/09/alternative-join-and-split-in-ruby
#
alias / :split
end
@dejan
dejan / gist:74643
Created March 5, 2009 23:29
(rails 2.3 feature) batch updating all definitions
Definition.find_in_batches { |group| group.each { |item| item.save } }
Video.find_in_batches { |group| group.each { |item| item.save } }
AttachedImage.find_in_batches(:conditions => 'parent_id is null') { |group| group.each { |item| item.save } }
"Poke %s!" % %w{him her}[@user.sex]
@dejan
dejan / code filter for haml
Created April 3, 2009 13:59
code filter for haml
module Haml
module Filters
module Code
include Base
def render(text)
text = Haml::Helpers.html_escape(text)
text = Haml::Helpers.preserve(text)
text
end
end
@dejan
dejan / compat-join.rb
Created April 3, 2009 18:22
compact join
[options[:class], clazz].compact * ' '
names.split(/\s*,\s*/).each { |name| ... }
@dejan
dejan / test.rake
Created April 11, 2009 18:31
rake test:coverage
namespace :test do
RCOV = "rcov --rails --aggregate coverage.data --text-summary -Ilib"
def rcov_on(files_selector)
unless Dir[files_selector].size == 0
system("#{RCOV} --html #{files_selector}")
end
end
desc 'Measures test coverage'
(0..6).map {|i| i.day.ago.strftime("%a")[0,1] }
@dejan
dejan / gist:123536
Created June 4, 2009 09:27
Nesting params in Rails
>> params = {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
=> {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
>> query = CGI::unescape(params.to_query)
=> "line_items[][quantity]=1&line_items[][quantity]=2"
>> parsed_params = ActionController::AbstractRequest.parse_query_parameters(query)
=> {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
>> params == parsed_params
<a class="hide" href="javascript:void(0);">hide</a>