rails_best_practices 1.13.5 → 1.13.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/lib/rails_best_practices/analyzer.rb +3 -2
- data/lib/rails_best_practices/command.rb +6 -1
- data/lib/rails_best_practices/core/check.rb +2 -1
- data/lib/rails_best_practices/core/gems.rb +21 -0
- data/lib/rails_best_practices/core/klasses.rb +1 -1
- data/lib/rails_best_practices/core/runner.rb +6 -3
- data/lib/rails_best_practices/lexicals/long_line_check.rb +2 -0
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +1 -0
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +1 -0
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +6 -6
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +31 -0
- data/lib/rails_best_practices/prepares.rb +1 -0
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +14 -0
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +8 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +1 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +16 -40
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +1 -1
- data/lib/rails_best_practices/version.rb +1 -1
- data/spec/rails_best_practices/core/gems_spec.rb +22 -0
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +17 -5
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +32 -0
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +60 -8
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +29 -0
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +15 -7
- metadata +7 -4
- data/.coveralls.yml +0 -1
- data/.rvmrc +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bd66b9f885d3de24877f8f671c8caa7ec05a0de
|
4
|
+
data.tar.gz: 4c82581c64139d752d9205689a5c0c805735d7fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc478b2544c16dd600bd97c126567638429bf16d3781e15123128e2e7381303019a795c15af8a05821d52d4619beae550c98b18bb6ae1b3c2013d8c126efa800
|
7
|
+
data.tar.gz: 0ea6abf20e3c61a99c42c4ebd311bc4b31f5eaa5055d07cfce79c7fdda8cd8ed866c8d34414837b9c050cf948c9cfbf8c19d981a7ae0e55b135a339b8129d476
|
data/.gitignore
CHANGED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rails_best_practices
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
@@ -116,8 +116,9 @@ module RailsBestPractices
|
|
116
116
|
files = file_ignore(files, pattern)
|
117
117
|
end
|
118
118
|
|
119
|
-
|
120
|
-
|
119
|
+
%w(Capfile Gemfile Gemfile.lock).each do |file|
|
120
|
+
files.unshift File.join(@path, file)
|
121
|
+
end
|
121
122
|
|
122
123
|
files.compact
|
123
124
|
end
|
@@ -44,7 +44,7 @@ OptionParser.new do |opts|
|
|
44
44
|
opts.on("--with-textmate", "open file by textmate in html format") do
|
45
45
|
options["with-textmate"] = true
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
opts.on("--with-sublime", "open file by sublime in html format") do
|
49
49
|
options["with-sublime"] = true
|
50
50
|
end
|
@@ -122,6 +122,11 @@ OptionParser.new do |opts|
|
|
122
122
|
opts.parse!
|
123
123
|
end
|
124
124
|
|
125
|
+
if ARGV.size > 0 && !File.exists?(ARGV.first)
|
126
|
+
puts "#{ARGV.first} doesn't exist"
|
127
|
+
exit 1
|
128
|
+
end
|
129
|
+
|
125
130
|
if options["generate"]
|
126
131
|
RailsBestPractices::Analyzer.new(ARGV.first).generate
|
127
132
|
else
|
@@ -15,8 +15,9 @@ module RailsBestPractices
|
|
15
15
|
HELPER_FILES = /app\/helpers\/.*\.rb$/
|
16
16
|
DEPLOY_FILES = /config\/deploy.*\.rb/
|
17
17
|
CONFIG_FILES = /config\/(application|environment|environments\/.*)\.rb/
|
18
|
+
INITIALIZER_FILES = /config\/initializers\/.*\.rb/
|
18
19
|
CAPFILE = /Capfile/
|
19
|
-
|
20
|
+
GEMFILE_LOCK = /Gemfile\.lock/
|
20
21
|
|
21
22
|
SKIP_FILES = /db\/schema.rb/
|
22
23
|
|
@@ -2,6 +2,27 @@
|
|
2
2
|
module RailsBestPractices
|
3
3
|
module Core
|
4
4
|
class Gems < Array
|
5
|
+
def has_gem?(gem_name)
|
6
|
+
self.find { |gem| gem.name == gem_name }
|
7
|
+
end
|
8
|
+
|
9
|
+
def gem_version(gem_name)
|
10
|
+
self.find { |gem| gem.name == gem_name }.try(:version)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Gem info includes gem name and gem version
|
15
|
+
class Gem
|
16
|
+
attr_reader :name, :version
|
17
|
+
|
18
|
+
def initialize(name, version)
|
19
|
+
@name = name
|
20
|
+
@version = version
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"#{@name} (#{@version})"
|
25
|
+
end
|
5
26
|
end
|
6
27
|
end
|
7
28
|
end
|
@@ -49,7 +49,8 @@ module RailsBestPractices
|
|
49
49
|
load_plugin_reviews if reviews.empty?
|
50
50
|
|
51
51
|
@lexical_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @lexicals)
|
52
|
-
@
|
52
|
+
@plain_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare })
|
53
|
+
@default_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @prepares.select { |checker| !checker.is_a? Prepares::GemfilePrepare })
|
53
54
|
@review_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @reviews)
|
54
55
|
end
|
55
56
|
|
@@ -70,11 +71,13 @@ module RailsBestPractices
|
|
70
71
|
# @param [String] filename of the file
|
71
72
|
# @param [String] content of the file
|
72
73
|
def prepare(filename, content)
|
73
|
-
@
|
74
|
+
@plain_prepare_checker.check(filename, content)
|
75
|
+
@default_prepare_checker.check(filename, content)
|
74
76
|
end
|
75
77
|
|
76
78
|
def after_prepare
|
77
|
-
@
|
79
|
+
@plain_prepare_checker.after_check
|
80
|
+
@default_prepare_checker.after_check
|
78
81
|
end
|
79
82
|
|
80
83
|
# review the file.
|
@@ -7,6 +7,7 @@ module RailsBestPractices
|
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/81-remove-tab
|
9
9
|
class RemoveTabCheck < Core::Check
|
10
|
+
interesting_files ALL_FILES
|
10
11
|
url "http://rails-bestpractices.com/posts/81-remove-tab"
|
11
12
|
|
12
13
|
# check if the content of file contains a tab.
|
@@ -7,6 +7,7 @@ module RailsBestPractices
|
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/60-remove-trailing-whitespace
|
9
9
|
class RemoveTrailingWhitespaceCheck < Core::Check
|
10
|
+
interesting_files ALL_FILES
|
10
11
|
url "http://rails-bestpractices.com/posts/60-remove-trailing-whitespace"
|
11
12
|
|
12
13
|
# check if the content of file contain a trailing whitespace.
|
@@ -5,17 +5,17 @@ module RailsBestPractices
|
|
5
5
|
module Prepares
|
6
6
|
# Remember all gems in Gemfile
|
7
7
|
class GemfilePrepare < Core::Check
|
8
|
-
|
9
|
-
interesting_files GEMFILE
|
8
|
+
interesting_files GEMFILE_LOCK
|
10
9
|
|
11
10
|
def initialize
|
12
11
|
@gems = Prepares.gems
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def check(filename, content)
|
15
|
+
content.split("\n").each do |line|
|
16
|
+
if line =~ /([^ ]+) \((\d.*)\)/
|
17
|
+
@gems << Core::Gem.new($1, $2)
|
18
|
+
end
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails_best_practices/core/check'
|
3
|
+
|
4
|
+
module RailsBestPractices
|
5
|
+
module Prepares
|
6
|
+
# Check all initializers
|
7
|
+
class InitializerPrepare < Core::Check
|
8
|
+
interesting_nodes :method_add_arg, :class
|
9
|
+
interesting_files INITIALIZER_FILES
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@configs = Prepares.configs
|
13
|
+
end
|
14
|
+
|
15
|
+
# check if AR include ActiveModel::ForbiddenAttributesProtection
|
16
|
+
add_callback :start_method_add_arg do |node|
|
17
|
+
if include_forbidden_attributes_protection?(node)
|
18
|
+
@configs["railsbp.include_forbidden_attributes_protection"] = "true"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# check if the node is
|
23
|
+
# ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
24
|
+
def include_forbidden_attributes_protection?(node)
|
25
|
+
"ActiveRecord::Base" == node.receiver.to_s &&
|
26
|
+
"send" == node.message.to_s &&
|
27
|
+
["include", "ActiveModel::ForbiddenAttributesProtection"] == node.arguments.all.map(&:to_s)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -7,6 +7,7 @@ require 'rails_best_practices/prepares/route_prepare'
|
|
7
7
|
require 'rails_best_practices/prepares/helper_prepare'
|
8
8
|
require 'rails_best_practices/prepares/config_prepare'
|
9
9
|
require 'rails_best_practices/prepares/gemfile_prepare'
|
10
|
+
require 'rails_best_practices/prepares/initializer_prepare'
|
10
11
|
|
11
12
|
module RailsBestPractices
|
12
13
|
module Prepares
|
@@ -20,7 +20,9 @@ module RailsBestPractices
|
|
20
20
|
# we treat it as mass assignment by default.
|
21
21
|
add_callback :start_class do |node|
|
22
22
|
@mass_assignement = true
|
23
|
+
check_rails_version
|
23
24
|
check_whitelist_attributes_config
|
25
|
+
check_include_forbidden_attributes_protection_config
|
24
26
|
end
|
25
27
|
|
26
28
|
# check if it is ActiveRecord::Base subclass and
|
@@ -52,12 +54,24 @@ module RailsBestPractices
|
|
52
54
|
end
|
53
55
|
|
54
56
|
private
|
57
|
+
def check_rails_version
|
58
|
+
if Prepares.gems.gem_version("rails").to_i > 3
|
59
|
+
@mass_assignement = false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
55
63
|
def check_whitelist_attributes_config
|
56
64
|
if "true" == Prepares.configs["config.active_record.whitelist_attributes"]
|
57
65
|
@whitelist_attributes = true
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
69
|
+
def check_include_forbidden_attributes_protection_config
|
70
|
+
if "true" == Prepares.configs["railsbp.include_forbidden_attributes_protection"]
|
71
|
+
@mass_assignement = false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
61
75
|
def check_rails_builtin(node)
|
62
76
|
if @whitelist_attributes && [node.to_s, node.message.to_s].any? { |str| %w(attr_accessible attr_protected).include? str }
|
63
77
|
@mass_assignement = false
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
include Exceptable
|
20
20
|
include InheritedResourcesable
|
21
21
|
|
22
|
-
interesting_nodes :class, :command, :method_add_arg
|
22
|
+
interesting_nodes :class, :command, :method_add_arg, :assign
|
23
23
|
interesting_files CONTROLLER_FILES, VIEW_FILES, HELPER_FILES
|
24
24
|
|
25
25
|
INHERITED_RESOURCES_METHODS = %w(resource collection begin_of_association_chain build_resource)
|
@@ -77,6 +77,13 @@ module RailsBestPractices
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
+
# mark assignment as used, like current_user = @user
|
81
|
+
add_callback :start_assign do |node|
|
82
|
+
if :var_field == node.left_value.sexp_type
|
83
|
+
call_method "#{node.left_value}=", current_class_name
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
80
87
|
# get all unused methods at the end of review process.
|
81
88
|
add_callback :after_check do
|
82
89
|
@routes.each do |route|
|
@@ -98,7 +98,7 @@ module RailsBestPractices
|
|
98
98
|
validate validate_each validate_on_create validate_on_update
|
99
99
|
human_attribute_name
|
100
100
|
assign_attributes
|
101
|
-
to_xml to_json to_param
|
101
|
+
to_xml to_json as_json to_param
|
102
102
|
before_save before_create before_update before_destroy after_save after_create
|
103
103
|
after_update after_destroy after_find after_initialize
|
104
104
|
method_missing
|
@@ -23,7 +23,6 @@ module RailsBestPractices
|
|
23
23
|
RESOURCES_METHODS = RESOURCE_METHODS + ["index"]
|
24
24
|
|
25
25
|
def initialize
|
26
|
-
super
|
27
26
|
@namespaces = []
|
28
27
|
@resource_controllers = []
|
29
28
|
end
|
@@ -77,26 +76,12 @@ module RailsBestPractices
|
|
77
76
|
private
|
78
77
|
# check resources call, if the routes generated by resources does not exist in the controller.
|
79
78
|
def check_resources(node)
|
80
|
-
|
81
|
-
return unless Prepares.controllers.include? controller_name
|
82
|
-
resources_methods = resources_methods(node)
|
83
|
-
unless resources_methods.all? { |meth| Prepares.controller_methods.has_method?(controller_name, meth) }
|
84
|
-
prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
|
85
|
-
only_methods = (resources_methods & prepared_method_names).map { |meth| ":#{meth}" }.join(", ")
|
86
|
-
add_error "restrict auto-generated routes #{friendly_route_name(node)} (only: [#{only_methods}])"
|
87
|
-
end
|
79
|
+
_check(node, RESOURCES_METHODS)
|
88
80
|
end
|
89
81
|
|
90
82
|
# check resource call, if the routes generated by resources does not exist in the controller.
|
91
83
|
def check_resource(node)
|
92
|
-
|
93
|
-
return unless Prepares.controllers.include? controller_name
|
94
|
-
resource_methods = resource_methods(node)
|
95
|
-
unless resource_methods.all? { |meth| Prepares.controller_methods.has_method?(controller_name, meth) }
|
96
|
-
prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
|
97
|
-
only_methods = (resource_methods & prepared_method_names).map { |meth| ":#{meth}" }.join(", ")
|
98
|
-
add_error "restrict auto-generated routes #{friendly_route_name(node)} (only: [#{only_methods}])"
|
99
|
-
end
|
84
|
+
_check(node, RESOURCE_METHODS)
|
100
85
|
end
|
101
86
|
|
102
87
|
# get the controller name.
|
@@ -124,32 +109,23 @@ module RailsBestPractices
|
|
124
109
|
end
|
125
110
|
end
|
126
111
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
end
|
141
|
-
else
|
142
|
-
methods
|
143
|
-
end
|
144
|
-
else
|
145
|
-
methods
|
112
|
+
def _check(node, methods)
|
113
|
+
controller_name = controller_name(node)
|
114
|
+
return unless Prepares.controllers.include? controller_name
|
115
|
+
_methods = _methods(node, methods)
|
116
|
+
unless _methods.all? { |meth| Prepares.controller_methods.has_method?(controller_name, meth) }
|
117
|
+
prepared_method_names = Prepares.controller_methods.get_methods(controller_name).map(&:method_name)
|
118
|
+
only_methods = (_methods & prepared_method_names).map { |meth| ":#{meth}" }
|
119
|
+
routes_message = if only_methods.size > 3
|
120
|
+
"except: [#{(methods.map { |meth| ":" + meth } - only_methods).join(', ')}]"
|
121
|
+
else
|
122
|
+
"only: [#{only_methods.join(', ')}]"
|
123
|
+
end
|
124
|
+
add_error "restrict auto-generated routes #{friendly_route_name(node)} (#{routes_message})"
|
146
125
|
end
|
147
126
|
end
|
148
127
|
|
149
|
-
|
150
|
-
def resource_methods(node)
|
151
|
-
methods = RESOURCE_METHODS
|
152
|
-
|
128
|
+
def _methods(node, methods)
|
153
129
|
if option_with_hash(node)
|
154
130
|
option_node = node.arguments.all[1]
|
155
131
|
if hash_key_exist?(option_node, "only")
|
@@ -18,7 +18,7 @@ module RailsBestPractices
|
|
18
18
|
|
19
19
|
# check command node to see if load 'deploy/assets'
|
20
20
|
add_callback :start_command do |node|
|
21
|
-
if !Prepares.gems.
|
21
|
+
if !Prepares.gems.has_gem?("turbo-sprockets-rails3") && "load" == node.message.to_s && "deploy/assets" == node.arguments.to_s
|
22
22
|
add_error "speed up assets precompile with turbo-sprockets-rails3"
|
23
23
|
end
|
24
24
|
end
|
@@ -3,5 +3,27 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices::Core
|
4
4
|
describe Gems do
|
5
5
|
it { should be_a_kind_of Array }
|
6
|
+
|
7
|
+
let(:gems) { Gems.new }
|
8
|
+
before do
|
9
|
+
gems << Gem.new("rails", "4.0.0")
|
10
|
+
gems << Gem.new("mysql2", "0.2.0")
|
11
|
+
end
|
12
|
+
|
13
|
+
context "#has_gem?" do
|
14
|
+
it "has rails gem" do
|
15
|
+
expect(gems).to be_has_gem "rails"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "hasn't sinatra gem" do
|
19
|
+
expect(gems).not_to be_has_gem "sinatra"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "#gem_version" do
|
24
|
+
it "gets rails version" do
|
25
|
+
expect(gems.gem_version("rails")).to eq "4.0.0"
|
26
|
+
end
|
27
|
+
end
|
6
28
|
end
|
7
29
|
end
|
@@ -8,13 +8,25 @@ module RailsBestPractices
|
|
8
8
|
context "gemfile" do
|
9
9
|
it "should parse gems" do
|
10
10
|
content =<<-EOF
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
rails (3.2.13)
|
15
|
+
actionmailer (= 3.2.13)
|
16
|
+
actionpack (= 3.2.13)
|
17
|
+
activerecord (= 3.2.13)
|
18
|
+
activeresource (= 3.2.13)
|
19
|
+
activesupport (= 3.2.13)
|
20
|
+
bundler (~> 1.0)
|
21
|
+
railties (= 3.2.13)
|
22
|
+
mysql2 (0.3.12b6)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
14
26
|
EOF
|
15
|
-
runner.prepare('Gemfile', content)
|
27
|
+
runner.prepare('Gemfile.lock', content)
|
16
28
|
gems = Prepares.gems
|
17
|
-
gems.should ==
|
29
|
+
gems.map(&:to_s).should == ["rails (3.2.13)", "mysql2 (0.3.12b6)"]
|
18
30
|
end
|
19
31
|
end
|
20
32
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe InitializerPrepare do
|
6
|
+
let(:runner) { Core::Runner.new(prepares: InitializerPrepare.new) }
|
7
|
+
|
8
|
+
context "initializers" do
|
9
|
+
it "should set include_forbidden_attributes_protection config" do
|
10
|
+
content =<<-EOF
|
11
|
+
class AR
|
12
|
+
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
13
|
+
end
|
14
|
+
EOF
|
15
|
+
runner.prepare('config/initializers/ar.rb', content)
|
16
|
+
configs = Prepares.configs
|
17
|
+
configs["railsbp.include_forbidden_attributes_protection"].should == "true"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not set include_forbidden_attributes_protection config" do
|
21
|
+
content =<<-EOF
|
22
|
+
class AR
|
23
|
+
end
|
24
|
+
EOF
|
25
|
+
runner.prepare('config/initializers/ar.rb', content)
|
26
|
+
configs = Prepares.configs
|
27
|
+
configs["railsbp.include_forbidden_attributes_protection"].should be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -3,7 +3,8 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe ProtectMassAssignmentReview do
|
6
|
-
let(:runner) { Core::Runner.new(
|
6
|
+
let(:runner) { Core::Runner.new(prepares: [Prepares::GemfilePrepare.new, Prepares::ConfigPrepare.new, Prepares::InitializerPrepare.new],
|
7
|
+
reviews: ProtectMassAssignmentReview.new) }
|
7
8
|
|
8
9
|
it "should protect mass assignment" do
|
9
10
|
content =<<-EOF
|
@@ -110,14 +111,65 @@ module RailsBestPractices
|
|
110
111
|
runner.should have(0).errors
|
111
112
|
end
|
112
113
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
114
|
+
context "strong_parameters" do
|
115
|
+
it "should not protect mass assignment for strong_parameters" do
|
116
|
+
content =<<-EOF
|
117
|
+
class User < ActiveRecord::Base
|
118
|
+
include ActiveModel::ForbiddenAttributesProtection
|
119
|
+
end
|
120
|
+
EOF
|
121
|
+
runner.review('app/models/user.rb', content)
|
122
|
+
runner.should have(0).errors
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should not protect mass assignment for strong_parameters" do
|
126
|
+
content =<<-EOF
|
127
|
+
class AR
|
128
|
+
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
129
|
+
end
|
130
|
+
EOF
|
131
|
+
runner.prepare('config/initializers/ar.rb', content)
|
132
|
+
content =<<-EOF
|
133
|
+
class User < ActiveRecord::Base
|
134
|
+
end
|
135
|
+
EOF
|
136
|
+
runner.review('app/models/user.rb', content)
|
137
|
+
runner.should have(0).errors
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context "rails 4" do
|
142
|
+
it "should not protect mass assignment for rails 4" do
|
143
|
+
content =<<-EOF
|
144
|
+
GEM
|
145
|
+
remote: http://rubygems.org
|
146
|
+
specs:
|
147
|
+
rails (4.0.0)
|
148
|
+
EOF
|
149
|
+
runner.prepare('Gemfile.lock', content)
|
150
|
+
content =<<-EOF
|
151
|
+
class User < ActiveRecord::Base
|
152
|
+
end
|
153
|
+
EOF
|
154
|
+
runner.review('app/models/user.rb', content)
|
155
|
+
runner.should have(0).errors
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should protect mass assignment for rails 3" do
|
159
|
+
content =<<-EOF
|
160
|
+
GEM
|
161
|
+
remote: http://rubygems.org
|
162
|
+
specs:
|
163
|
+
rails (3.2.13)
|
164
|
+
EOF
|
165
|
+
runner.prepare('Gemfile.lock', content)
|
166
|
+
content =<<-EOF
|
167
|
+
class User < ActiveRecord::Base
|
168
|
+
end
|
169
|
+
EOF
|
170
|
+
runner.review('app/models/user.rb', content)
|
171
|
+
runner.should have(1).errors
|
117
172
|
end
|
118
|
-
EOF
|
119
|
-
runner.review('app/models/user.rb', content)
|
120
|
-
runner.should have(0).errors
|
121
173
|
end
|
122
174
|
end
|
123
175
|
end
|
@@ -186,6 +186,35 @@ module RailsBestPractices
|
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
189
|
+
context "assignment" do
|
190
|
+
it "should not remove unused methods if call in base class" do
|
191
|
+
content =<<-EOF
|
192
|
+
RailsBestPracticesCom::Application.routes.draw do
|
193
|
+
resources :user, only: :show do; end
|
194
|
+
end
|
195
|
+
EOF
|
196
|
+
runner.prepare('config/routes.rb', content)
|
197
|
+
application_content =<<-EOF
|
198
|
+
class ApplicationController
|
199
|
+
def current_user=(user); end
|
200
|
+
end
|
201
|
+
EOF
|
202
|
+
runner.prepare('app/controllers/application_controller.rb', application_content)
|
203
|
+
users_content =<<-EOF
|
204
|
+
class UsersController < ApplicationController
|
205
|
+
def show
|
206
|
+
current_user = @user
|
207
|
+
end
|
208
|
+
end
|
209
|
+
EOF
|
210
|
+
runner.prepare('app/controllers/users_controller.rb', users_content)
|
211
|
+
runner.review('app/controllers/application_controller.rb', application_content)
|
212
|
+
runner.review('app/controllers/users_controller.rb', users_content)
|
213
|
+
runner.after_review
|
214
|
+
runner.should have(0).errors
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
189
218
|
context "helper_method" do
|
190
219
|
it "should remove unused methods if helper method is not called" do
|
191
220
|
content = <<-EOF
|
@@ -29,7 +29,7 @@ module RailsBestPractices
|
|
29
29
|
EOF
|
30
30
|
runner.review('config/routes.rb', content)
|
31
31
|
runner.should have(1).errors
|
32
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (
|
32
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (except: [:index])"
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should not restrict auto-generated routes with only" do
|
@@ -81,7 +81,7 @@ module RailsBestPractices
|
|
81
81
|
EOF
|
82
82
|
runner.review('config/routes.rb', content)
|
83
83
|
runner.should have(1).errors
|
84
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (
|
84
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (except: [:index])"
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -111,7 +111,7 @@ module RailsBestPractices
|
|
111
111
|
EOF
|
112
112
|
runner.review('config/routes.rb', content)
|
113
113
|
runner.should have(1).errors
|
114
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (
|
114
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (except: [:index])"
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should not restrict auto-generated routes with only" do
|
@@ -149,7 +149,7 @@ module RailsBestPractices
|
|
149
149
|
EOF
|
150
150
|
runner.review('config/routes.rb', content)
|
151
151
|
runner.should have(1).errors
|
152
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (
|
152
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (except: [:index])"
|
153
153
|
end
|
154
154
|
|
155
155
|
it "should not restrict auto-generated routes with only" do
|
@@ -181,7 +181,7 @@ module RailsBestPractices
|
|
181
181
|
EOF
|
182
182
|
runner.review('config/routes.rb', content)
|
183
183
|
runner.should have(1).errors
|
184
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (
|
184
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes articles (except: [:index])"
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
@@ -208,7 +208,7 @@ module RailsBestPractices
|
|
208
208
|
EOF
|
209
209
|
runner.review('config/routes.rb', content)
|
210
210
|
runner.should have(1).errors
|
211
|
-
runner.errors[0].to_s.should == "config/routes.rb:3 - restrict auto-generated routes admin/comments (
|
211
|
+
runner.errors[0].to_s.should == "config/routes.rb:3 - restrict auto-generated routes admin/comments (except: [:index])"
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
@@ -238,7 +238,7 @@ module RailsBestPractices
|
|
238
238
|
EOF
|
239
239
|
runner.review('config/routes.rb', content)
|
240
240
|
runner.should have(1).errors
|
241
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (
|
241
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes posts (except: [:index])"
|
242
242
|
end
|
243
243
|
|
244
244
|
it "should not restrict auto-generated routes with only" do
|
@@ -291,7 +291,7 @@ module RailsBestPractices
|
|
291
291
|
EOF
|
292
292
|
runner.review('config/routes.rb', content)
|
293
293
|
runner.should have(1).errors
|
294
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (
|
294
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (except: [:destroy])"
|
295
295
|
end
|
296
296
|
|
297
297
|
it "should not restrict auto-generated routes with only" do
|
@@ -324,7 +324,7 @@ module RailsBestPractices
|
|
324
324
|
EOF
|
325
325
|
runner.review('config/routes.rb', content)
|
326
326
|
runner.should have(1).errors
|
327
|
-
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (
|
327
|
+
runner.errors[0].to_s.should == "config/routes.rb:2 - restrict auto-generated routes account (except: [:destroy])"
|
328
328
|
end
|
329
329
|
|
330
330
|
it "should not restrict auto-generated routes with only" do
|
@@ -23,13 +23,22 @@ module RailsBestPractices
|
|
23
23
|
|
24
24
|
it "should not use turbo-sprockets-rails3 with turbo-sprockets-rails3 gem" do
|
25
25
|
content = <<-EOF
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
GEM
|
27
|
+
remote: http://rubygems.org
|
28
|
+
specs:
|
29
|
+
rails (3.2.13)
|
30
|
+
actionmailer (= 3.2.13)
|
31
|
+
actionpack (= 3.2.13)
|
32
|
+
activerecord (= 3.2.13)
|
33
|
+
activeresource (= 3.2.13)
|
34
|
+
activesupport (= 3.2.13)
|
35
|
+
bundler (~> 1.0)
|
36
|
+
railties (= 3.2.13)
|
37
|
+
turbo-sprockets-rails3 (0.3.6)
|
38
|
+
railties (> 3.2.8, < 4.0.0)
|
39
|
+
sprockets (>= 2.0.0)
|
31
40
|
EOF
|
32
|
-
runner.prepare('Gemfile', content)
|
41
|
+
runner.prepare('Gemfile.lock', content)
|
33
42
|
content = <<-EOF
|
34
43
|
load 'deploy' if respond_to?(:namespace)
|
35
44
|
load 'deploy/assets'
|
@@ -51,4 +60,3 @@ module RailsBestPractices
|
|
51
60
|
end
|
52
61
|
end
|
53
62
|
end
|
54
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_best_practices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.13.
|
4
|
+
version: 1.13.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: code_analyzer
|
@@ -186,11 +186,11 @@ executables:
|
|
186
186
|
extensions: []
|
187
187
|
extra_rdoc_files: []
|
188
188
|
files:
|
189
|
-
- .coveralls.yml
|
190
189
|
- .gemtest
|
191
190
|
- .gitignore
|
192
191
|
- .rspec
|
193
|
-
- .
|
192
|
+
- .ruby-gemset
|
193
|
+
- .ruby-version
|
194
194
|
- .travis.yml
|
195
195
|
- Gemfile
|
196
196
|
- Guardfile
|
@@ -229,6 +229,7 @@ files:
|
|
229
229
|
- lib/rails_best_practices/prepares/controller_prepare.rb
|
230
230
|
- lib/rails_best_practices/prepares/gemfile_prepare.rb
|
231
231
|
- lib/rails_best_practices/prepares/helper_prepare.rb
|
232
|
+
- lib/rails_best_practices/prepares/initializer_prepare.rb
|
232
233
|
- lib/rails_best_practices/prepares/mailer_prepare.rb
|
233
234
|
- lib/rails_best_practices/prepares/model_prepare.rb
|
234
235
|
- lib/rails_best_practices/prepares/route_prepare.rb
|
@@ -301,6 +302,7 @@ files:
|
|
301
302
|
- spec/rails_best_practices/prepares/controller_prepare_spec.rb
|
302
303
|
- spec/rails_best_practices/prepares/gemfile_prepare_spec.rb
|
303
304
|
- spec/rails_best_practices/prepares/helper_prepare_spec.rb
|
305
|
+
- spec/rails_best_practices/prepares/initializer_prepare_spec.rb
|
304
306
|
- spec/rails_best_practices/prepares/mailer_prepare_spec.rb
|
305
307
|
- spec/rails_best_practices/prepares/model_prepare_spec.rb
|
306
308
|
- spec/rails_best_practices/prepares/route_prepare_spec.rb
|
@@ -410,6 +412,7 @@ test_files:
|
|
410
412
|
- spec/rails_best_practices/prepares/controller_prepare_spec.rb
|
411
413
|
- spec/rails_best_practices/prepares/gemfile_prepare_spec.rb
|
412
414
|
- spec/rails_best_practices/prepares/helper_prepare_spec.rb
|
415
|
+
- spec/rails_best_practices/prepares/initializer_prepare_spec.rb
|
413
416
|
- spec/rails_best_practices/prepares/mailer_prepare_spec.rb
|
414
417
|
- spec/rails_best_practices/prepares/model_prepare_spec.rb
|
415
418
|
- spec/rails_best_practices/prepares/route_prepare_spec.rb
|
data/.coveralls.yml
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
repo_token: HdiJjr0c7NCGp2GIL3areCAENLRMT21Er
|
data/.rvmrc
DELETED