Skip to content

Instantly share code, notes, and snippets.

@rogercampos
rogercampos / current_controller.rb
Created November 18, 2010 08:39
Simple snippet similar to current_page? but only maching the controller
def current_controller?(opts)
hash = Rails.application.routes.recognize_path(url_for(opts))
params[:controller] == hash[:controller]
end
@rogercampos
rogercampos / calling_method.rb
Created December 17, 2010 08:00
Return the calling method. This version also works when the call was from inside a block.
def this_method
caller[0] =~ /`([^']*)'/ and $1
end
# This one only works in ruby 1.8.x, see the other file for 1.9 version
def calling_method(depth = 1, file = "")
caller[depth] =~ /^(.+?):(\d+)(?::in `(.*)')?/
if $3 && ($1 == file || depth == 1)
$3
else
@rogercampos
rogercampos / lazy_hash.rb
Created December 17, 2010 20:09
Asign a value with an arbitrary deep of keys to a lazy hash. The example shows a use case with i18n keys.
# lazy_hash.rb
module LazyHash
class << self
def lazy_add(hash, key, value, pre = nil)
skeys = key.split(".")
f = skeys.shift
if skeys.empty?
pre.nil? ? hash.send("[]=", f, value) : pre.send("[]=", f, value)
else
pre = pre.nil? ? hash.send("[]", f) : pre.send("[]", f)
@rogercampos
rogercampos / iso8859_2_utf8.rb
Created December 23, 2010 15:20
Encode input files to ut8 from iso-8859-1
#!/usr/bin/env ruby
files = ARGV.select do |x|
`file -i #{x} | grep iso-8859-1` != ''
end
files.each do |file|
`iconv -f iso-8859-1 -t utf-8 #{file} > aux`
`mv aux #{file}`
puts "Converted #{file}"
@rogercampos
rogercampos / README.markdown
Created August 13, 2011 14:50 — forked from ariera/README.markdown
Nestable, sortable and dragable categories

Nestable, sortable and dragable categories:

In the project I'm working on we wanted to have a Category model which we wanted to be nestable. But we also liked the user to have a draggable interface to manage and rearrange the order of his categories. So we chose awesome_nested_set for the model and jQuery.nestedSortable for the UI.

It took me some time to arrange things to work properly so I wanted to share my work in case it helps anybody.

Before beginning

you might want to take a look at a demo app

  1. go to: http://awesomenestedsortable.heroku.com/groups/
  2. click in show of any group
@rogercampos
rogercampos / example.rb
Created November 23, 2011 16:06
Retrying Hominid api calls due to EOFError
# Example of a DelayedJob that syncs info with mailchimp
class SyncMailchimp < Struct.new(:opts)
include Dobexer::ExceptionNotifier
def run_hominid(attempts = 0, &block)
attempts += 1
block.call
rescue EOFError => e
@rogercampos
rogercampos / play-with-modules.rb
Created December 20, 2011 21:46
Playing with modules
module A
def name= a
@name = a + " mi anonim"
end
end
module Version1
def armotize(name)
mixin = Module.new
mixin.module_eval <<-STR
@rogercampos
rogercampos / example.rb
Created December 23, 2011 16:54
Redisplay a filled form with redirect
# In the controller
def update
@model = Model.find params[:id]
if @model.update_attributes(params[:model])
redirect_to an_url
else
save_for_redisplay(@model)
redirect_to other_url
end
// Use it like:
var myQueue = new TimedQueue(800);
// Call this 100 times in less than 500 ms:
myQueue.push(myCallback);
// myCallback function will be called only once, exactly 800 ms after the last call to 'push'
@rogercampos
rogercampos / sql_query_assertions.rb
Created June 19, 2012 18:44
check for sql queries
def count_query_reads_for(clazz)
old = ActiveRecord::Base.logger
log_stream = StringIO.new
logger = Logger.new(log_stream)
ActiveRecord::Base.logger = logger
yield
ActiveRecord::Base.logger = old
logger.close
log_stream.string.scan(/#{clazz} Load/).size
end