Sha256: 131b60dba12c88a8e1c7f2368e5c283380a22cbe5e941a32aaca4bf31e7da634

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

class JsRoutes
class SprocketsExtension
  def initialize(filename, &block)
    @filename = filename
    @source   = block.call
  end

  def render(context, empty_hash_wtf)
    self.class.run(@filename, @source, context)
  end

  def self.run(filename, source, context)
    if context.logical_path == 'js-routes'
      routes = Rails.root.join('config', 'routes.rb').to_s
      context.depend_on(routes)
    end
    source
  end

  def self.call(input)
    filename = input[:filename]
    source   = input[:data]
    context  = input[:environment].context_class.new(input)

    result = run(filename, source, context)
    context.metadata.merge(data: result)
  end
end


class Engine < ::Rails::Engine
  require 'sprockets/version'
  v2                = Gem::Dependency.new('', ' ~> 2')
  vgte3             = Gem::Dependency.new('', ' >= 3')
  sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
  initializer_args  = case sprockets_version
                        when -> (v) { v2.match?('', v) }
                          { after: "sprockets.environment" }
                        when -> (v) { vgte3.match?('', v) }
                          { after: :engines_blank_point, before: :finisher_hook }
                        else
                          raise StandardError, "Sprockets version #{sprockets_version} is not supported"
                      end

  initializer 'js-routes.dependent_on_routes', initializer_args do
    case sprockets_version
      when  -> (v) { v2.match?('', v) },
            -> (v) { vgte3.match?('', v) }

      # Other rails version, assumed newer
      Rails.application.config.assets.configure do |config|
        config.register_preprocessor(
          "application/javascript",
          SprocketsExtension,
        )
      end
    else
      raise StandardError, "Sprockets version #{sprockets_version} is not supported"
    end
  end
end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
js-routes-1.4.9 lib/js_routes/engine.rb
js-routes-1.4.8 lib/js_routes/engine.rb
js-routes-1.4.7 lib/js_routes/engine.rb
js-routes-1.4.6 lib/js_routes/engine.rb
js-routes-1.4.5 lib/js_routes/engine.rb