Skip to content

Commit

Permalink
Merge pull request #163 from coletivoEITA/master
Browse files Browse the repository at this point in the history
Support for nested optional parameters (Fix #162)
  • Loading branch information
bogdan committed Sep 14, 2015
2 parents 2fb4099 + 9c8b6e8 commit 0cd53dc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Based on Rails routes of APP_CLASS
case NodeTypes.CAT:
left_part = this.visit(left, parameters, optional);
right_part = this.visit(right, parameters, optional);
if (optional && !(left_part && right_part)) {
if (optional && (((left[0] === NodeTypes.SYMBOL || left[0] === NodeTypes.CAT) && !left_part) || ((right[0] === NodeTypes.SYMBOL || right[0] === NodeTypes.CAT) && !right_part))) {
return "";
}
return "" + left_part + right_part;
Expand Down
3 changes: 2 additions & 1 deletion lib/routes.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ Utils =
when NodeTypes.CAT
left_part = @visit(left, parameters, optional)
right_part = @visit(right, parameters, optional)
return "" if optional and not (left_part and right_part)
return "" if optional and (((left[0] == NodeTypes.SYMBOL or left[0] == NodeTypes.CAT) and not left_part) or
((right[0] == NodeTypes.SYMBOL or right[0] == NodeTypes.CAT) and not right_part))
"#{left_part}#{right_part}"
when NodeTypes.SYMBOL
value = parameters[left]
Expand Down
5 changes: 5 additions & 0 deletions spec/js_routes/rails_routes_compatibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@
expect(evaljs("Routes.things_path({optional_id: 5})")).to eq(routes.things_path(:optional_id => 5))
end

context "on nested optional parts" do
it "should include everything that is not optional" do
expect(evaljs("Routes.classic_path({controller: 'classic', action: 'edit'})")).to eq(routes.classic_path(controller: :classic, action: :edit))
end
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def draw_routes
resources :things
end

get "/:controller(/:action(/:id))" => "classic#classic", :as => :classic

get "/other_optional/(:optional_id)" => "foo#foo", :as => :foo

get 'books/*section/:title' => 'books#show', :as => :book
Expand Down

0 comments on commit 0cd53dc

Please sign in to comment.