Sha256: 73742a86fecb0b32f10219fa22fd8341bb7e554dd77264855e5e568dfe2df039

Contents?: true

Size: 733 Bytes

Versions: 2

Compression:

Stored size: 733 Bytes

Contents

class JsRoutes
  # A Rack middleware that automatically updates routes file
  # whenever routes.rb is modified
  #
  # Inspired by
  # https://github.com/fnando/i18n-js/blob/main/lib/i18n/js/middleware.rb
  class Middleware
    def initialize(app)
      @app = app
      @routes_file = Rails.root.join("config/routes.rb")
      @mtime = nil
    end

    def call(env)
      update_js_routes
      @app.call(env)
    end

    protected

    def update_js_routes
      new_mtime = routes_mtime
      unless new_mtime == @mtime
        JsRoutes.generate!
        JsRoutes.definitions!
        @mtime = new_mtime
      end
    end

    def routes_mtime
      File.mtime(@routes_file)
    rescue Errno::ENOENT
      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
js-routes-2.2.1 lib/js_routes/middleware.rb
js-routes-2.2.0 lib/js_routes/middleware.rb