Permalink
Browse files

added support url links

  • Loading branch information...
1 parent df1a733 commit ba3d7eb6b66ea7080db28377fc76862ea9eb029a @le0pard le0pard committed Mar 25, 2013
Showing with 71 additions and 16 deletions.
  1. +2 −0 Readme.md
  2. +23 −4 lib/js_routes.rb
  3. +0 −12 lib/routes.js
  4. +46 −0 spec/js_routes/options_spec.rb
View
@@ -61,6 +61,8 @@ Available options:
* Default: blank
* `camel_case` (version >= 0.8.8) - Generate camel case route names.
* Default: false
+* `url_links` (version >= 0.8.9) - Generate additional url links, where url_links value is beginning of url routes (ex: http[s]://example.com).
+ * Default: false
You can generate routes files on the application side like this:
View
@@ -1,4 +1,6 @@
+require 'uri'
require 'js_routes/version'
+
class JsRoutes
#
@@ -12,7 +14,8 @@ class JsRoutes
:exclude => [],
:include => //,
:file => DEFAULT_PATH,
- :prefix => "",
+ :prefix => nil,
+ :url_links => nil,
:camel_case => false,
:default_url_options => {}
}
@@ -29,6 +32,8 @@ class JsRoutes
:DOT => 8
}
+ LAST_OPTIONS_KEY = "options".freeze
+
class Options < Struct.new(*DEFAULTS.keys)
def to_hash
Hash[*members.zip(values).flatten(1)].symbolize_keys
@@ -89,7 +94,7 @@ 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!("PREFIX", @options[:prefix] || "")
js.gsub!("NODE_TYPES", json(NODE_TYPES))
js.gsub!("ROUTES", js_routes)
end
@@ -153,14 +158,28 @@ def build_js(route, parent_route)
optional_parts.push(required_parts.delete :format) if required_parts.include?(:format)
route_name = "#{name.join('_')}_path"
route_name = route_name.camelize(:lower) if true == @options[:camel_case]
+ url_link = generate_url_link(name, route_name, required_parts)
_ = <<-JS.strip!
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
#{route_name}: function(#{build_params(required_parts)}) {
return Utils.build_path(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments);
- }
+ }#{",\n" + url_link if url_link.length > 0}
JS
end
+ 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?
+ url_route_name = "#{name.join('_')}_url"
+ url_route_name = url_route_name.camelize(:lower) if true == @options[:camel_case]
+ _ = <<-JS.strip!
+ #{url_route_name}: function(#{build_params(required_parts)}) {
+ if (!#{LAST_OPTIONS_KEY}){ #{LAST_OPTIONS_KEY} = {}; }
+ return "" + #{@options[:url_links].inspect} + this.#{route_name}(#{build_params(required_parts)});
+ }
+ JS
+ end
+
def json(string)
self.class.json(string)
end
@@ -170,7 +189,7 @@ def build_params required_parts
# prepending each parameter name with underscore
# to prevent conflict with JS reserved words
"_" + name.to_s
- end << "options"
+ end << LAST_OPTIONS_KEY
params.join(", ")
end
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -172,4 +172,50 @@
end
end
end
+
+ describe "url_links" do
+ context "with default option" do
+ let(:_options) { Hash.new }
+ it "should generate only path links" do
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
+ evaljs("Routes.inbox_url").should be_nil
+ end
+ end
+
+ context "with host" do
+ let(:_options) { { :url_links => "http://localhost" } }
+ it "should generate path and url links" do
+ evaljs("Routes.inbox_path").should_not be_nil
+ evaljs("Routes.inbox_url").should_not be_nil
+ evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1)
+ evaljs("Routes.inbox_url(1)").should == "http://localhost#{routes.inbox_path(1)}"
+ end
+ end
+
+ context "with invalid host" do
+ it "should raise error" do
+ expect { JsRoutes.generate({ :url_links => "localhost" }) }.to raise_error RuntimeError
+ end
+ end
+
+ context "with host and camel_case" do
+ let(:_options) { { :camel_case => true, :url_links => "http://localhost" } }
+ it "should generate path and url links" do
+ evaljs("Routes.inboxPath").should_not be_nil
+ evaljs("Routes.inboxUrl").should_not be_nil
+ evaljs("Routes.inboxPath(1)").should == routes.inbox_path(1)
+ evaljs("Routes.inboxUrl(1)").should == "http://localhost#{routes.inbox_path(1)}"
+ end
+ end
+
+ context "with host and prefix" do
+ let(:_options) { { :prefix => "/api", :url_links => "https://example.com" } }
+ it "should generate path and url links" do
+ evaljs("Routes.inbox_path").should_not be_nil
+ evaljs("Routes.inbox_url").should_not be_nil
+ evaljs("Routes.inbox_path(1)").should == "/api#{routes.inbox_path(1)}"
+ evaljs("Routes.inbox_url(1)").should == "https://example.com/api#{routes.inbox_path(1)}"
+ end
+ end
+ end
end

0 comments on commit ba3d7eb

Please sign in to comment.