named-routes 0.2.4 → 0.2.5
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.
- data/VERSION +1 -1
- data/lib/named-routes/routes.rb +5 -6
- data/spec/named-routes/routes_spec.rb +36 -11
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
data/lib/named-routes/routes.rb
CHANGED
@@ -16,7 +16,7 @@ module NamedRoutes
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def route(name, definition, include_prefix=true)
|
19
|
-
full_definition = (
|
19
|
+
full_definition = eval(definition, {}, :prefix => include_prefix)
|
20
20
|
_defined_routes[name.to_s] = full_definition
|
21
21
|
define_method name do |*args|
|
22
22
|
self.class.eval(full_definition, [args.first].compact.first || {})
|
@@ -38,12 +38,13 @@ module NamedRoutes
|
|
38
38
|
@_defined_routes ||= {}
|
39
39
|
end
|
40
40
|
|
41
|
-
def eval(definition, params_arg={})
|
41
|
+
def eval(definition, params_arg={}, options={})
|
42
|
+
full_definition = (options[:prefix] && prefix) ? File.join("", prefix, definition) : definition
|
42
43
|
params = Mash.new(params_arg)
|
43
44
|
uri_string = if params.empty?
|
44
|
-
|
45
|
+
full_definition
|
45
46
|
else
|
46
|
-
|
47
|
+
full_definition.split("/").map do |segment|
|
47
48
|
segment_value = segment[/^:(.*)/, 1]
|
48
49
|
segment_value_parts = segment_value.to_s.split(".")
|
49
50
|
segment_name = segment_value_parts[0]
|
@@ -61,8 +62,6 @@ module NamedRoutes
|
|
61
62
|
uri_string
|
62
63
|
end
|
63
64
|
|
64
|
-
# TODO: Create eval_without_prefix
|
65
|
-
|
66
65
|
def escape_params(params={})
|
67
66
|
params.inject({}) do |memo, kv|
|
68
67
|
key, value = kv
|
@@ -69,21 +69,46 @@ module NamedRoutes
|
|
69
69
|
end
|
70
70
|
|
71
71
|
describe ".eval" do
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
describe "params hash" do
|
73
|
+
context "when params hash is not given" do
|
74
|
+
it "returns the definition" do
|
75
|
+
routes_class.eval("/current-user/:category/top-choices").should == "/current-user/:category/top-choices"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when params hash is given" do
|
80
|
+
it "returns the uri with the param replaced with the given param value with additional params added as url params" do
|
81
|
+
schemed_uri_1 = routes_class.current_user_category_top_choices(:category => "cars", :foo => "bar", :baz => {"one" => "two three"})
|
82
|
+
|
83
|
+
path, query = schemed_uri_1.split("?")
|
84
|
+
path.should == "/current-user/cars/top-choices"
|
85
|
+
query.should include("foo=bar")
|
86
|
+
query.should include("baz[one]=two+three")
|
87
|
+
routes_class.eval("/decision-streams/:stream_id", :stream_id => 99).should == "/decision-streams/99"
|
88
|
+
end
|
75
89
|
end
|
76
90
|
end
|
77
91
|
|
78
|
-
|
79
|
-
|
80
|
-
|
92
|
+
describe "options hash" do
|
93
|
+
describe "defaults" do
|
94
|
+
it "defaults to :prefix false" do
|
95
|
+
routes_class.prefix = "/user"
|
96
|
+
routes_class.eval("/current-user/:category/top-choices", {}).should == "/current-user/:category/top-choices"
|
97
|
+
end
|
98
|
+
end
|
81
99
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
100
|
+
context "when :prefix is true" do
|
101
|
+
it "prepends the prefix to the uri" do
|
102
|
+
routes_class.prefix = "/user"
|
103
|
+
routes_class.eval("/current-user/:category/top-choices", {}, :prefix => true).should == "/user/current-user/:category/top-choices"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when :prefix is false" do
|
108
|
+
it "does not prepend the prefix to the uri" do
|
109
|
+
routes_class.prefix = "/user"
|
110
|
+
routes_class.eval("/current-user/:category/top-choices", {}, :prefix => false).should == "/current-user/:category/top-choices"
|
111
|
+
end
|
87
112
|
end
|
88
113
|
end
|
89
114
|
end
|