dynamic_router 0.0.9 → 0.0.10
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.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/dynamic_router/version.rb +1 -1
- data/lib/dynamic_router.rb +3 -2
- data/spec/dynamic_router_spec.rb +5 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 543ab20c6bee3d7923fe190569a070b6a8f379d2
|
4
|
+
data.tar.gz: 661d66a572b0e9898e38249fceba62272e69a6e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 377572337cef53d523111cab62d6cafecd352c3715cb0fc03b8f417836081a11a4d188b9808c207db2929379c538545e0043eb44886e580b673b808cfc24a020
|
7
|
+
data.tar.gz: a885e7ca61004c097227f3b36a96daa1521e6281ceeeaa0389247368a27ce9c35141d0d0bf875f4451303be5f5186fb13a5bbe065e90686e4a321ecdde43d4cd
|
data/README.md
CHANGED
@@ -28,7 +28,7 @@ Suppose you want to create a friendly URL for a resource based on fields of an e
|
|
28
28
|
|
29
29
|
To create a route to a resource using the field 'url' as URL, add the following line to your routes.rb:
|
30
30
|
|
31
|
-
DynamicRouter.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method"
|
31
|
+
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method"
|
32
32
|
|
33
33
|
After this when you create models like:
|
34
34
|
|
@@ -39,11 +39,11 @@ The dynamic router will create the routes "/abc" and "/123" mapping to DummyCont
|
|
39
39
|
|
40
40
|
You can pass the desired HTTP method also:
|
41
41
|
|
42
|
-
DynamicRouter.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :method => :post
|
42
|
+
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :method => :post
|
43
43
|
|
44
44
|
And can specify default values to be passed, like:
|
45
45
|
|
46
|
-
DynamicRouter.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :defaults => {:some_value => Proc.new {|example| example.default_field}}
|
46
|
+
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :defaults => {:some_value => Proc.new {|example| example.default_field}}
|
47
47
|
|
48
48
|
The dynamic router will map ALL records of the model on the startup and will create an after_save hook to create new routes as the models are created.
|
49
49
|
|
data/lib/dynamic_router.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "dynamic_router/version"
|
2
2
|
|
3
3
|
module DynamicRouter
|
4
|
-
|
5
|
-
def has_dynamic_route_for(klass, url, target, options = {})
|
4
|
+
class Router
|
5
|
+
def self.has_dynamic_route_for(klass, url, target, options = {})
|
6
6
|
route_method = options[:method] || :get
|
7
7
|
|
8
8
|
if ActiveRecord::Base.connection.table_exists? klass.table_name
|
@@ -35,4 +35,5 @@ module DynamicRouter
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
|
+
end
|
38
39
|
end
|
data/spec/dynamic_router_spec.rb
CHANGED
@@ -18,13 +18,13 @@ RSpec.describe DynamicRouter do
|
|
18
18
|
expect(connection).to receive(:table_exists?).with(Example.table_name)
|
19
19
|
|
20
20
|
Rails.application.routes.draw do
|
21
|
-
DynamicRouter.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
|
21
|
+
DynamicRouter::Router.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should create a get route with the supplied url" do
|
26
26
|
Rails.application.routes.draw do
|
27
|
-
DynamicRouter.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
|
27
|
+
DynamicRouter::Router.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
|
28
28
|
end
|
29
29
|
|
30
30
|
expect(Rails.application.routes.recognize_path('/path_a/path_a_a', :method => :get))
|
@@ -33,7 +33,7 @@ RSpec.describe DynamicRouter do
|
|
33
33
|
|
34
34
|
it "should accept the method option" do
|
35
35
|
Rails.application.routes.draw do
|
36
|
-
DynamicRouter.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method", :method => :post)
|
36
|
+
DynamicRouter::Router.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method", :method => :post)
|
37
37
|
end
|
38
38
|
|
39
39
|
expect(Rails.application.routes.recognize_path('/path_a/path_a_a', :method => :post))
|
@@ -41,7 +41,7 @@ RSpec.describe DynamicRouter do
|
|
41
41
|
|
42
42
|
it "should accept the defaults option" do
|
43
43
|
Rails.application.routes.draw do
|
44
|
-
DynamicRouter.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method", :defaults => {:default_value => Proc.new {|example| example.default_field}})
|
44
|
+
DynamicRouter::Router.has_dynamic_route_for(Example, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method", :defaults => {:default_value => Proc.new {|example| example.default_field}})
|
45
45
|
end
|
46
46
|
|
47
47
|
expect(Rails.application.routes.routes.named_routes["path_a_path_a_a"].defaults).to match a_hash_including(:default_value => "default_value")
|
@@ -55,7 +55,7 @@ RSpec.describe DynamicRouter do
|
|
55
55
|
|
56
56
|
it "should not create the route if the url is blank" do
|
57
57
|
Rails.application.routes.draw do
|
58
|
-
DynamicRouter.has_dynamic_route_for(Example, Proc.new {|example| ""}, "dummy#dummy_method")
|
58
|
+
DynamicRouter::Router.has_dynamic_route_for(Example, Proc.new {|example| ""}, "dummy#dummy_method")
|
59
59
|
end
|
60
60
|
|
61
61
|
expect(Rails.application.routes.routes.named_routes).to be_empty
|