rails_best_practices 1.3.0 → 1.4.0

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.
Files changed (60) hide show
  1. data/Gemfile.lock +1 -1
  2. data/README.md +14 -12
  3. data/lib/rails_best_practices.rb +7 -21
  4. data/lib/rails_best_practices/core.rb +1 -0
  5. data/lib/rails_best_practices/core/check.rb +156 -6
  6. data/lib/rails_best_practices/core/checking_visitor.rb +2 -2
  7. data/lib/rails_best_practices/core/methods.rb +1 -0
  8. data/lib/rails_best_practices/core/nil.rb +10 -0
  9. data/lib/rails_best_practices/core/routes.rb +33 -0
  10. data/lib/rails_best_practices/core/runner.rb +3 -1
  11. data/lib/rails_best_practices/core_ext/sexp.rb +11 -0
  12. data/lib/rails_best_practices/prepares.rb +5 -2
  13. data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -14
  14. data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -7
  15. data/lib/rails_best_practices/prepares/model_prepare.rb +3 -8
  16. data/lib/rails_best_practices/prepares/route_prepare.rb +142 -0
  17. data/lib/rails_best_practices/prepares/schema_prepare.rb +3 -8
  18. data/lib/rails_best_practices/reviews.rb +1 -0
  19. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +3 -8
  20. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +9 -12
  21. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -8
  22. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +3 -8
  23. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -8
  24. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +3 -4
  25. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +2 -8
  26. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +3 -8
  27. data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +3 -8
  28. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +2 -8
  29. data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +3 -8
  30. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +3 -8
  31. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +3 -8
  32. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +3 -9
  33. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -8
  34. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +57 -0
  35. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +6 -92
  36. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +3 -8
  37. data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +3 -8
  38. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +3 -8
  39. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -8
  40. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -8
  41. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +3 -8
  42. data/lib/rails_best_practices/reviews/use_model_association_review.rb +3 -8
  43. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +3 -8
  44. data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -8
  45. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +2 -4
  46. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -8
  47. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +3 -8
  48. data/lib/rails_best_practices/version.rb +1 -1
  49. data/rails_best_practices.yml +1 -0
  50. data/spec/rails_best_practices/core/check_spec.rb +2 -2
  51. data/spec/rails_best_practices/core/checking_visitor_spec.rb +12 -32
  52. data/spec/rails_best_practices/core/nil_spec.rb +12 -0
  53. data/spec/rails_best_practices/core/routes_spec.rb +10 -0
  54. data/spec/rails_best_practices/core_ext/sexp_spec.rb +14 -0
  55. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +502 -0
  56. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +14 -1
  57. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +20 -4
  58. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +120 -0
  59. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +1 -1
  60. metadata +113 -123
@@ -15,6 +15,9 @@ module RailsBestPractices
15
15
  # if there is a route generated, but there is not action in that controller,
16
16
  # then you should restrict your routes.
17
17
  class RestrictAutoGeneratedRoutesReview < Review
18
+ interesting_nodes :command, :command_call, :method_add_block
19
+ interesting_files ROUTE_FILES
20
+
18
21
  RESOURCE_METHODS = ["show", "new", "create", "edit", "update", "destroy"]
19
22
  RESOURCES_METHODS = RESOURCE_METHODS + ["index"]
20
23
 
@@ -22,14 +25,6 @@ module RailsBestPractices
22
25
  "http://rails-bestpractices.com/posts/86-restrict-auto-generated-routes"
23
26
  end
24
27
 
25
- def interesting_nodes
26
- [:command, :command_call, :method_add_block]
27
- end
28
-
29
- def interesting_files
30
- ROUTE_FILES
31
- end
32
-
33
28
  def initialize
34
29
  super
35
30
  @namespaces = []
@@ -14,18 +14,13 @@ module RailsBestPractices
14
14
  # if there is a key 'action', 'template' or 'file' in the argument,
15
15
  # then they should be replaced by simplified syntax.
16
16
  class SimplifyRenderInControllersReview < Review
17
+ interesting_nodes :command
18
+ interesting_files CONTROLLER_FILES
19
+
17
20
  def url
18
21
  "http://rails-bestpractices.com/posts/62-simplify-render-in-controllers"
19
22
  end
20
23
 
21
- def interesting_nodes
22
- [:command]
23
- end
24
-
25
- def interesting_files
26
- CONTROLLER_FILES
27
- end
28
-
29
24
  # check command node in the controller file,
30
25
  # if its message is render and the arguments contain a key action, template or file,
31
26
  # then it should be replaced by simplified syntax.
@@ -13,18 +13,13 @@ module RailsBestPractices
13
13
  # check all render method commands in view files,
14
14
  # if there is a key 'partial' in the argument, then they should be replaced by simplified syntax.
15
15
  class SimplifyRenderInViewsReview < Review
16
+ interesting_nodes :command
17
+ interesting_files VIEW_FILES
18
+
16
19
  def url
17
20
  "http://rails-bestpractices.com/posts/61-simplify-render-in-views"
18
21
  end
19
22
 
20
- def interesting_nodes
21
- [:command]
22
- end
23
-
24
- def interesting_files
25
- VIEW_FILES
26
- end
27
-
28
23
  # check command node in view file,
29
24
  # if its message is render and the arguments contain a key partial,
30
25
  # then it should be replaced by simplified syntax.
@@ -13,18 +13,13 @@ module RailsBestPractices
13
13
  # check all first code line in method definitions (actions),
14
14
  # if they are duplicated, then they should be moved to before_filter.
15
15
  class UseBeforeFilterReview < Review
16
+ interesting_nodes :class
17
+ interesting_files CONTROLLER_FILES
18
+
16
19
  def url
17
20
  "http://rails-bestpractices.com/posts/22-use-before_filter"
18
21
  end
19
22
 
20
- def interesting_nodes
21
- [:class]
22
- end
23
-
24
- def interesting_files
25
- CONTROLLER_FILES
26
- end
27
-
28
23
  def initialize(options = {})
29
24
  super()
30
25
  @customize_count = options['customize_count'] || 2
@@ -16,18 +16,13 @@ module RailsBestPractices
16
16
  # and the subjects of attribute assignment node and call node are the same,
17
17
  # then model association should be used instead of xxx_id assignment.
18
18
  class UseModelAssociationReview < Review
19
+ interesting_nodes :def
20
+ interesting_files CONTROLLER_FILES
21
+
19
22
  def url
20
23
  "http://rails-bestpractices.com/posts/2-use-model-association"
21
24
  end
22
25
 
23
- def interesting_nodes
24
- [:def]
25
- end
26
-
27
- def interesting_files
28
- CONTROLLER_FILES
29
- end
30
-
31
26
  # check method define nodes to see if there are some attribute assignments that can use model association instead.
32
27
  #
33
28
  # it will check attribute assignment node with message xxx_id=, and call node with message "save" or "save!"
@@ -14,18 +14,13 @@ module RailsBestPractices
14
14
  # check class node to remember the class name,
15
15
  # and check the method definition nodes to see if the corresponding mailer views exist or not.
16
16
  class UseMultipartAlternativeAsContentTypeOfEmailReview < Review
17
+ interesting_nodes :class, :def
18
+ interesting_files MAILER_FILES
19
+
17
20
  def url
18
21
  "http://rails-bestpractices.com/posts/41-use-multipart-alternative-as-content_type-of-email"
19
22
  end
20
23
 
21
- def interesting_nodes
22
- [:class, :def]
23
- end
24
-
25
- def interesting_files
26
- MAILER_FILES
27
- end
28
-
29
24
  # check class node to remember the ActionMailer class name.
30
25
  def start_class(node)
31
26
  @klazz_name = node.class_name.to_s
@@ -20,18 +20,13 @@ module RailsBestPractices
20
20
  # and there is a mailer deliver call,
21
21
  # then the method should be replaced by using observer.
22
22
  class UseObserverReview < Review
23
+ interesting_nodes :def, :command
24
+ interesting_files MODEL_FILES
25
+
23
26
  def url
24
27
  "http://rails-bestpractices.com/posts/19-use-observer"
25
28
  end
26
29
 
27
- def interesting_nodes
28
- [:def, :command]
29
- end
30
-
31
- def interesting_files
32
- MODEL_FILES
33
- end
34
-
35
30
  def initialize
36
31
  super
37
32
  @callbacks = []
@@ -16,6 +16,8 @@ module RailsBestPractices
16
16
  # and their messages of second call are one of nil?, blank?, present?, or they are == ""
17
17
  # then you can use query attribute instead.
18
18
  class UseQueryAttributeReview < Review
19
+ interesting_nodes :if, :unless, :elsif
20
+ interesting_files ALL_FILES
19
21
 
20
22
  QUERY_METHODS = %w(nil? blank? present?)
21
23
 
@@ -23,10 +25,6 @@ module RailsBestPractices
23
25
  "http://rails-bestpractices.com/posts/56-use-query-attribute"
24
26
  end
25
27
 
26
- def interesting_nodes
27
- [:if, :unless, :elsif]
28
- end
29
-
30
28
  # check if node to see whose conditional statement nodes contain nodes that can use query attribute instead.
31
29
  #
32
30
  # it will check every call nodes in the if nodes. If the call node is
@@ -15,6 +15,8 @@ module RailsBestPractices
15
15
  # and the message of method call is not say, say_with_time and default migration methods (such as add_column and create_table),
16
16
  # then the method call should be wrapped by say or say_with_time.
17
17
  class UseSayWithTimeInMigrationsReview < Review
18
+ interesting_nodes :defs
19
+ interesting_files MIGRATION_FILES
18
20
 
19
21
  WITH_SAY_METHODS = %w(say say_with_time)
20
22
 
@@ -22,14 +24,6 @@ module RailsBestPractices
22
24
  "http://rails-bestpractices.com/posts/46-use-say-and-say_with_time-in-migrations-to-make-a-useful-migration-log"
23
25
  end
24
26
 
25
- def interesting_nodes
26
- [:defs]
27
- end
28
-
29
- def interesting_files
30
- MIGRATION_FILES
31
- end
32
-
33
27
  # check a class method define node to see if there are method calls that need to be wrapped by say or say_with_time.
34
28
  #
35
29
  # it will check the first block node,
@@ -16,18 +16,13 @@ module RailsBestPractices
16
16
  # and there is redirect_to method call in if block body,
17
17
  # then it should be replaced by using scope access.
18
18
  class UseScopeAccessReview < Review
19
+ interesting_nodes :if, :unless, :elsif
20
+ interesting_files CONTROLLER_FILES
21
+
19
22
  def url
20
23
  "http://rails-bestpractices.com/posts/3-use-scope-access"
21
24
  end
22
25
 
23
- def interesting_nodes
24
- [:if, :unless, :elsif]
25
- end
26
-
27
- def interesting_files
28
- CONTROLLER_FILES
29
- end
30
-
31
26
  # check if node.
32
27
  #
33
28
  # if it is a method call compared with current_user or current_user.id,
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module RailsBestPractices
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
@@ -28,3 +28,4 @@ RemoveEmptyHelpersCheck: { }
28
28
  RemoveTabCheck: { }
29
29
  RestrictAutoGeneratedRoutesCheck: { }
30
30
  RemoveUnusedMethodsInModelsCheck: { except_methods: [] }
31
+ RemoveUnusedMethodsInControllersCheck: { except_methods: [] }
@@ -7,8 +7,8 @@ describe RailsBestPractices::Core::Check do
7
7
  check.interesting_nodes.should == []
8
8
  end
9
9
 
10
- it "should match all files of interesting files" do
11
- check.interesting_files.should == /.*/
10
+ it "should match none of interesting files" do
11
+ check.interesting_files.should == []
12
12
  end
13
13
 
14
14
  context "node_start" do
@@ -5,44 +5,24 @@ class TestLexical1
5
5
  end
6
6
  class TestLexical2
7
7
  end
8
- class TestPrepare1
9
- def interesting_nodes
10
- [:call]
11
- end
12
-
13
- def interesting_files
14
- RailsBestPractices::Core::Check::MODEL_FILES
15
- end
8
+ class TestPrepare1 < RailsBestPractices::Core::Check
9
+ interesting_nodes :call
10
+ interesting_files MODEL_FILES
16
11
  end
17
12
 
18
- class TestPrepare2
19
- def interesting_nodes
20
- [:class]
21
- end
22
-
23
- def interesting_files
24
- RailsBestPractices::Core::Check::MAILER_FILES
25
- end
13
+ class TestPrepare2 < RailsBestPractices::Core::Check
14
+ interesting_nodes :class
15
+ interesting_files MAILER_FILES
26
16
  end
27
17
 
28
- class TestReview1
29
- def interesting_nodes
30
- [:defn]
31
- end
32
-
33
- def interesting_files
34
- RailsBestPractices::Core::Check::CONTROLLER_FILES
35
- end
18
+ class TestReview1 < RailsBestPractices::Core::Check
19
+ interesting_nodes :defn
20
+ interesting_files CONTROLLER_FILES
36
21
  end
37
22
 
38
- class TestReview2
39
- def interesting_nodes
40
- [:call]
41
- end
42
-
43
- def interesting_files
44
- RailsBestPractices::Core::Check::VIEW_FILES
45
- end
23
+ class TestReview2 < RailsBestPractices::Core::Check
24
+ interesting_nodes :call
25
+ interesting_files VIEW_FILES
46
26
  end
47
27
 
48
28
  describe RailsBestPractices::Core::CheckingVisitor do
@@ -20,4 +20,16 @@ describe RailsBestPractices::Core::Nil do
20
20
  core_nil.undefined.should == core_nil
21
21
  end
22
22
  end
23
+
24
+ context "present?" do
25
+ it "should return false" do
26
+ core_nil.should_not be_present
27
+ end
28
+ end
29
+
30
+ context "blank?" do
31
+ it "should return true" do
32
+ core_nil.should be_blank
33
+ end
34
+ end
23
35
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Core::Routes do
4
+ let(:routes) { RailsBestPractices::Core::Routes.new }
5
+
6
+ it "should add route" do
7
+ routes.add_route(["admin", "test"], "posts", "new")
8
+ routes.map(&:to_s).should == ["Admin::Test::PostsController#new"]
9
+ end
10
+ end
@@ -523,6 +523,20 @@ describe Sexp do
523
523
  end
524
524
  end
525
525
 
526
+ describe "present?" do
527
+ it "should return true" do
528
+ node = parse_content("hello world")
529
+ node.should be_present
530
+ end
531
+ end
532
+
533
+ describe "blank?" do
534
+ it "should return false" do
535
+ node = parse_content("hello world")
536
+ node.should_not be_blank
537
+ end
538
+ end
539
+
526
540
  describe "remove_line_and_column" do
527
541
  it "should remove" do
528
542
  s(:@ident, "test", s(2, 12)).remove_line_and_column.should_equal s(:@ident, "test")
@@ -0,0 +1,502 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsBestPractices::Prepares::RoutePrepare do
4
+ let(:runner) { RailsBestPractices::Core::Runner.new(:prepares => RailsBestPractices::Prepares::RoutePrepare.new) }
5
+
6
+ context "rails2" do
7
+ context "resources" do
8
+ it "should add resources route" do
9
+ content =<<-EOF
10
+ ActionController::Routing::Routes.draw do |map|
11
+ map.resources :posts
12
+ end
13
+ EOF
14
+ runner.prepare('config/routes.rb', content)
15
+ routes = RailsBestPractices::Prepares.routes
16
+ routes.size.should == 7
17
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create", "PostsController#edit", "PostsController#update", "PostsController#destroy"]
18
+ end
19
+
20
+ it "should add multiple resources route" do
21
+ content =<<-EOF
22
+ ActionController::Routing::Routes.draw do |map|
23
+ map.resources :posts, :users
24
+ end
25
+ EOF
26
+ runner.prepare('config/routes.rb', content)
27
+ routes = RailsBestPractices::Prepares.routes
28
+ routes.size.should == 14
29
+ end
30
+
31
+ it "should add resources route with explict controller" do
32
+ content =<<-EOF
33
+ ActionController::Routing::Routes.draw do |map|
34
+ map.resources :posts, :controller => :blog_posts
35
+ end
36
+ EOF
37
+ runner.prepare('config/routes.rb', content)
38
+ routes = RailsBestPractices::Prepares.routes
39
+ routes.size.should == 7
40
+ routes.map(&:to_s).should == ["BlogPostsController#index", "BlogPostsController#show", "BlogPostsController#new", "BlogPostsController#create", "BlogPostsController#edit", "BlogPostsController#update", "BlogPostsController#destroy"]
41
+ end
42
+
43
+ it "should add resources route with only option" do
44
+ content =<<-EOF
45
+ ActionController::Routing::Routes.draw do |map|
46
+ map.resources :posts, :only => [:index, :show, :new, :create]
47
+ end
48
+ EOF
49
+ runner.prepare('config/routes.rb', content)
50
+ routes = RailsBestPractices::Prepares.routes
51
+ routes.size.should == 4
52
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create"]
53
+ end
54
+
55
+ it "should add resources route with except option" do
56
+ content =<<-EOF
57
+ ActionController::Routing::Routes.draw do |map|
58
+ map.resources :posts, :except => [:edit, :update, :destroy]
59
+ end
60
+ EOF
61
+ runner.prepare('config/routes.rb', content)
62
+ routes = RailsBestPractices::Prepares.routes
63
+ routes.size.should == 4
64
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create"]
65
+ end
66
+
67
+ it "should not add resources routes with :only => :none" do
68
+ content =<<-EOF
69
+ ActionController::Routing::Routes.draw do |map|
70
+ map.resources :posts, :only => :none
71
+ end
72
+ EOF
73
+ runner.prepare('config/routes.rb', content)
74
+ routes = RailsBestPractices::Prepares.routes
75
+ routes.size.should == 0
76
+ end
77
+
78
+ it "should not add resources routes with :except => :all" do
79
+ content =<<-EOF
80
+ ActionController::Routing::Routes.draw do |map|
81
+ map.resources :posts, :except => :all
82
+ end
83
+ EOF
84
+ runner.prepare('config/routes.rb', content)
85
+ routes = RailsBestPractices::Prepares.routes
86
+ routes.size.should == 0
87
+ end
88
+
89
+ it "should add resource routes with hash collection/member routes" do
90
+ content =<<-EOF
91
+ ActionController::Routing::Routes.draw do |map|
92
+ map.resources :posts, :only => [:show], :collection => { :list => :get }, :member => { :create => :post, :update => :put, :destroy => :delete }
93
+ end
94
+ EOF
95
+ runner.prepare('config/routes.rb', content)
96
+ routes = RailsBestPractices::Prepares.routes
97
+ routes.size.should == 5
98
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#create", "PostsController#update", "PostsController#destroy", "PostsController#list"]
99
+ end
100
+
101
+ it "should add resource routes with array collection/member routes" do
102
+ content =<<-EOF
103
+ ActionController::Routing::Routes.draw do |map|
104
+ map.resources :posts, :only => [:show], :collection => [:list], :member => [:create, :update, :destroy]
105
+ end
106
+ EOF
107
+ runner.prepare('config/routes.rb', content)
108
+ routes = RailsBestPractices::Prepares.routes
109
+ routes.size.should == 5
110
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#create", "PostsController#update", "PostsController#destroy", "PostsController#list"]
111
+ end
112
+
113
+ it "should add route with nested routes" do
114
+ content =<<-EOF
115
+ ActionController::Routing::Routes.draw do |map|
116
+ map.resources :posts do |post|
117
+ post.resources :comments
118
+ end
119
+ end
120
+ EOF
121
+ runner.prepare('config/routes.rb', content)
122
+ routes = RailsBestPractices::Prepares.routes
123
+ routes.size.should == 14
124
+ end
125
+
126
+ it "should add rout with namespace" do
127
+ content =<<-EOF
128
+ ActionController::Routing::Routes.draw do |map|
129
+ map.namespace :admin do |admin|
130
+ admin.namespace :test do |test|
131
+ test.resources :posts, :only => [:index]
132
+ end
133
+ end
134
+ end
135
+ EOF
136
+ runner.prepare('config/routes.rb', content)
137
+ routes = RailsBestPractices::Prepares.routes
138
+ routes.map(&:to_s).should == ["Admin::Test::PostsController#index"]
139
+ end
140
+ end
141
+
142
+ context "resource" do
143
+ it "should add resource route" do
144
+ content =<<-EOF
145
+ ActionController::Routing::Routes.draw do |map|
146
+ map.resource :posts
147
+ end
148
+ EOF
149
+ runner.prepare('config/routes.rb', content)
150
+ routes = RailsBestPractices::Prepares.routes
151
+ routes.size.should == 6
152
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create", "PostsController#edit", "PostsController#update", "PostsController#destroy"]
153
+ end
154
+
155
+ it "should add multiple resource route" do
156
+ content =<<-EOF
157
+ ActionController::Routing::Routes.draw do |map|
158
+ map.resource :posts, :users
159
+ end
160
+ EOF
161
+ runner.prepare('config/routes.rb', content)
162
+ routes = RailsBestPractices::Prepares.routes
163
+ routes.size.should == 12
164
+ end
165
+
166
+ it "should add resource route with only option" do
167
+ content =<<-EOF
168
+ ActionController::Routing::Routes.draw do |map|
169
+ map.resource :posts, :only => [:show, :new, :create]
170
+ end
171
+ EOF
172
+ runner.prepare('config/routes.rb', content)
173
+ routes = RailsBestPractices::Prepares.routes
174
+ routes.size.should == 3
175
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create"]
176
+ end
177
+
178
+ it "should add resource route with except option" do
179
+ content =<<-EOF
180
+ ActionController::Routing::Routes.draw do |map|
181
+ map.resource :posts, :except => [:edit, :update, :destroy]
182
+ end
183
+ EOF
184
+ runner.prepare('config/routes.rb', content)
185
+ routes = RailsBestPractices::Prepares.routes
186
+ routes.size.should == 3
187
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create"]
188
+ end
189
+
190
+ it "should not add resource routes with :only => :none" do
191
+ content =<<-EOF
192
+ ActionController::Routing::Routes.draw do |map|
193
+ map.resource :posts, :only => :none
194
+ end
195
+ EOF
196
+ runner.prepare('config/routes.rb', content)
197
+ routes = RailsBestPractices::Prepares.routes
198
+ routes.size.should == 0
199
+ end
200
+
201
+ it "should not add resource routes with :except => :all" do
202
+ content =<<-EOF
203
+ ActionController::Routing::Routes.draw do |map|
204
+ map.resource :posts, :except => :all
205
+ end
206
+ EOF
207
+ runner.prepare('config/routes.rb', content)
208
+ routes = RailsBestPractices::Prepares.routes
209
+ routes.size.should == 0
210
+ end
211
+ end
212
+
213
+ it "should add connect route" do
214
+ content =<<-EOF
215
+ ActionController::Routing::Routes.draw do |map|
216
+ map.connect 'vote', :controller => "votes", :action => "create", :method => :post
217
+ end
218
+ EOF
219
+ runner.prepare('config/routes.rb', content)
220
+ routes = RailsBestPractices::Prepares.routes
221
+ routes.map(&:to_s).should == ["VotesController#create"]
222
+ end
223
+
224
+ it "should add connect route with all actions" do
225
+ content =<<-EOF
226
+ ActionController::Routing::Routes.draw do |map|
227
+ map.connect 'internal/:action/*whatever', :controller => "internal"
228
+ end
229
+ EOF
230
+ runner.prepare('config/routes.rb', content)
231
+ routes = RailsBestPractices::Prepares.routes
232
+ routes.map(&:to_s).should == ["InternalController#*"]
233
+ end
234
+
235
+ it "should add named route" do
236
+ content =<<-EOF
237
+ ActionController::Routing::Routes.draw do |map|
238
+ map.login '/player/login', :controller => 'sessions', :action => 'new', :conditions => { :method => :get }
239
+ end
240
+ EOF
241
+ runner.prepare('config/routes.rb', content)
242
+ routes = RailsBestPractices::Prepares.routes
243
+ routes.map(&:to_s).should == ["SessionsController#new"]
244
+ end
245
+ end
246
+
247
+ context "rails3" do
248
+ context "resources" do
249
+ it "should add resources route" do
250
+ content =<<-EOF
251
+ RailsBestPracticesCom::Application.routes.draw do
252
+ resources :posts
253
+ end
254
+ EOF
255
+ runner.prepare('config/routes.rb', content)
256
+ routes = RailsBestPractices::Prepares.routes
257
+ routes.size.should == 7
258
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create", "PostsController#edit", "PostsController#update", "PostsController#destroy"]
259
+ end
260
+
261
+ it "should add multiple resources route" do
262
+ content =<<-EOF
263
+ RailsBestPracticesCom::Application.routes.draw do
264
+ resources :posts, :users
265
+ end
266
+ EOF
267
+ runner.prepare('config/routes.rb', content)
268
+ routes = RailsBestPractices::Prepares.routes
269
+ routes.size.should == 14
270
+ end
271
+
272
+ it "should add resources route with explict controller" do
273
+ content =<<-EOF
274
+ RailsBestPracticesCom::Application.routes.draw do
275
+ resources :posts, :controller => :blog_posts
276
+ end
277
+ EOF
278
+ runner.prepare('config/routes.rb', content)
279
+ routes = RailsBestPractices::Prepares.routes
280
+ routes.size.should == 7
281
+ routes.map(&:to_s).should == ["BlogPostsController#index", "BlogPostsController#show", "BlogPostsController#new", "BlogPostsController#create", "BlogPostsController#edit", "BlogPostsController#update", "BlogPostsController#destroy"]
282
+ end
283
+
284
+ it "should add resources route with only option" do
285
+ content =<<-EOF
286
+ RailsBestPracticesCom::Application.routes.draw do
287
+ resources :posts, :only => [:index, :show, :new, :create]
288
+ end
289
+ EOF
290
+ runner.prepare('config/routes.rb', content)
291
+ routes = RailsBestPractices::Prepares.routes
292
+ routes.size.should == 4
293
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create"]
294
+ end
295
+
296
+ it "should add resources route with except option" do
297
+ content =<<-EOF
298
+ RailsBestPracticesCom::Application.routes.draw do
299
+ resources :posts, :except => [:edit, :update, :destroy]
300
+ end
301
+ EOF
302
+ runner.prepare('config/routes.rb', content)
303
+ routes = RailsBestPractices::Prepares.routes
304
+ routes.size.should == 4
305
+ routes.map(&:to_s).should == ["PostsController#index", "PostsController#show", "PostsController#new", "PostsController#create"]
306
+ end
307
+
308
+ it "should not add resources routes with :only => :none" do
309
+ content =<<-EOF
310
+ RailsBestPracticesCom::Application.routes.draw do
311
+ resources :posts, :only => :none
312
+ end
313
+ EOF
314
+ runner.prepare('config/routes.rb', content)
315
+ routes = RailsBestPractices::Prepares.routes
316
+ routes.size.should == 0
317
+ end
318
+
319
+ it "should not add resources routes with :except => :all" do
320
+ content =<<-EOF
321
+ RailsBestPracticesCom::Application.routes.draw do
322
+ resources :posts, :except => :all
323
+ end
324
+ EOF
325
+ runner.prepare('config/routes.rb', content)
326
+ routes = RailsBestPractices::Prepares.routes
327
+ routes.size.should == 0
328
+ end
329
+
330
+ it "should add connect route" do
331
+ content =<<-EOF
332
+ ActionController::Routing::Routes.draw do |map|
333
+ map.connect 'vote', :controller => "votes", :action => "create", :method => :post
334
+ end
335
+ EOF
336
+ runner.prepare('config/routes.rb', content)
337
+ routes = RailsBestPractices::Prepares.routes
338
+ routes.map(&:to_s).should == ["VotesController#create"]
339
+ end
340
+
341
+ it "should add named route" do
342
+ content =<<-EOF
343
+ ActionController::Routing::Routes.draw do |map|
344
+ map.login '/player/login', :controller => 'sessions', :action => 'new', :conditions => { :method => :get }
345
+ end
346
+ EOF
347
+ runner.prepare('config/routes.rb', content)
348
+ routes = RailsBestPractices::Prepares.routes
349
+ routes.map(&:to_s).should == ["SessionsController#new"]
350
+ end
351
+ end
352
+
353
+ context "resource" do
354
+ it "should add resource route" do
355
+ content =<<-EOF
356
+ RailsBestPracticesCom::Application.routes.draw do
357
+ resource :posts
358
+ end
359
+ EOF
360
+ runner.prepare('config/routes.rb', content)
361
+ routes = RailsBestPractices::Prepares.routes
362
+ routes.size.should == 6
363
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create", "PostsController#edit", "PostsController#update", "PostsController#destroy"]
364
+ end
365
+
366
+ it "should add multiple resource route" do
367
+ content =<<-EOF
368
+ RailsBestPracticesCom::Application.routes.draw do
369
+ resource :posts, :users
370
+ end
371
+ EOF
372
+ runner.prepare('config/routes.rb', content)
373
+ routes = RailsBestPractices::Prepares.routes
374
+ routes.size.should == 12
375
+ end
376
+
377
+ it "should add resource route with only option" do
378
+ content =<<-EOF
379
+ RailsBestPracticesCom::Application.routes.draw do
380
+ resource :posts, :only => [:show, :new, :create]
381
+ end
382
+ EOF
383
+ runner.prepare('config/routes.rb', content)
384
+ routes = RailsBestPractices::Prepares.routes
385
+ routes.size.should == 3
386
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create"]
387
+ end
388
+
389
+ it "should add resource route with except option" do
390
+ content =<<-EOF
391
+ RailsBestPracticesCom::Application.routes.draw do
392
+ resource :posts, :except => [:edit, :update, :destroy]
393
+ end
394
+ EOF
395
+ runner.prepare('config/routes.rb', content)
396
+ routes = RailsBestPractices::Prepares.routes
397
+ routes.size.should == 3
398
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#new", "PostsController#create"]
399
+ end
400
+
401
+ it "should not add resource routes with :only => :none" do
402
+ content =<<-EOF
403
+ RailsBestPracticesCom::Application.routes.draw do
404
+ resource :posts, :only => :none
405
+ end
406
+ EOF
407
+ runner.prepare('config/routes.rb', content)
408
+ routes = RailsBestPractices::Prepares.routes
409
+ routes.size.should == 0
410
+ end
411
+
412
+ it "should not add resource routes with :except => :all" do
413
+ content =<<-EOF
414
+ RailsBestPracticesCom::Application.routes.draw do
415
+ resource :posts, :except => :all
416
+ end
417
+ EOF
418
+ runner.prepare('config/routes.rb', content)
419
+ routes = RailsBestPractices::Prepares.routes
420
+ routes.size.should == 0
421
+ end
422
+
423
+ it "should add resource routes with get/post/put/delete routes" do
424
+ content =<<-EOF
425
+ RailsBestPracticesCom::Application.routes.draw do
426
+ resources :posts, :only => [:show] do
427
+ get :list, :on => :collection
428
+ post :create, :on => :member
429
+ put :update, :on => :member
430
+ delete :destroy, :on => :memeber
431
+ end
432
+ end
433
+ EOF
434
+ runner.prepare('config/routes.rb', content)
435
+ routes = RailsBestPractices::Prepares.routes
436
+ routes.size.should == 5
437
+ routes.map(&:to_s).should == ["PostsController#show", "PostsController#list", "PostsController#create", "PostsController#update", "PostsController#destroy"]
438
+ end
439
+
440
+ it "should add route with nested routes" do
441
+ content =<<-EOF
442
+ RailsBestPracticesCom::Application.routes.draw do
443
+ resources :posts
444
+ resources :comments
445
+ end
446
+ end
447
+ EOF
448
+ runner.prepare('config/routes.rb', content)
449
+ routes = RailsBestPractices::Prepares.routes
450
+ routes.size.should == 14
451
+ end
452
+
453
+ it "should add rout with namespace" do
454
+ content =<<-EOF
455
+ RailsBestPracticesCom::Application.routes.draw do
456
+ namespace :admin do
457
+ namespace :test do
458
+ resources :posts, :only => [:index]
459
+ end
460
+ end
461
+ end
462
+ EOF
463
+ runner.prepare('config/routes.rb', content)
464
+ routes = RailsBestPractices::Prepares.routes
465
+ routes.map(&:to_s).should == ["Admin::Test::PostsController#index"]
466
+ end
467
+
468
+ it "should add match route" do
469
+ content =<<-EOF
470
+ RailsBestPracticesCom::Application.routes.draw do
471
+ match '/auth/:provider/callback' => 'authentications#create'
472
+ end
473
+ EOF
474
+ runner.prepare('config/routes.rb', content)
475
+ routes = RailsBestPractices::Prepares.routes
476
+ routes.map(&:to_s).should == ["AuthenticationsController#create"]
477
+ end
478
+
479
+ it "should add match route with all actions" do
480
+ content =<<-EOF
481
+ RailsBestPracticesCom::Application.routes.draw do
482
+ match 'internal/:action/*whatever', :controller => "internal"
483
+ end
484
+ EOF
485
+ runner.prepare('config/routes.rb', content)
486
+ routes = RailsBestPractices::Prepares.routes
487
+ routes.map(&:to_s).should == ["InternalController#*"]
488
+ end
489
+
490
+ it "should add root route" do
491
+ content =<<-EOF
492
+ RailsBestPracticesCom::Application.routes.draw do
493
+ root :to => 'home#index'
494
+ end
495
+ EOF
496
+ runner.prepare('config/routes.rb', content)
497
+ routes = RailsBestPractices::Prepares.routes
498
+ routes.map(&:to_s).should == ["HomeController#index"]
499
+ end
500
+ end
501
+ end
502
+ end