Created
July 17, 2012 20:08
-
-
Save vesan/3131711 to your computer and use it in GitHub Desktop.
Rails router class & module
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
# 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