rake_routes_normalizer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,23 @@ Rails 2 to 3 routes.
7
7
 
8
8
  (Yes, Rails 3 was released last year. But better late than never.)
9
9
 
10
+ With rake_routes_normalizer, you can transform this:
11
+
12
+ user_session POST /user_session(.:format) {:action=>"create", :controller=>"user_sessions"}
13
+ new_user_session GET /user_session/new(.:format) {:action=>"new", :controller=>"user_sessions"}
14
+ edit_user_session GET /user_session/edit(.:format) {:action=>"edit", :controller=>"user_sessions"}
15
+ PUT /user_session(.:format) {:action=>"update", :controller=>"user_sessions"}
16
+ DELETE /user_session(.:format) {:action=>"destroy", :controller=>"user_sessions"}
17
+
18
+ Into this!
19
+
20
+ /user_session(.:format) DELETE user_session {"action"=>"destroy", "controller"=>"user_sessions"}
21
+ /user_session(.:format) POST user_session {"action"=>"create", "controller"=>"user_sessions"}
22
+ /user_session(.:format) PUT user_session {"action"=>"update", "controller"=>"user_sessions"}
23
+ /user_session/edit(.:format) GET edit_user_session {"action"=>"edit", "controller"=>"user_sessions"}
24
+ /user_session/new(.:format) GET new_user_session {"action"=>"new", "controller"=>"user_sessions"}
25
+
26
+
10
27
  Usage
11
28
  =====
12
29
 
@@ -3,7 +3,7 @@ module RakeRoutesNormalizer
3
3
  desc "RAKE_PRINTOUT_FILE", "Print the normalized version of RAKE_PRINTOUT_FILE"
4
4
  def normalize(text)
5
5
  table = RouteSet.parse(text).normalize.routes.map do |route|
6
- [route.name, route.http_verb, route.url_pattern, route.params.inspect]
6
+ [route.url_pattern, route.http_verb, route.name, route.params.inspect]
7
7
  end
8
8
  print_table table
9
9
  end
@@ -28,12 +28,19 @@ module RakeRoutesNormalizer
28
28
  def normalize(options = {})
29
29
  clone.tap do |result|
30
30
  result.http_verb = 'GET' if result.http_verb.to_s =~ /^\s*$/
31
- result.name = options[:previous_route].name if result.name =~ /^\s*$/
31
+ result.name = options[:previous_route].name if result.name =~ /^\s*$/ && options[:previous_route]
32
32
  result.params = Dictionary[KeyHash[params || {}]].order_by_key
33
+ result.url_pattern = "#{result.url_pattern}(.:format)" unless result.url_pattern =~ /\(\.:format\)/
33
34
  end
35
+
36
+ rescue Exception => e
37
+ raise CannotNormalizeRoute.new(:self => self, :options => options)
34
38
  end
35
39
 
36
40
  class CannotParseLine < ExceptionWithDefaultMessage
37
41
  end
42
+
43
+ class CannotNormalizeRoute < ExceptionWithDefaultMessage
44
+ end
38
45
  end
39
46
  end
@@ -10,7 +10,8 @@ module RakeRoutesNormalizer
10
10
 
11
11
  def normalize
12
12
  result = routes.inject(RouteSet.new) do|route_set, route|
13
- route_set.routes << route.normalize(:previous_route => route_set.routes.last)
13
+ previous_route = route_set.routes.detect{|r| route.url_pattern == r.url_pattern }
14
+ route_set.routes << route.normalize(:previous_route => previous_route)
14
15
  route_set
15
16
  end
16
17
  result.routes.sort!
@@ -1,3 +1,3 @@
1
1
  module RakeRoutesNormalizer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -57,14 +57,29 @@ describe RouteSet do
57
57
  it "should normalize a route based the route's previous route" do
58
58
  route_set = RouteSet.new(
59
59
  :routes => [
60
- Route.new(:name => 'account'),
61
- Route.new(:name => '')
60
+ Route.new(:name => 'account', :url_pattern => '/account(.:format)'),
61
+ Route.new(:name => '', :url_pattern => '/account(.:format)')
62
62
  ]
63
63
  )
64
64
  copy = route_set.normalize
65
- copy.routes.map(&:name).uniq.must_equal ['account']
65
+ copy.routes.map(&:name).must_equal ['account', 'account']
66
66
  end
67
67
 
68
+ it "should normalize a route based on a previous route even if the previous route if not adjacent to the current route" do
69
+ route_set = RouteSet.new(
70
+ :routes => [
71
+ Route.new(:name => 'account', :http_verb => 'POST', :url_pattern => '/account(.:format)'),
72
+ Route.new(:name => 'new_account', :http_verb => 'GET', :url_pattern => '/account/new(.:format)'),
73
+ Route.new(:name => '', :http_verb => 'GET', :url_pattern => '/account(.:format)')
74
+ ]
75
+ )
76
+
77
+ copy = route_set.normalize
78
+ copy.routes.map(&:name).sort.must_equal ['account', 'account', 'new_account']
79
+ end
80
+
81
+
82
+
68
83
  it "should sort the routes" do
69
84
  route_set = RouteSet.new(
70
85
  :routes => [
@@ -68,10 +68,25 @@ describe Route do
68
68
  route.normalize(:previous_route => previous_route).name.must_equal 'account_path'
69
69
  end
70
70
 
71
+ it "should set the name to '' if the name is blank and the route does not have previous route" do
72
+ route = Route.new(:name => '')
73
+ route.normalize(:previous_route => nil).name.must_equal ''
74
+ end
75
+
71
76
  it "should sort the route's params by key" do
72
77
  route = Route.new(:params => {:controller=>"users", :action=>"edit_password", :protocol=>nil})
73
78
  route.normalize.params.inspect.must_equal %q|{"action"=>"edit_password", "controller"=>"users", "protocol"=>nil}|
74
79
  end
80
+
81
+ it "should append (.:format) to the route if it is not specified" do
82
+ route = Route.new(:url_pattern => "/jobs(/:job)")
83
+ route.normalize.url_pattern.must_equal "/jobs(/:job)(.:format)"
84
+ end
85
+
86
+ it "should not append (.:format) to the route if it is specified" do
87
+ route = Route.new(:url_pattern => "/account/edit_password(.:format)")
88
+ route.normalize.url_pattern.must_equal "/account/edit_password(.:format)"
89
+ end
75
90
  end
76
91
 
77
92
  describe "<=>(other)" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_routes_normalizer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - George Mendoza