From aad01b8ae510c1668a2be175a1c282913ce71c51 Mon Sep 17 00:00:00 2001 From: Sergey Pchelincev Date: Mon, 15 Sep 2014 23:55:41 +0300 Subject: [PATCH] add a compact mode In the compact mode path routes generated without `_path` suffix --- Readme.md | 5 ++++- gemfiles/rails32.gemfile | 5 ++++- lib/js_routes.rb | 12 +++++++----- spec/js_routes/options_spec.rb | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index c48dd0f9..0a2ca5cc 100644 --- a/Readme.md +++ b/Readme.md @@ -64,6 +64,9 @@ Available options: * `url_links` (version >= 0.8.9) - Generate `*_url` links (in addition to default `*_path`), where url_links value is beginning of url routes * Example: http[s]://example.com * Default: false +* `compact` (version > 0.9.9) - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled) + * Default: false + * Sample route call when option is set to true: Routes.users() => `/users` ### Very Advanced Setup @@ -145,7 +148,7 @@ Spork.trap_method(JsRoutes, :generate!) ## JS-Routes and heroku -Heroku environment has a specific problems with setup. It is impossible to use asset pipeline in this environtment. You should use "Very Advanced Setup" schema in this case. +Heroku environment has a specific problems with setup. It is impossible to use asset pipeline in this environtment. You should use "Very Advanced Setup" schema in this case. For example create routes.js.erb in assets folder with needed content: diff --git a/gemfiles/rails32.gemfile b/gemfiles/rails32.gemfile index 819eca11..a3dbd705 100644 --- a/gemfiles/rails32.gemfile +++ b/gemfiles/rails32.gemfile @@ -4,5 +4,8 @@ source "http://rubygems.org" gem "railties", "~> 3.2.18" gem "tzinfo" +platforms :ruby do + gem "libv8", "<= 3.16.14.3" +end -gemspec :path=>"../" \ No newline at end of file +gemspec :path=>"../" diff --git a/lib/js_routes.rb b/lib/js_routes.rb index 4c01ba83..a10d1a5a 100644 --- a/lib/js_routes.rb +++ b/lib/js_routes.rb @@ -18,7 +18,8 @@ class JsRoutes prefix: nil, url_links: nil, camel_case: false, - default_url_options: {} + default_url_options: {}, + compact: false } NODE_TYPES = { @@ -154,7 +155,7 @@ def build_js(route, parent_route) parent_spec = parent_route.try(:path).try(:spec) required_parts, optional_parts = route.required_parts.clone, route.optional_parts.clone optional_parts.push(required_parts.delete :format) if required_parts.include?(:format) - route_name = generate_route_name(name) + route_name = generate_route_name(name, (:path unless @options[:compact])) url_link = generate_url_link(name, route_name, required_parts) _ = <<-JS.strip! // #{name.join('.')} => #{parent_spec}#{route.path.spec} @@ -168,14 +169,15 @@ def generate_url_link(name, route_name, required_parts) return "" unless @options[:url_links] raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::regexp(%w(http https))).nil? _ = <<-JS.strip! - #{generate_route_name(name, true)}: function(#{build_params(required_parts)}) { + #{generate_route_name(name, :url)}: function(#{build_params(required_parts)}) { return "" + #{@options[:url_links].inspect} + this.#{route_name}(#{build_params(required_parts)}); } JS end - def generate_route_name(name, is_url = false) - route_name = "#{name.join('_')}_#{is_url ? "url" : "path"}" + def generate_route_name(name, suffix) + route_name = name.join('_') + route_name << "_#{ suffix }" if suffix @options[:camel_case] ? route_name.camelize(:lower) : route_name end diff --git a/spec/js_routes/options_spec.rb b/spec/js_routes/options_spec.rb index fb64e97b..562b84b5 100644 --- a/spec/js_routes/options_spec.rb +++ b/spec/js_routes/options_spec.rb @@ -275,4 +275,20 @@ end end end + + describe "when the compact mode is enabled" do + let(:_options) { { :compact => true } } + it "should avoid a path suffix" do + expect(evaljs("Routes.inboxes()")).to eq(routes.inboxes_path()) + expect(evaljs("Routes.inbox(2)")).to eq(routes.inbox_path(2)) + end + + context "with url links" do + let(:_options) { { :compact => true, :url_links => "http://localhost" } } + it "should not strip urls" do + expect(evaljs("Routes.inbox(1)")).to eq(routes.inbox_path(1)) + expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}") + end + end + end end