Class: JsRoutes

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

Defined Under Namespace

Classes: 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,
  special_options_key: "_options",
}
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, :subdomain]
URL_OPTIONS =
[:protocol, :domain, :host, :port, :subdomain]
VERSION =
"1.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsRoutes

Implementation



99
100
101
# File 'lib/js_routes.rb', line 99

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.



83
84
85
86
87
88
# File 'lib/js_routes.rb', line 83

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



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

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



72
73
74
75
76
77
78
# File 'lib/js_routes.rb', line 72

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



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

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

.optionsObject



57
58
59
60
61
# File 'lib/js_routes.rb', line 57

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

.setup(&block) ⇒ Object



53
54
55
# File 'lib/js_routes.rb', line 53

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

Instance Method Details

#deprecate_url_optionsObject



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

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



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

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] || Rails.application.config.relative_url_root || "",
    "NODE_TYPES"          => json(NODE_TYPES),
    "SERIALIZER"          => @options[:serializer] || json(nil),
    "ROUTES"              => js_routes,
    "SPECIAL_OPTIONS_KEY" => @options[:special_options_key].to_s
  }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
    js.gsub!(key, value)
  end
end

#generate!(file_name = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/js_routes.rb', line 141

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