Skip to content

Instantly share code, notes, and snippets.

@vesan
Created July 17, 2012 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vesan/3131711 to your computer and use it in GitHub Desktop.
Save vesan/3131711 to your computer and use it in GitHub Desktop.
Rails router class & module
# From: http://www.broadcastingadam.com/2012/03/generating_urls_whenever_and_wherever_you_want/
## Class
class Router
include Rails.application.routes.url_helpers
def self.default_url_options
ActionMailer::Base.default_url_options
end
end
router = Router.new
router.posts_url # http://localhost:3000/posts
router.posts_path # /posts
## Module
module Routing
extend ActiveSupport::Concern
include Rails.application.routes.url_helpers
included do
def default_url_options
ActionMailer::Base.default_url_options
end
end
end
class UrlGenerator
include Routing
end
generator = UrlGenerator.new
generator.posts_url
generator.posts_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment