Class: JsRoutes
- Inherits:
-
Object
- Object
- JsRoutes
- Defined in:
- lib/js_routes.rb,
lib/js_routes/engine.rb,
lib/js_routes/version.rb
Defined Under Namespace
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.9"
Class Method Summary collapse
-
.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.
- .generate(opts = {}) ⇒ Object
- .generate!(file_name = nil, opts = {}) ⇒ Object
- .json(string) ⇒ Object
- .options ⇒ Object
- .setup(&block) ⇒ Object
Instance Method Summary collapse
- #deprecated_default_format ⇒ Object
- #generate ⇒ Object
- #generate!(file_name = nil) ⇒ Object
-
#initialize(options = {}) ⇒ JsRoutes
constructor
Implementation.
Constructor Details
#initialize(options = {}) ⇒ JsRoutes
Implementation
89 90 91 |
# File 'lib/js_routes.rb', line 89 def initialize( = {}) @options = self.class..to_hash.merge() 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.
73 74 75 76 77 78 |
# File 'lib/js_routes.rb', line 73 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
58 59 60 |
# File 'lib/js_routes.rb', line 58 def generate(opts = {}) new(opts).generate end |
.generate!(file_name = nil, opts = {}) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/js_routes.rb', line 62 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
80 81 82 |
# File 'lib/js_routes.rb', line 80 def json(string) ActiveSupport::JSON.encode(string) end |
.options ⇒ Object
52 53 54 55 56 |
# File 'lib/js_routes.rb', line 52 def @options ||= Options.new.tap do |opts| DEFAULTS.each_pair {|k,v| opts[k] = v} end end |
.setup(&block) ⇒ Object
48 49 50 |
# File 'lib/js_routes.rb', line 48 def setup(&block) .tap(&block) if block end |
Instance Method Details
#deprecated_default_format ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/js_routes.rb', line 102 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 |
#generate ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/js_routes.rb', line 93 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
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/js_routes.rb', line 111 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.['file'] File.open(Rails.root.join(file_name || DEFAULT_PATH), 'w') do |f| f.write generate end end end |