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: nil,
  camel_case: false,
  default_url_options: {}
}
NODE_TYPES =
{
  GROUP: 1,
  CAT: 2,
  SYMBOL: 3,
  OR: 4,
  STAR: 5,
  LITERAL: 6,
  SLASH: 7,
  DOT: 8
}
LAST_OPTIONS_KEY =
"options".freeze
VERSION =
"0.9.7"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JsRoutes

Implementation



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

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.



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

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



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

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

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



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

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



79
80
81
# File 'lib/js_routes.rb', line 79

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

.optionsObject



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

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

.setup(&block) ⇒ Object



47
48
49
# File 'lib/js_routes.rb', line 47

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

Instance Method Details

#deprecated_default_formatObject



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

def deprecated_default_format
  if @options.key?(:default_format)
    warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead")
    { format: @options[:default_format] }
  else
    {}
  end
end

#generateObject



92
93
94
95
96
97
98
99
# File 'lib/js_routes.rb', line 92

def generate
  js = File.read(File.dirname(__FILE__) + "/routes.js")
  js.gsub!("NAMESPACE", @options[:namespace])
  js.gsub!("DEFAULT_URL_OPTIONS", json(@options[:default_url_options].merge(deprecated_default_format)))
  js.gsub!("PREFIX", @options[:prefix] || "")
  js.gsub!("NODE_TYPES", json(NODE_TYPES))
  js.gsub!("ROUTES", js_routes)
end

#generate!(file_name = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/js_routes.rb', line 110

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