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: 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
- VERSION =
"1.2.0"
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
96 97 98 |
# File 'lib/js_routes.rb', line 96 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.
80 81 82 83 84 85 |
# File 'lib/js_routes.rb', line 80 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
60 61 62 63 64 65 66 67 |
# File 'lib/js_routes.rb', line 60 def generate(opts = {}) # Ensure routes are loaded. If they're not, load them. if Rails.application.routes.named_routes.routes.keys.empty? Rails.application.reload_routes! end new(opts).generate end |
.generate!(file_name = nil, opts = {}) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/js_routes.rb', line 69 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
87 88 89 |
# File 'lib/js_routes.rb', line 87 def json(string) ActiveSupport::JSON.encode(string) end |
.options ⇒ Object
54 55 56 57 58 |
# File 'lib/js_routes.rb', line 54 def @options ||= Options.new.tap do |opts| DEFAULTS.each_pair {|k,v| opts[k] = v} end end |
.setup(&block) ⇒ Object
50 51 52 |
# File 'lib/js_routes.rb', line 50 def setup(&block) .tap(&block) if block end |
Instance Method Details
#deprecated_default_format ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/js_routes.rb', line 112 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
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/js_routes.rb', line 100 def generate js = File.read(File.dirname(__FILE__) + "/routes.js") js.gsub!("GEM_VERSION", JsRoutes::VERSION) js.gsub!("APP_CLASS", Rails.application.class.to_s) 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!("SERIALIZER", @options[:serializer] || "null") js.gsub!("ROUTES", js_routes) end |
#generate!(file_name = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/js_routes.rb', line 121 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 |