Skip to content

Commit

Permalink
Merge pull request #125 from jalkoby/master
Browse files Browse the repository at this point in the history
Add the compact mode
  • Loading branch information
bogdan committed Sep 17, 2014
2 parents 5f31f20 + aad01b8 commit b2d9263
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down
5 changes: 4 additions & 1 deletion gemfiles/rails32.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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=>"../"
gemspec :path=>"../"
12 changes: 7 additions & 5 deletions lib/js_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class JsRoutes
prefix: nil,
url_links: nil,
camel_case: false,
default_url_options: {}
default_url_options: {},
compact: false
}

NODE_TYPES = {
Expand Down Expand Up @@ -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}
Expand All @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/js_routes/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b2d9263

Please sign in to comment.