Skip to content

Instantly share code, notes, and snippets.

@mcls
Created January 4, 2021 18:10
Show Gist options
  • Select an option

  • Save mcls/530e8cd23fad2c1ed55784fdae596a8c to your computer and use it in GitHub Desktop.

Select an option

Save mcls/530e8cd23fad2c1ed55784fdae596a8c to your computer and use it in GitHub Desktop.

Revisions

  1. mcls created this gist Jan 4, 2021.
    26 changes: 26 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # 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"