Class: JsRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/js_routes.rb,
lib/js_routes/engine.rb,
lib/js_routes/version.rb

Defined Under Namespace

Classes: Configuration, Engine, SprocketsExtension

Constant Summary collapse

DEFAULTS =

OPTIONS

{
  namespace: -> { defined?(Webpacker) ? nil : "Routes" },
  exclude: [],
  include: //,
  file: -> do
    webpacker_dir = Rails.root.join('app', 'javascript')
    sprockets_dir = Rails.root.join('app','assets','javascripts')
    sprockets_file = sprockets_dir.join('routes.js')
    webpacker_file = webpacker_dir.join('routes.js')
    !Dir.exist?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
  end,
  prefix: -> { Rails.application.config.relative_url_root || "" },
  url_links: false,
  camel_case: false,
  default_url_options: {},
  compact: false,
  serializer: nil,
  special_options_key: "_options",
  application: -> { Rails.application },
  module_type: 'ESM',
  documentation: true,
}
NODE_TYPES =

:nodoc:

{
  GROUP: 1,
  CAT: 2,
  SYMBOL: 3,
  OR: 4,
  STAR: 5,
  LITERAL: 6,
  SLASH: 7,
  DOT: 8
}
LAST_OPTIONS_KEY =

:nodoc:

"options".freeze
FILTERED_DEFAULT_PARTS =

:nodoc:

[:controller, :action]
URL_OPTIONS =

:nodoc:

[:protocol, :domain, :host, :port, :subdomain]
VERSION =
"2.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsRoutes

Implementation



118
119
120
# File 'lib/js_routes.rb', line 118

def initialize(options = {})
  @configuration = self.class.configuration.merge(options)
end

Class Method Details

.configurationObject



93
94
95
# File 'lib/js_routes.rb', line 93

def configuration
  @configuration ||= Configuration.new
end

.generate(opts = {}) ⇒ Object



97
98
99
# File 'lib/js_routes.rb', line 97

def generate(opts = {})
  new(opts).generate
end

.generate!(file_name = nil, opts = {}) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/js_routes.rb', line 101

def generate!(file_name=nil, opts = {})
  if file_name.is_a?(Hash)
    opts = file_name
    file_name = opts[:file]
  end
  new(opts).generate!(file_name)
end

.json(string) ⇒ Object



109
110
111
# File 'lib/js_routes.rb', line 109

def json(string)
  ActiveSupport::JSON.encode(string)
end

.setup(&block) ⇒ Object



89
90
91
# File 'lib/js_routes.rb', line 89

def setup(&block)
  configuration.tap(&block) if block
end

Instance Method Details

#generateObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/js_routes.rb', line 122

def generate
  # Ensure routes are loaded. If they're not, load them.
  if named_routes.to_a.empty? && application.respond_to?(:reload_routes!)
    application.reload_routes!
  end

  {
    'GEM_VERSION'         => JsRoutes::VERSION,
    'ROUTES_OBJECT'              => routes_object,
    'RAILS_VERSION'       => ActionPack.version,
    'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,

    'APP_CLASS'           => application.class.to_s,
    'NAMESPACE'           => json(@configuration.namespace),
    'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
    'PREFIX'              => json(@configuration.prefix),
    'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
    'SERIALIZER'          => @configuration.serializer || json(nil),
    'MODULE_TYPE'         => json(@configuration.module_type),
    'WRAPPER'             => @configuration.esm? ? 'const __jsr = ' : '',
  }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
    js.gsub!("RubyVariables.#{key}", value.to_s) ||
      raise("Missing key #{key} in JS template")
  end + routes_export
end

#generate!(file_name = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/js_routes.rb', line 148

def generate!(file_name = nil)
  # Some libraries like Devise do not yet loaded their routes so we will wait
  # until initialization process finish
  # https://github.com/railsware/js-routes/issues/7
  Rails.configuration.after_initialize do
    file_name ||= self.class.configuration['file']
    file_path = Rails.root.join(file_name)
    js_content = generate

    # We don't need to rewrite file if it already exist and have same content.
    # It helps asset pipeline or webpack understand that file wasn't changed.
    next if File.exist?(file_path) && File.read(file_path) == js_content

    File.open(file_path, 'w') do |f|
      f.write js_content
    end
  end
end