Skip to content

Instantly share code, notes, and snippets.

@wkrsz
Created May 7, 2010 14:16
Show Gist options
  • Save wkrsz/393466 to your computer and use it in GitHub Desktop.
Save wkrsz/393466 to your computer and use it in GitHub Desktop.
hack to support post_url(@post)
require 'rubygems'
require 'usher'
# according to README should be:
# Usher::Util::Rails.activate
# but above gives an error: vendor/plugins/usher/lib/usher/util/rails.rb:13: class definition in method body (SyntaxError)
# - so instead we load appropriate interface manually:
ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails23)"
# Monkey-patch to make Usher support usage like post_url(@post)
class Usher
module Interface
class Rails23
def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
#*_url and hash_for_*_url
Array(destinations).each do |d| d.module_eval { include Helpers }
@router.named_routes.each do |name, route|
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
def #{name}_url(options = {})
options = {:id => options.id} unless options.is_a?(Hash) # if we receive an AR record, put it in :id param
unless options.is_a?(Hash)
options = {:id => options}
end
ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
end
def #{name}_path(options = {})
options = {:id => options.id} unless options.is_a?(Hash) # if we receive an AR record, put it in :id param
ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
end
end_eval
end
d.__send__(:include, @module)
@router.named_routes.instance_eval "
def helpers
{ }
end
"
@router.named_routes.helpers.__send__(:extend, @module)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment