Skip to content

Instantly share code, notes, and snippets.

@mcls
Created January 4, 2021 18:10
Show Gist options
  • Save mcls/530e8cd23fad2c1ed55784fdae596a8c to your computer and use it in GitHub Desktop.
Save mcls/530e8cd23fad2c1ed55784fdae596a8c to your computer and use it in GitHub Desktop.
Rails Routes class
# Make it easier to call rails routes from anywhere
class Routes
include Rails.application.routes.url_helpers
delegate :asset_path, :image_path, to: :controller_helpers
protected
def controller_helpers
ActionController::Base.helpers
end
class << self
def default_url_options
ActionMailer::Base.default_url_options
end
end
end
# Examples
ROUTER = Routes.new
ROUTER.root_path
# => "/"
ROUTER.asset_path("logo.png")
# => "/assets/logo-4877cac84535062a0d0a24c77c7a09bcb3290a4ec0beda3a401f6f60713bce5f.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment