rails_best_practices 1.9.0 → 1.9.1
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/.travis.yml +4 -1
- data/Gemfile +0 -3
- data/Gemfile.lock +10 -9
- data/README.md +2 -0
- data/lib/rails_best_practices/analyzer.rb +40 -21
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +12 -0
- data/lib/rails_best_practices/core/configs.rb +7 -0
- data/lib/rails_best_practices/core/error.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +11 -5
- data/lib/rails_best_practices/core/runner.rb +6 -2
- data/lib/rails_best_practices/core_ext/sexp.rb +3 -1
- data/lib/rails_best_practices/prepares.rb +5 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +21 -0
- data/lib/rails_best_practices/prepares/route_prepare.rb +24 -7
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +11 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +7 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +1 -0
- data/spec/rails_best_practices/analyzer_spec.rb +52 -32
- data/spec/rails_best_practices/core/check_spec.rb +64 -45
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +65 -64
- data/spec/rails_best_practices/core/configs_spec.rb +7 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +4 -2
- data/spec/rails_best_practices/core/error_spec.rb +28 -22
- data/spec/rails_best_practices/core/helpers_spec.rb +4 -2
- data/spec/rails_best_practices/core/klasses_spec.rb +8 -6
- data/spec/rails_best_practices/core/mailers_spec.rb +4 -2
- data/spec/rails_best_practices/core/methods_spec.rb +37 -35
- data/spec/rails_best_practices/core/model_associations_spec.rb +17 -15
- data/spec/rails_best_practices/core/model_attributes_spec.rb +17 -15
- data/spec/rails_best_practices/core/models_spec.rb +4 -2
- data/spec/rails_best_practices/core/modules_spec.rb +21 -19
- data/spec/rails_best_practices/core/nil_spec.rb +23 -21
- data/spec/rails_best_practices/core/routes_spec.rb +21 -14
- data/spec/rails_best_practices/core/runner_spec.rb +17 -15
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +32 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +35 -31
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +35 -31
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +24 -0
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +122 -123
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +36 -32
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +12 -8
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +326 -322
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +619 -573
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +25 -21
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +85 -87
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +224 -222
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +30 -26
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +79 -75
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +138 -140
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +38 -34
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +22 -18
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +62 -63
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +51 -50
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +140 -136
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +48 -44
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +42 -38
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +151 -149
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +86 -55
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +23 -19
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +290 -286
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +601 -597
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +55 -54
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +34 -30
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +288 -289
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +59 -55
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +73 -69
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +89 -98
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +52 -52
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +192 -190
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +114 -121
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +189 -190
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +97 -93
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +146 -151
- data/spec/spec_helper.rb +11 -20
- metadata +42 -26
- data/.watchr.example +0 -65
@@ -1,28 +1,34 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
:message => "not good",
|
9
|
-
:type => "BogusReview").to_s.should == "app/models/user.rb:100 - not good"
|
10
|
-
end
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Error do
|
5
|
+
it "should have highlight with false by default" do
|
6
|
+
Error.new.highlight.should == false
|
7
|
+
end
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
it "should return error with filename, line number and message" do
|
10
|
+
Error.new(
|
11
|
+
:filename => "app/models/user.rb",
|
12
|
+
:line_number => "100",
|
13
|
+
:message => "not good",
|
14
|
+
:type => "BogusReview").to_s.should == "app/models/user.rb:100 - not good"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return short filename" do
|
18
|
+
Runner.base_path = "../rails-bestpractices.com"
|
19
|
+
Error.new(
|
20
|
+
:filename => "../rails-bestpractices.com/app/models/user.rb",
|
21
|
+
:line_number => "100",
|
22
|
+
:message => "not good",
|
23
|
+
:type => "BogusReview").short_filename.should == "app/models/user.rb"
|
24
|
+
end
|
20
25
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
it "should return first line number" do
|
27
|
+
Error.new(
|
28
|
+
:filename => "app/models/user.rb",
|
29
|
+
:line_number => "50,70,100",
|
30
|
+
:message => "not good",
|
31
|
+
:type => "BogusReview").first_line_number.should == "50"
|
32
|
+
end
|
27
33
|
end
|
28
34
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Klasses do
|
5
|
+
it { should be_a_kind_of Array }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
context "Klass" do
|
8
|
+
it "should get to_s" do
|
9
|
+
klass = Klass.new("BlogPost", "Post", ["Admin"])
|
10
|
+
klass.to_s.should == "Admin::BlogPost"
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
@@ -1,44 +1,46 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Methods do
|
5
|
+
let(:methods) { Methods.new }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
before :each do
|
8
|
+
methods.add_method("Post", "create")
|
9
|
+
methods.add_method("Post", "destroy")
|
10
|
+
methods.add_method("Post", "save_or_update", {}, "protected")
|
11
|
+
methods.add_method("Post", "find_by_sql", {}, "private")
|
12
|
+
methods.add_method("Comment", "create")
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
it "should get_methods" do
|
16
|
+
methods.get_methods("Post").map(&:method_name).should == ["create", "destroy", "save_or_update", "find_by_sql"]
|
17
|
+
methods.get_methods("Post", "public").map(&:method_name).should == ["create", "destroy"]
|
18
|
+
methods.get_methods("Post", "protected").map(&:method_name).should == ["save_or_update"]
|
19
|
+
methods.get_methods("Post", "private").map(&:method_name).should == ["find_by_sql"]
|
20
|
+
methods.get_methods("Comment").map(&:method_name).should == ["create"]
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
it "should has_method?" do
|
24
|
+
methods.should be_has_method("Post", "create", "public")
|
25
|
+
methods.should be_has_method("Post", "destroy", "public")
|
26
|
+
methods.should_not be_has_method("Post", "save_or_update", "public")
|
27
|
+
methods.should be_has_method("Post", "save_or_update", "protected")
|
28
|
+
methods.should_not be_has_method("Post", "find_by_sql", "public")
|
29
|
+
methods.should be_has_method("Post", "find_by_sql", "private")
|
30
|
+
methods.should_not be_has_method("Comment", "destroy")
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
it "should get_method" do
|
34
|
+
methods.get_method("Post", "create", "public").should_not be_nil
|
35
|
+
methods.get_method("Post", "create", "protected").should be_nil
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
it "should get_all_unused_methods" do
|
39
|
+
methods.get_method("Comment", "create").mark_used
|
40
|
+
methods.get_all_unused_methods("public").map(&:method_name).should == ["create", "destroy"]
|
41
|
+
methods.get_all_unused_methods("protected").map(&:method_name).should == ["save_or_update"]
|
42
|
+
methods.get_all_unused_methods("private").map(&:method_name).should == ["find_by_sql"]
|
43
|
+
methods.get_all_unused_methods.map(&:method_name).should == ["create", "destroy", "save_or_update", "find_by_sql"]
|
44
|
+
end
|
43
45
|
end
|
44
46
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe ModelAssociations do
|
5
|
+
let(:model_associations) { ModelAssociations.new }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
before :each do
|
8
|
+
model_associations.add_association("Project", "project_manager", "belongs_to")
|
9
|
+
model_associations.add_association("Project", "people", "has_many", "Person")
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
it "should get model associations" do
|
13
|
+
model_associations.get_association("Project", "project_manager").should == {"meta" => "belongs_to", "class_name" => "ProjectManager"}
|
14
|
+
model_associations.get_association("Project", "people").should == {"meta" => "has_many", "class_name" => "Person"}
|
15
|
+
model_associations.get_association("Project", "unknown").should be_nil
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
it "should check is model associatiosn" do
|
19
|
+
model_associations.is_association?("Project", "project_manager").should be_true
|
20
|
+
model_associations.is_association?("Project", "people").should be_true
|
21
|
+
model_associations.is_association?("Project", "unknown").should be_false
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe ModelAttributes do
|
5
|
+
let(:model_attributes) { ModelAttributes.new }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
before :each do
|
8
|
+
model_attributes.add_attribute("Post", "title", :string)
|
9
|
+
model_attributes.add_attribute("Post", "user_id", :integer)
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
it "should get model attributes" do
|
13
|
+
model_attributes.get_attribute_type("Post", "title").should == :string
|
14
|
+
model_attributes.get_attribute_type("Post", "user_id").should == :integer
|
15
|
+
model_attributes.get_attribute_type("Post", "unknonw").should be_nil
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
it "should check is model attributes" do
|
19
|
+
model_attributes.is_attribute?("Post", "title").should be_true
|
20
|
+
model_attributes.is_attribute?("Post", "user_id").should be_true
|
21
|
+
model_attributes.is_attribute?("Post", "unknonw").should be_false
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,26 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Modules do
|
5
|
+
it { should be_a_kind_of Array }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
context "Modules" do
|
8
|
+
before do
|
9
|
+
@mod = Mod.new("PostsHelper", [])
|
10
|
+
end
|
11
|
+
subject { Modules.new.tap { |modules| modules << @mod } }
|
12
|
+
it "should add decendant to the corresponding module" do
|
13
|
+
@mod.should_receive(:add_decendant).with("PostsController")
|
14
|
+
subject.add_module_decendant("PostsHelper", "PostsController")
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
context "Mod" do
|
19
|
+
subject {
|
20
|
+
Mod.new("UsersHelper", ["Admin"]).tap do |mod|
|
21
|
+
mod.add_decendant("Admin::UsersController")
|
22
|
+
end
|
23
|
+
}
|
24
|
+
its(:to_s) { should == "Admin::UsersHelper" }
|
25
|
+
its(:decendants) { should == ["Admin::UsersController"] }
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
@@ -1,35 +1,37 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Nil do
|
5
|
+
let(:core_nil) { Nil.new }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
context "to_s" do
|
8
|
+
it "should return self" do
|
9
|
+
core_nil.to_s.should == core_nil
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
context "hash_size" do
|
14
|
+
it "should return 0" do
|
15
|
+
core_nil.hash_size.should == 0
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
context "method_missing" do
|
20
|
+
it "should return self" do
|
21
|
+
core_nil.undefined.should == core_nil
|
22
|
+
end
|
21
23
|
end
|
22
|
-
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
context "present?" do
|
26
|
+
it "should return false" do
|
27
|
+
core_nil.should_not be_present
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
context "blank?" do
|
32
|
+
it "should return true" do
|
33
|
+
core_nil.should be_blank
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -1,22 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Routes do
|
5
|
+
let(:routes) { Routes.new }
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
context "route" do
|
12
|
-
it "should add namesapces, controller name and action name" do
|
13
|
-
route = RailsBestPractices::Core::Route.new(['admin', 'test'], 'posts', 'new')
|
14
|
-
route.to_s.should == "Admin::Test::PostsController#new"
|
7
|
+
it "should add route" do
|
8
|
+
routes.add_route(["admin", "test"], "posts", "new")
|
9
|
+
routes.map(&:to_s).should == ["Admin::Test::PostsController#new"]
|
15
10
|
end
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
context "route" do
|
13
|
+
it "should add namesapces, controller name and action name" do
|
14
|
+
route = Route.new(['admin', 'test'], 'posts', 'new')
|
15
|
+
route.to_s.should == "Admin::Test::PostsController#new"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should add controller name with namespace" do
|
19
|
+
route = Route.new(['admin'], 'test/posts', 'new')
|
20
|
+
route.to_s.should == "Admin::Test::PostsController#new"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should add routes without controller" do
|
24
|
+
route = Route.new(['posts'], nil, 'new')
|
25
|
+
route.to_s.should == "PostsController#new"
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
22
29
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module RailsBestPractices::Core
|
4
|
+
describe Runner do
|
5
|
+
describe "load_plugin_reviews" do
|
6
|
+
shared_examples_for 'load_plugin_reviews' do
|
7
|
+
it "should load plugins in lib/rails_best_practices/plugins/reviews" do
|
8
|
+
runner = Runner.new
|
9
|
+
runner.instance_variable_get('@reviews').map(&:class).should include(RailsBestPractices::Plugins::Reviews::NotUseRailsRootReview)
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
context "given a path that ends with a slash" do
|
14
|
+
before { Runner.base_path = 'spec/fixtures/' }
|
15
|
+
it_should_behave_like 'load_plugin_reviews'
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
context "given a path that does not end with a slash" do
|
19
|
+
before { Runner.base_path = 'spec/fixtures' }
|
20
|
+
it_should_behave_like 'load_plugin_reviews'
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
|
-
end
|
24
|
+
end
|