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: Engine, Options

Constant Summary collapse

DEFAULT_PATH =

OPTIONS

File.join('app','assets','javascripts','routes.js')
DEFAULTS =
{
  namespace: "Routes",
  exclude: [],
  include: //,
  file: DEFAULT_PATH,
  prefix: nil,
  url_links: false,
  camel_case: false,
  default_url_options: {},
  compact: false,
  serializer: nil
}
NODE_TYPES =
{
  GROUP: 1,
  CAT: 2,
  SYMBOL: 3,
  OR: 4,
  STAR: 5,
  LITERAL: 6,
  SLASH: 7,
  DOT: 8
}
LAST_OPTIONS_KEY =
"options".freeze
FILTERED_DEFAULT_PARTS =
[:controller, :action]
SPROCKETS3 =
Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0.0')
VERSION =
"1.2.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsRoutes

Implementation



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

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

Class Method Details

.assert_usable_configuration!Object

Under rails 3.1.1 and higher, perform a check to ensure that the full environment will be available during asset compilation. This is required to ensure routes are loaded.



81
82
83
84
85
86
# File 'lib/js_routes.rb', line 81

def assert_usable_configuration!
  if 3 == Rails::VERSION::MAJOR && !Rails.application.config.assets.initialize_on_precompile
    raise("Cannot precompile js-routes unless environment is initialized. Please set config.assets.initialize_on_precompile to true.")
  end
  true
end

.generate(opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/js_routes.rb', line 61

def generate(opts = {})
  # Ensure routes are loaded. If they're not, load them.
  if Rails.application.routes.named_routes.to_a.empty?
    Rails.application.reload_routes!
  end

  new(opts).generate
end

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



70
71
72
73
74
75
76
# File 'lib/js_routes.rb', line 70

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



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

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

.optionsObject



55
56
57
58
59
# File 'lib/js_routes.rb', line 55

def options
  @options ||= Options.new.tap do |opts|
    DEFAULTS.each_pair {|k,v| opts[k] = v}
  end
end

.setup(&block) ⇒ Object



51
52
53
# File 'lib/js_routes.rb', line 51

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

Instance Method Details

#deprecate_url_optionsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/js_routes.rb', line 116

def deprecate_url_options
  result = {}
  if @options.key?(:default_format)
    warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead")
    result.merge!(  format: @options[:default_format]  )
  end
  if @options[:url_links].is_a?(String)
    ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')

    raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::Parser.new.make_regexp(%w(http https))).nil?
    uri = URI.parse(@options[:url_links])
    default_port = uri.scheme == "https" ? 443 : 80
    port = uri.port == default_port ? nil : uri.port
    result.merge!(
      host: uri.host,
      port: port,
      protocol: uri.scheme,
    )
  end
  result
end

#generateObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/js_routes.rb', line 101

def generate
  {
    "GEM_VERSION"         => JsRoutes::VERSION,
    "APP_CLASS"           => Rails.application.class.to_s,
    "NAMESPACE"           => @options[:namespace],
    "DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)),
    "PREFIX"              => @options[:prefix] || "",
    "NODE_TYPES"          => json(NODE_TYPES),
    "SERIALIZER"          => @options[:serializer] || "null",
    "ROUTES"              => js_routes,
  }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
    js.gsub!(key, value)
  end
end

#generate!(file_name = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/js_routes.rb', line 138

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.options['file']
    File.open(Rails.root.join(file_name || DEFAULT_PATH), 'w') do |f|
      f.write generate
    end
  end
end