Class: JsRoutes
- Inherits:
-
Object
- Object
- JsRoutes
- Defined in:
- lib/js_routes.rb,
lib/js_routes/version.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- DEFAULT_PATH =
OPTIONS
File.join('app','assets','javascripts','routes.js')
- DEFAULTS =
{ namespace: "Routes", exclude: [], include: //, file: DEFAULT_PATH, prefix: -> { Rails.application.config.relative_url_root || "" }, url_links: false, camel_case: false, default_url_options: {}, compact: false, serializer: nil, special_options_key: "_options", application: -> { Rails.application } }
- 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.3.1"
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.
- .configuration ⇒ Object
- .generate(opts = {}) ⇒ Object
- .generate!(file_name = nil, opts = {}) ⇒ Object
- .json(string) ⇒ Object
- .options ⇒ Object
- .setup(&block) ⇒ Object
Instance Method Summary collapse
- #generate ⇒ Object
- #generate!(file_name = nil) ⇒ Object
-
#initialize(options = {}) ⇒ JsRoutes
constructor
Implementation.
Constructor Details
#initialize(options = {}) ⇒ JsRoutes
Implementation
120 121 122 |
# File 'lib/js_routes.rb', line 120 def initialize( = {}) @configuration = self.class.configuration.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.
104 105 106 107 108 109 |
# File 'lib/js_routes.rb', line 104 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 |
.configuration ⇒ Object
85 86 87 |
# File 'lib/js_routes.rb', line 85 def configuration @configuration ||= Configuration.new end |
.generate(opts = {}) ⇒ Object
89 90 91 |
# File 'lib/js_routes.rb', line 89 def generate(opts = {}) new(opts).generate end |
.generate!(file_name = nil, opts = {}) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/js_routes.rb', line 93 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
111 112 113 |
# File 'lib/js_routes.rb', line 111 def json(string) ActiveSupport::JSON.encode(string) end |
.options ⇒ Object
80 81 82 83 |
# File 'lib/js_routes.rb', line 80 def ActiveSupport::Deprecation.warn('JsRoutes.options method is deprecated use JsRoutes.configuration instead') configuration end |
.setup(&block) ⇒ Object
76 77 78 |
# File 'lib/js_routes.rb', line 76 def setup(&block) configuration.tap(&block) if block end |
Instance Method Details
#generate ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/js_routes.rb', line 124 def generate # Ensure routes are loaded. If they're not, load them. if named_routes.to_a.empty? && application.respond_to?(:reload_routes!) application.reload_routes! end { "GEM_VERSION" => JsRoutes::VERSION, "ROUTES" => js_routes, "DEPRECATED_BEHAVIOR" => Rails.version < "4", "NODE_TYPES" => json(NODE_TYPES), "APP_CLASS" => application.class.to_s, "NAMESPACE" => json(@configuration.namespace), "DEFAULT_URL_OPTIONS" => json(@configuration.), "PREFIX" => json(@configuration.prefix), "SPECIAL_OPTIONS_KEY" => json(@configuration.), "SERIALIZER" => @configuration.serializer || json(nil), }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)| js.gsub!(key, value.to_s) end end |
#generate!(file_name = nil) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/js_routes.rb', line 147 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.configuration['file'] File.open(Rails.root.join(file_name), 'w') do |f| f.write generate end end end |