js-routes 1.2.9 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Readme.md +20 -0
- data/lib/js_routes.rb +4 -2
- data/lib/js_routes/version.rb +1 -1
- data/lib/routes.js +8 -4
- data/lib/routes.js.coffee +5 -3
- data/spec/js_routes/options_spec.rb +10 -0
- data/spec/js_routes/rails_routes_compatibility_spec.rb +21 -9
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 1b2b15773ea1f4c5fd8870ccda544a7386e28108
         | 
| 4 | 
            +
              data.tar.gz: ffd6b967fd16e89a1d976098da3fa00109924319
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6e5b2c94c2c60924ab690669b61c95075e7bcc3ef576cd8284ee1a7d30aa63427a8826079eece47629cfe31bcafbde00ee63bae5b9f0d39f91a505ae4561c2f6
         | 
| 7 | 
            +
              data.tar.gz: 69f1daf6f674da744d13dd6440af0a70399cfdfe3ecd29a3203c6ec9447e5367baaad19b998c85403c982cfbc6dc04e61f16a55ce796f493c47d69a43932265f
         | 
    
        data/Readme.md
    CHANGED
    
    | @@ -71,6 +71,10 @@ Available options: | |
| 71 71 | 
             
              * Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
         | 
| 72 72 | 
             
              * Example: `MyApp.custom_serialize` - use completely custom serializer of your application.
         | 
| 73 73 |  | 
| 74 | 
            +
            * `special_options_key` - a special key that helps js-routes to destinguish serialized model from options hash
         | 
| 75 | 
            +
              * This options is required because JS doesn't provide a difference between an object and a hash
         | 
| 76 | 
            +
              * Default: `_options`
         | 
| 77 | 
            +
             | 
| 74 78 | 
             
            ### Very Advanced Setup
         | 
| 75 79 |  | 
| 76 80 | 
             
            In case you need multiple route files for different parts of your application, you have to create the files manually.
         | 
| @@ -168,6 +172,22 @@ Routes.users_path.required_params // => [] | |
| 168 172 | 
             
            Routes.user_path.required_params // => ['id']
         | 
| 169 173 | 
             
            ```
         | 
| 170 174 |  | 
| 175 | 
            +
             | 
| 176 | 
            +
            ## Rails Compatibilities
         | 
| 177 | 
            +
             | 
| 178 | 
            +
            JsRoutes ties to be as close as possible to rails behaviour in all aspects of routing API.
         | 
| 179 | 
            +
            Please make and issue in case of any incomtibilities found outside of described below.
         | 
| 180 | 
            +
             | 
| 181 | 
            +
            ### Object and Hash distinction issue
         | 
| 182 | 
            +
             | 
| 183 | 
            +
            Sometimes the destinction between JS Hash and Object can not be found by js-routes.
         | 
| 184 | 
            +
            In this case you would need to pass a special key to help:
         | 
| 185 | 
            +
             | 
| 186 | 
            +
            ``` js
         | 
| 187 | 
            +
            Routes.company_project_path({company_id: 1, id: 2, _options: true}) // => "/companies/1/projects/2"
         | 
| 188 | 
            +
            ```
         | 
| 189 | 
            +
             | 
| 190 | 
            +
             | 
| 171 191 | 
             
            ## What about security?
         | 
| 172 192 |  | 
| 173 193 | 
             
            js-routes itself do not have security holes. It makes URLs
         | 
    
        data/lib/js_routes.rb
    CHANGED
    
    | @@ -20,7 +20,8 @@ class JsRoutes | |
| 20 20 | 
             
                camel_case: false,
         | 
| 21 21 | 
             
                default_url_options: {},
         | 
| 22 22 | 
             
                compact: false,
         | 
| 23 | 
            -
                serializer: nil
         | 
| 23 | 
            +
                serializer: nil,
         | 
| 24 | 
            +
                special_options_key: "_options",
         | 
| 24 25 | 
             
              }
         | 
| 25 26 |  | 
| 26 27 | 
             
              NODE_TYPES = {
         | 
| @@ -107,8 +108,9 @@ class JsRoutes | |
| 107 108 | 
             
                  "DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)),
         | 
| 108 109 | 
             
                  "PREFIX"              => @options[:prefix] || Rails.application.config.relative_url_root || "",
         | 
| 109 110 | 
             
                  "NODE_TYPES"          => json(NODE_TYPES),
         | 
| 110 | 
            -
                  "SERIALIZER"          => @options[:serializer] ||  | 
| 111 | 
            +
                  "SERIALIZER"          => @options[:serializer] || json(nil),
         | 
| 111 112 | 
             
                  "ROUTES"              => js_routes,
         | 
| 113 | 
            +
                  "SPECIAL_OPTIONS_KEY" => @options[:special_options_key].to_s
         | 
| 112 114 | 
             
                }.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
         | 
| 113 115 | 
             
                  js.gsub!(key, value)
         | 
| 114 116 | 
             
                end
         | 
    
        data/lib/js_routes/version.rb
    CHANGED
    
    
    
        data/lib/routes.js
    CHANGED
    
    | @@ -4,7 +4,7 @@ Based on Rails routes of APP_CLASS | |
| 4 4 | 
             
             */
         | 
| 5 5 |  | 
| 6 6 | 
             
            (function() {
         | 
| 7 | 
            -
              var NodeTypes, ParameterMissing, ReservedOptions, Utils, createGlobalJsRoutesObject, defaults, root,
         | 
| 7 | 
            +
              var NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, createGlobalJsRoutesObject, defaults, root,
         | 
| 8 8 | 
             
                hasProp = {}.hasOwnProperty,
         | 
| 9 9 | 
             
                slice = [].slice;
         | 
| 10 10 |  | 
| @@ -23,6 +23,8 @@ Based on Rails routes of APP_CLASS | |
| 23 23 |  | 
| 24 24 | 
             
              NodeTypes = NODE_TYPES;
         | 
| 25 25 |  | 
| 26 | 
            +
              SpecialOptionsKey = "SPECIAL_OPTIONS_KEY";
         | 
| 27 | 
            +
             | 
| 26 28 | 
             
              ReservedOptions = ['anchor', 'trailing_slash', 'host', 'port', 'protocol'];
         | 
| 27 29 |  | 
| 28 30 | 
             
              Utils = {
         | 
| @@ -86,16 +88,18 @@ Based on Rails routes of APP_CLASS | |
| 86 88 | 
             
                  return path.join("://");
         | 
| 87 89 | 
             
                },
         | 
| 88 90 | 
             
                extract_options: function(number_of_params, args) {
         | 
| 89 | 
            -
                  var last_el;
         | 
| 91 | 
            +
                  var last_el, options;
         | 
| 90 92 | 
             
                  last_el = args[args.length - 1];
         | 
| 91 93 | 
             
                  if ((args.length > number_of_params && last_el === void 0) || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.looks_like_serialized_model(last_el))) {
         | 
| 92 | 
            -
                     | 
| 94 | 
            +
                    options = args.pop() || {};
         | 
| 95 | 
            +
                    delete options[SpecialOptionsKey];
         | 
| 96 | 
            +
                    return options;
         | 
| 93 97 | 
             
                  } else {
         | 
| 94 98 | 
             
                    return {};
         | 
| 95 99 | 
             
                  }
         | 
| 96 100 | 
             
                },
         | 
| 97 101 | 
             
                looks_like_serialized_model: function(object) {
         | 
| 98 | 
            -
                  return "id" in object || "to_param" in object;
         | 
| 102 | 
            +
                  return !object[SpecialOptionsKey] && ("id" in object || "to_param" in object);
         | 
| 99 103 | 
             
                },
         | 
| 100 104 | 
             
                path_identifier: function(object) {
         | 
| 101 105 | 
             
                  var property;
         | 
    
        data/lib/routes.js.coffee
    CHANGED
    
    | @@ -12,6 +12,7 @@ defaults = | |
| 12 12 | 
             
              default_url_options: DEFAULT_URL_OPTIONS
         | 
| 13 13 |  | 
| 14 14 | 
             
            NodeTypes = NODE_TYPES
         | 
| 15 | 
            +
            SpecialOptionsKey = "SPECIAL_OPTIONS_KEY"
         | 
| 15 16 |  | 
| 16 17 | 
             
            ReservedOptions = [
         | 
| 17 18 | 
             
              'anchor'
         | 
| @@ -64,13 +65,14 @@ Utils = | |
| 64 65 | 
             
              extract_options: (number_of_params, args) ->
         | 
| 65 66 | 
             
                last_el = args[args.length - 1]
         | 
| 66 67 | 
             
                if (args.length > number_of_params and last_el == undefined) or(last_el? and "object" is @get_object_type(last_el) and !@looks_like_serialized_model(last_el))
         | 
| 67 | 
            -
                  args.pop() || {}
         | 
| 68 | 
            +
                  options = args.pop() || {}
         | 
| 69 | 
            +
                  delete options[SpecialOptionsKey]
         | 
| 70 | 
            +
                  options
         | 
| 68 71 | 
             
                else
         | 
| 69 72 | 
             
                  {}
         | 
| 70 73 |  | 
| 71 74 | 
             
              looks_like_serialized_model: (object) ->
         | 
| 72 | 
            -
                 | 
| 73 | 
            -
                "id" of object or "to_param" of object
         | 
| 75 | 
            +
                !object[SpecialOptionsKey] and ("id" of object or "to_param" of object)
         | 
| 74 76 |  | 
| 75 77 |  | 
| 76 78 | 
             
              path_identifier: (object) ->
         | 
| @@ -476,4 +476,14 @@ describe JsRoutes, "options" do | |
| 476 476 | 
             
                  end
         | 
| 477 477 | 
             
                end
         | 
| 478 478 | 
             
              end
         | 
| 479 | 
            +
             | 
| 480 | 
            +
              describe "special_options_key" do
         | 
| 481 | 
            +
                let(:_options) { { special_options_key: :__options__ } }
         | 
| 482 | 
            +
                it "can be redefined" do
         | 
| 483 | 
            +
                  expect {
         | 
| 484 | 
            +
                    expect(evaljs("Routes.inbox_message_path({inbox_id: 1, id: 2, _options: true})")).to eq("")
         | 
| 485 | 
            +
                  }.to raise_error(js_error_class)
         | 
| 486 | 
            +
                  expect(evaljs("Routes.inbox_message_path({inbox_id: 1, id: 2, __options__: true})")).to eq(routes.inbox_message_path(inbox_id: 1, id: 2))
         | 
| 487 | 
            +
                end
         | 
| 488 | 
            +
              end
         | 
| 479 489 | 
             
            end
         | 
| @@ -18,14 +18,6 @@ describe JsRoutes, "compatibility with Rails"  do | |
| 18 18 | 
             
                expect(evaljs("Routes.inbox_path(0)")).to eq(routes.inbox_path(0))
         | 
| 19 19 | 
             
              end
         | 
| 20 20 |  | 
| 21 | 
            -
              it "should support 0 as a to_param option" do
         | 
| 22 | 
            -
                expect(evaljs("Routes.inbox_path({to_param: 0})")).to eq(routes.inbox_path(0))
         | 
| 23 | 
            -
              end
         | 
| 24 | 
            -
             | 
| 25 | 
            -
              it "should support 0 as an id option" do
         | 
| 26 | 
            -
                expect(evaljs("Routes.inbox_path({id: 0})")).to eq(routes.inbox_path(0))
         | 
| 27 | 
            -
              end
         | 
| 28 | 
            -
             | 
| 29 21 | 
             
              it "should generate nested routing with one parameter" do
         | 
| 30 22 | 
             
                expect(evaljs("Routes.inbox_messages_path(1)")).to eq(routes.inbox_messages_path(1))
         | 
| 31 23 | 
             
              end
         | 
| @@ -253,7 +245,27 @@ describe JsRoutes, "compatibility with Rails"  do | |
| 253 245 |  | 
| 254 246 | 
             
              context "when arguments are objects" do
         | 
| 255 247 |  | 
| 256 | 
            -
                let(: | 
| 248 | 
            +
                let(:klass) { Struct.new(:id, :to_param) }
         | 
| 249 | 
            +
                let(:inbox) { klass.new(1,"my") }
         | 
| 250 | 
            +
             | 
| 251 | 
            +
                it "should support 0 as a to_param option" do
         | 
| 252 | 
            +
                  expect(evaljs("Routes.inbox_path({to_param: 0})")).to eq(routes.inbox_path(0))
         | 
| 253 | 
            +
                end
         | 
| 254 | 
            +
             | 
| 255 | 
            +
                it "should check for options special key" do
         | 
| 256 | 
            +
                  expect(evaljs("Routes.inbox_path({id: 7, q: 'hello', _options: true})")).to eq(routes.inbox_path(id: 7, q: 'hello'))
         | 
| 257 | 
            +
                  expect {
         | 
| 258 | 
            +
                    evaljs("Routes.inbox_path({to_param: 7, _options: true})")
         | 
| 259 | 
            +
                  }.to raise_error(js_error_class)
         | 
| 260 | 
            +
                  expect(evaljs("Routes.inbox_message_path(5, {id: 7, q: 'hello', _options: true})")).to eq(routes.inbox_message_path(5, id: 7, q: 'hello'))
         | 
| 261 | 
            +
                end
         | 
| 262 | 
            +
             | 
| 263 | 
            +
                it "should check for options special key" do
         | 
| 264 | 
            +
                end
         | 
| 265 | 
            +
             | 
| 266 | 
            +
                it "should support 0 as an id option" do
         | 
| 267 | 
            +
                  expect(evaljs("Routes.inbox_path({id: 0})")).to eq(routes.inbox_path(0))
         | 
| 268 | 
            +
                end
         | 
| 257 269 |  | 
| 258 270 | 
             
                it "should use id property of the object in path" do
         | 
| 259 271 | 
             
                  expect(evaljs("Routes.inbox_path({id: 1})")).to eq(routes.inbox_path(1))
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: js-routes
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1. | 
| 4 | 
            +
              version: 1.3.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Bogdan Gusiev
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-08- | 
| 11 | 
            +
            date: 2016-08-17 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: railties
         |