routes_revealer 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f0aa69a278eaaa4d9705bb26e555f906de653cb
4
- data.tar.gz: e21d078145df3484198d0ddc99be1dbfd35b2e78
3
+ metadata.gz: 2fbe87a8116015c5bcaca20233cad3b9428396d1
4
+ data.tar.gz: 5fe2bf4b73c80e0a75751f9e8cf8dca2a368745d
5
5
  SHA512:
6
- metadata.gz: 3a0ed2a2c9ade4abc71e453db1b7213468a2085b87e6e8012ffa954ee4bf69c44daf1748a5d2fa64098a6bd8e56a0bbac755d54ce43910ce2fb06efd1e39aba6
7
- data.tar.gz: d3011b3a3d64b57841c3aba9027598a585521164f845437f64b93438d721fbaffd1d29cb843848f0baa307055da8701e2c794b9c0adf44e5b0dfc2556ca54fe1
6
+ metadata.gz: bf9857a1e74a4fe19721441e8d0e4dbc433d9ce2e0f688849ed5ac92273061a020a63786ee54847c46d3d3235f58658acf3a9b68a4c023b6d013a1d85ea39dc1
7
+ data.tar.gz: 7fe56f3fb5bbbd71c472fb35b655ffd539a5757f48276dce53a807f3f579f987cd3d0da6d93452f87aa7d99c3b142ff5c2e84ee378263a2da47b7e2d55161b71
@@ -3,16 +3,7 @@ module RoutesRevealer
3
3
  skip_filter(*_process_action_callbacks.map(&:filter))
4
4
 
5
5
  def index
6
- output = []
7
- Rails.application.routes.routes.each do |r|
8
- route_string = r.path.spec.to_s.sub('(.:format)', '')
9
- unless ignore_route?(route_string)
10
- output.push route_string if r.defaults[:controller]
11
- end
12
- end
13
-
14
- output.push Rails.application.class.config.assets.prefix
15
-
6
+ output = map_routes(Rails.application.routes.routes).flatten.compact.uniq
16
7
  render json: output
17
8
  end
18
9
 
@@ -21,5 +12,28 @@ module RoutesRevealer
21
12
  def ignore_route?(route_string)
22
13
  route_string =~ /(\/[0-9]{3})|(^\/routes)|(^\/rails)/
23
14
  end
15
+
16
+ def map_routes(routes, prepend='')
17
+ prepend = '' if prepend == '/'
18
+ route_hash_array(routes).map do |route_hash|
19
+ if route_hash[:reqs].include?("::Engine")
20
+ map_routes(Module.const_get(route_hash[:reqs]).routes.routes, route_hash[:path])
21
+ else
22
+ "#{prepend}#{route_hash[:path]}" unless ignore_route?(route_hash[:path])
23
+ end
24
+ end
25
+ end
26
+
27
+ def route_hash_array(routes)
28
+ routes.map do |route|
29
+ ActionDispatch::Routing::RouteWrapper.new(route)
30
+ end.map do |route|
31
+ {
32
+ path: route.path.sub('(.:format)', ''),
33
+ verb: route.verb,
34
+ reqs: route.reqs
35
+ }
36
+ end
37
+ end
24
38
  end
25
39
  end
@@ -1,3 +1,3 @@
1
1
  module RoutesRevealer
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -3,29 +3,67 @@ require 'spec_helper'
3
3
  describe RoutesRevealer::RoutesController do
4
4
  describe '#index' do
5
5
 
6
- let(:good_defaults) { { controller: true } }
7
- let(:bad_defaults) { { controller: nil } }
8
- let(:good_route1) { double("good_route1", path: double("path", spec: "/good/route1(.:format)"), defaults: good_defaults) }
9
- let(:good_route2) { double("good_route2", path: double("path", spec: "/good/route2(.:format)"), defaults: good_defaults) }
10
- let(:bad_route1) { double("bad_route1", path: double("path", spec: "/404(.:format)"), defaults: good_defaults) }
11
- let(:bad_route2) { double("bad_route2", path: double("path", spec: "/rails(.:format)"), defaults: good_defaults) }
12
- let(:bad_route3) { double("bad_route3", path: double("path", spec: "/routes(.:format)"), defaults: good_defaults) }
13
- let(:bad_route4) { double("bad_route4", path: double("path", spec: "/(.:format)"), defaults: good_defaults) }
14
- let(:bad_route5) { double("bad_route5", path: double("path", spec: "/asdf(.:format)"), defaults: bad_defaults) }
15
- let(:routes) { [good_route1, good_route2, bad_route1, bad_route2, bad_route3, bad_route4] }
6
+ let(:this_engine_string) { "This::Engine" }
7
+ let(:that_engine_string) { "That::Engine" }
8
+
9
+ let(:route1) { double("route1", path: "/1", verb: "GET", reqs: "controller#1") }
10
+ let(:route2) { double("route2", path: "/2", verb: "POST", reqs: "controller#2") }
11
+ let(:route3) { double("route3", path: "/3", verb: "PUT", reqs: "controller#3") }
12
+ let(:route4) { double("route4", path: "/4", verb: "DELETE", reqs: "controller#4") }
13
+ let(:route5) { double("route5", path: "/", verb: "GET", reqs: this_engine_string) }
14
+ let(:route6) { double("route6", path: "/that", verb: "GET", reqs: that_engine_string) }
15
+ let(:bad_route7) { double("bad_route7", path: "/routes", verb: "HEAD", reqs: "controller#route7") }
16
+ let(:bad_route8) { double("bad_route7", path: "/rails", verb: "TRACE", reqs: "controller#route8") }
17
+
18
+ let(:this_route1) { double("this_route1", path: "/t1", verb: "RAMBO", reqs: "this_controller#1") }
19
+ let(:this_route2) { double("this_route2", path: "/t2", verb: "ALWAYS", reqs: "this_controller#2") }
20
+ let(:that_route1) { double("that_route1", path: "/1", verb: "SHOOTS", reqs: "that_controller#1") }
21
+ let(:that_route2) { double("that_route2", path: "/2", verb: "WALNUTS", reqs: "that_controller#2") }
22
+
23
+ let(:routes) { [route1, route2, route3, route4, route5, route6, bad_route7, bad_route8] }
24
+ let(:this_routes) { [this_route1, this_route2] }
25
+ let(:that_routes) { [that_route1, that_route2] }
26
+
27
+ let(:this_engine) { double("this_engine", routes: double("routes", routes: this_routes))}
28
+ let(:that_engine) { double("that_engine", routes: double("routes", routes: that_routes))}
29
+
30
+ let(:route_wrapper) { ActionDispatch::Routing::RouteWrapper }
16
31
 
17
32
  before do
18
33
  allow(Rails).to receive_message_chain(:application, :routes, :routes).and_return(routes)
19
34
  allow(Rails).to receive_message_chain(:application, :class, :config, :assets, :prefix).and_return("/assets")
35
+
36
+ allow(Module).to receive(:const_get).with(this_engine_string).and_return(this_engine)
37
+ allow(Module).to receive(:const_get).with(that_engine_string).and_return(that_engine)
38
+
39
+ allow(route_wrapper).to receive(:new).with(route1).and_return(route1)
40
+ allow(route_wrapper).to receive(:new).with(route2).and_return(route2)
41
+ allow(route_wrapper).to receive(:new).with(route3).and_return(route3)
42
+ allow(route_wrapper).to receive(:new).with(route4).and_return(route4)
43
+ allow(route_wrapper).to receive(:new).with(route5).and_return(route5)
44
+ allow(route_wrapper).to receive(:new).with(route6).and_return(route6)
45
+ allow(route_wrapper).to receive(:new).with(bad_route7).and_return(bad_route7)
46
+ allow(route_wrapper).to receive(:new).with(bad_route8).and_return(bad_route8)
47
+
48
+ allow(route_wrapper).to receive(:new).with(this_route1).and_return(this_route1)
49
+ allow(route_wrapper).to receive(:new).with(this_route2).and_return(this_route2)
50
+
51
+ allow(route_wrapper).to receive(:new).with(that_route1).and_return(that_route1)
52
+ allow(route_wrapper).to receive(:new).with(that_route2).and_return(that_route2)
53
+
20
54
  get :index
21
55
  end
22
56
 
23
57
  it { expect(response.status).to eq 200 }
24
- it { expect(JSON.parse(response.body).length).to eq 4 }
25
- it { expect(JSON.parse(response.body)[0]).to eq "/good/route1" }
26
- it { expect(JSON.parse(response.body)[1]).to eq "/good/route2" }
27
- it { expect(JSON.parse(response.body)[2]).to eq "/" }
28
- it { expect(JSON.parse(response.body)[3]).to eq "/assets" }
58
+ it { expect(JSON.parse(response.body).length).to eq 8 }
59
+ it { expect(JSON.parse(response.body)[0]).to eq "/1" }
60
+ it { expect(JSON.parse(response.body)[1]).to eq "/2" }
61
+ it { expect(JSON.parse(response.body)[2]).to eq "/3" }
62
+ it { expect(JSON.parse(response.body)[3]).to eq "/4" }
63
+ it { expect(JSON.parse(response.body)[4]).to eq "/t1" }
64
+ it { expect(JSON.parse(response.body)[5]).to eq "/t2" }
65
+ it { expect(JSON.parse(response.body)[6]).to eq "/that/1" }
66
+ it { expect(JSON.parse(response.body)[7]).to eq "/that/2" }
29
67
 
30
68
  end
31
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routes_revealer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Consumer Development
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-10 00:00:00.000000000 Z
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails