Skip to content

Commit 49be504

Browse files
committed
Merge pull request #2418 from alphagov/use-auto-generated-rubocop-config-ready-to-merge
Use auto generated Rubocop config
2 parents a1e7d62 + 131e4e2 commit 49be504

File tree

79 files changed

+714
-164
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+714
-164
lines changed

.rubocop.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inherit_from:
2+
- .rubocop_todo.yml
3+
AllCops:
4+
Exclude:
5+
- 'tmp/**/*'

.rubocop_todo.yml

+536
Large diffs are not rendered by default.

app/models/ukba_country.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
class UkbaCountry < OpenStruct
44
def self.all
5-
@countries ||= YAML.load_file(Rails.root.join('lib', 'data', 'ukba_additional_countries.yml')).map {|c| self.new(c) }
5+
@countries ||= YAML.load_file(Rails.root.join('lib', 'data', 'ukba_additional_countries.yml')).map { |c| self.new(c) }
66
end
77
end

app/models/world_location.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ def organisations
6565
end
6666

6767
def fco_organisation
68-
self.organisations.find {|o| o.fco_sponsored? }
68+
self.organisations.find { |o| o.fco_sponsored? }
6969
end
7070
end

app/models/worldwide_organisation.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def all_offices
2020
end
2121

2222
def fco_sponsored?
23-
@data.sponsors.any? {|s| s.details.acronym == "FCO" }
23+
@data.sponsors.any? { |s| s.details.acronym == "FCO" }
2424
end
2525

2626
def offices_with_service(service_title)

app/presenters/country_select_question_presenter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class CountrySelectQuestionPresenter < QuestionPresenter
22
def response_label(value)
3-
options.find {|option| option.slug == value}.name
3+
options.find { |option| option.slug == value }.name
44
end
55

66
def options

app/presenters/date_question_presenter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def end_date
1717
@node.range == false ? 3.years.from_now : @node.range.end
1818
end
1919

20-
private
20+
private
2121

2222
def only_display_day_and_month?(value)
2323
value.year.zero?
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class MultipleChoiceQuestionPresenter < QuestionPresenter
22
def response_label(value)
3-
options.find {|option| option.value == value}.label
3+
options.find { |option| option.value == value }.label
44
end
55
end

config/initializers/flow_preview.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
if Rails.env.production?
44
FLOW_REGISTRY_OPTIONS = {}
55
else
6-
FLOW_REGISTRY_OPTIONS = {show_drafts: true}
6+
FLOW_REGISTRY_OPTIONS = { show_drafts: true }
77
end
88

99
# Uncomment the following to run smartanswers with the test flows instead of the real ones

doc/rubocop.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# Rubocop
22

3-
We're using the govuk-lint Gem to include Rubocop and the GOV.UK styleguide rules in the project.
3+
We're using the `govuk-lint` Gem to include Rubocop and the GOV.UK styleguide rules in the project.
44

55
### Jenkins
66

7-
We run Rubocop as part of the test suite executed on Jenkins (see jenkins.sh). Rubocop only tests for violations in files introduced in the branch being tested. This should prevent us from introducing new violations without us having to first fix all existing violations.
7+
We run `govuk-lint-ruby` as part of the test suite executed on Jenkins (see jenkins.sh). Behind the scenes this runs Rubocop with a set of cops defined in the `govuk-lint` gem. However, because `govuk-lint` was retro-fitted to the application relatively recently, there were a large number of cop violations in the codebase.
8+
9+
We've dealt with this by generating a project-specific `.rubocop_todo.yml` file using `govuk-lint-ruby --auto-gen-config --exclude-limit 99999` which [excludes existing violations on a per-cop basis][1]. The limit was set to `99999` to minimise the number of cops that are disabled entirely - most are only disabled for a specific set of files.
10+
11+
The generated `.rubocop_todo.yml` file is included into the configuration via a local `.rubocop.yml` file using an `inherit_from` key. This means that the exclusions in `.rubocop_todo.yml` are used for all `govuk-lint-ruby` commands by default. Note that the `.rubocop.yml` file does not override the cop definitions in the `govuk-lint` gem - it is merely a way to add extra configuration.
12+
13+
Using this approach should prevent us from introducing new violations into the project without needing to fix all the existing violations first. However, in the long run, the idea is for us to gradually remove these exclusions to bring the project fully in line with the default `govuk-lint-ruby` configuration. Ultimately we will be able to remove the `.rubocop_todo.yml` & `.rubocop.yml` files and just rely on the default `govuk-lint` configuration.
14+
15+
Note that as per the header comment in `.rubocop_todo.yml`, changes in the inspected code, or installation of new versions of RuboCop, may require `.rubocop_todo.yml` to be generated again.
816

917
### Running locally
1018

11-
Testing for violations in the entire codebase:
19+
Testing for violations in the entire codebase (used by `jenkins.sh`):
1220

1321
```bash
1422
$ govuk-lint-ruby
@@ -26,4 +34,4 @@ Testing for violations in code staged and committed locally that's not present i
2634
$ govuk-lint-ruby --diff --cached
2735
```
2836

29-
NOTE. This is mostly useful for Jenkins as we first merge, but don't commit, changes from master before running the test suite.
37+
[1]: https://github.com/bbatsov/rubocop/blob/master/README.md#automatically-generated-configuration

jenkins.sh

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export DISPLAY=:99
2626

2727
if [ -z "$RUN_REGRESSION_TESTS" ]; then
2828
bundle exec govuk-lint-ruby \
29-
--diff --cached \
3029
--format clang
3130

3231
RAILS_ENV=test TEST_COVERAGE=true bundle exec rake test

lib/data/state_pension_date_query.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def find_date
2828
private
2929

3030
def run(pension_dates)
31-
pension_dates.find {|p| p.match?(dob, gender)}
31+
pension_dates.find { |p| p.match?(dob, gender) }
3232
end
3333

3434
# Handle the case where the person's d.o.b. is 29th Feb

lib/graph_presenter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def initialize(flow)
44
end
55

66
def labels
7-
Hash[@flow.nodes.map { |node| [node.name, graph_label_text(node)]}]
7+
Hash[@flow.nodes.map { |node| [node.name, graph_label_text(node)] }]
88
end
99

1010
def adjacency_list

lib/graphviz_presenter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def label_lines
3232
style: "filled"
3333
)
3434
end
35-
attribute_clause = attrs.map {|k, v| "#{k}=\"#{v}\""}.join(' ')
35+
attribute_clause = attrs.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
3636
%Q{#{normalize_name(name)} [#{attribute_clause}]}
3737
end
3838
end

lib/smart_answer/calculators/commodity_code_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def has_commodity_code?
2222
commodity_code != 'X'
2323
end
2424

25-
private
25+
private
2626

2727
def glucose_sucrose_index
2828
starch_glucose_to_sucrose[@starch_glucose_weight][@sucrose_weight]

lib/smart_answer/calculators/country_name_formatter.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CountryNameFormatter
1212
"usa" => "the USA"
1313
}
1414

15-
def definitive_article(country, capitalized=false)
15+
def definitive_article(country, capitalized = false)
1616
result = country_name(country)
1717
if requires_definite_article?(country)
1818
result = capitalized ? "The #{result}" : "the #{result}"
@@ -32,7 +32,7 @@ def friendly_name(country)
3232
FRIENDLY_COUNTRY_NAME[country]
3333
end
3434

35-
private
35+
private
3636

3737
def country_name(country)
3838
WorldLocation.find(country).name

lib/smart_answer/calculators/landlord_immigration_check_calculator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def included_country?
1313
postcode_within?(VALID_COUNTRIES, 'country_name')
1414
end
1515

16-
private
16+
private
1717

1818
def postcode_within?(included_areas, key_name)
19-
areas_for_postcode.select {|a| included_areas.include?(a[key_name]) }.any?
19+
areas_for_postcode.select { |a| included_areas.include?(a[key_name]) }.any?
2020
end
2121

2222
def areas_for_postcode

lib/smart_answer/calculators/marriage_abroad_data_query.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def ss_alt_fees_table_country?(country_slug, calculator)
8282
SS_ALT_FEES_TABLE_COUNTRY.include?(country_slug) ||
8383
(SS_ALT_FEES_TABLE_OR_OUTCOME_GROUP_A.include?(country_slug) && calculator.partner_british?) ||
8484
(SS_ALT_FEES_TABLE_OR_OUTCOME_GROUP_B.include?(country_slug) && calculator.partner_is_not_national_of_ceremony_country?) &&
85-
(%w(cambodia vietnam).exclude?(country_slug))
85+
(%w(cambodia vietnam).exclude?(country_slug))
8686
end
8787

8888
def ss_marriage_not_possible?(country_slug, calculator)

lib/smart_answer/calculators/maternity_benefits_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def smp_lel
4242
end
4343
end
4444

45-
private
45+
private
4646

4747
def due_date_before_7th_april_2013?
4848
@due_date < Date.parse("7th April 2013")

lib/smart_answer/calculators/minimum_wage_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def any_overtime_hours_worked?
190190
overtime_hours > 0
191191
end
192192

193-
protected
193+
protected
194194

195195
def weekly_multiplier
196196
(@pay_frequency.to_f / 7).round(3)

lib/smart_answer/calculators/part_year_profit_tax_credits_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def pro_rata_taxable_profit
6060
(profit_per_day * award_period.number_of_days).floor
6161
end
6262

63-
private
63+
private
6464

6565
def accounting_year_end_date_in_the_tax_year_that_tax_credits_award_ends
6666
accounting_date = accounts_end_month_and_day.change(year: tax_year.begins_on.year)

lib/smart_answer/calculators/pay_leave_for_parents_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def in_2015_2016_fin_year?(date)
9292
(Date.new(2015, 05, 06)..Date.new(2016, 05, 05)).cover?(date)
9393
end
9494

95-
private
95+
private
9696

9797
def saturday_before(date)
9898
(date - date.wday) - 1.day

lib/smart_answer/calculators/plan_adoption_leave.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def period_of_additional_leave
5959
period_of_ordinary_leave && weeks_later(period_of_ordinary_leave, 26)
6060
end
6161

62-
private
62+
private
6363
def weeks_later(range, weeks)
6464
(range.first + weeks * 7) .. (range.last + weeks * 7)
6565
end

lib/smart_answer/calculators/plan_maternity_leave.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def period_of_additional_leave
5454
period_of_ordinary_leave && weeks_later(period_of_ordinary_leave, 26)
5555
end
5656

57-
private
57+
private
5858
def weeks_later(range, weeks)
5959
(range.first + weeks * 7) .. (range.last + weeks * 7)
6060
end

lib/smart_answer/calculators/self_assessment_penalties.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def late_payment_penalty
8080
end
8181
end
8282

83-
private
83+
private
8484
def overdue_filing_days
8585
(filing_date - filing_deadline).to_i
8686
end

lib/smart_answer/calculators/state_pension_topup_calculator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def lump_sum_and_age(dob, weekly_amount, gender)
1515
age = age_at_date(dob, TOPUP_START_DATE)
1616
(TOPUP_START_DATE.year..TOPUP_END_DATE.year).each do |_|
1717
break if birthday_after_topup_end?(dob, age)
18-
rows << {amount: lump_sum_amount(age, weekly_amount), age: age} if age >= retirement_age(gender)
18+
rows << { amount: lump_sum_amount(age, weekly_amount), age: age } if age >= retirement_age(gender)
1919
age += 1
2020
end
2121
rows

lib/smart_answer/calculators/vat_payment_deadlines.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def funds_received_by
5151
end
5252
end
5353

54-
private
54+
private
5555

5656
def end_of_month_after(date)
5757
1.month.since(date).end_of_month

lib/smart_answer/erb_renderer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def erb_template_path
3838
@template_directory.join(erb_template_name)
3939
end
4040

41-
private
41+
private
4242

4343
def erb_template_name
4444
"#{@template_name}.govspeak.erb"

lib/smart_answer/flow.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ def questions
9595
end
9696

9797
def node_exists?(node_or_name)
98-
@nodes.any? {|n| n.name == node_or_name.to_sym }
98+
@nodes.any? { |n| n.name == node_or_name.to_sym }
9999
end
100100

101101
def node(node_or_name)
102-
@nodes.find {|n| n.name == node_or_name.to_sym } or raise "Node '#{node_or_name}' does not exist"
102+
@nodes.find { |n| n.name == node_or_name.to_sym } or raise "Node '#{node_or_name}' does not exist"
103103
end
104104

105105
def start_state
@@ -131,7 +131,7 @@ def normalize_responses(responses)
131131

132132
class InvalidStatus < StandardError; end
133133

134-
private
134+
private
135135

136136
def add_question(klass, name, options = {}, &block)
137137
add_node klass.new(self, name, options, &block)

lib/smart_answer/question/checkbox.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse_input(raw_input)
3131
end
3232

3333
def to_response(input)
34-
input.split(',').reject {|v| v == NONE_OPTION }
34+
input.split(',').reject { |v| v == NONE_OPTION }
3535
end
3636
end
3737
end

lib/smart_answer/question/country_select.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def country_in(countries)
2121
end
2222

2323
def valid_option?(option)
24-
options.map {|v| v.slug}.include? (option.to_s)
24+
options.map { |v| v.slug }.include? (option.to_s)
2525
end
2626

2727
def parse_input(raw_input)
@@ -40,7 +40,7 @@ def load_countries
4040
countries = countries.reject { |c| @exclude_countries.include?(c.slug) }
4141
end
4242
if @additional_countries
43-
countries = (countries + @additional_countries).sort {|a, b| a.name <=> b.name }
43+
countries = (countries + @additional_countries).sort { |a, b| a.name <=> b.name }
4444
end
4545
countries
4646
end

lib/smart_answer/question/date.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,25 @@ def range
5252
end
5353

5454
def parse_input(input)
55-
date = case input
55+
date = begin
56+
case input
5657
when Hash, ActiveSupport::HashWithIndifferentAccess
57-
input = input.symbolize_keys
58-
year_month_and_day = [
59-
default_year || input[:year],
60-
default_month || input[:month],
61-
default_day || input[:day],
62-
]
63-
raise InvalidResponse unless year_month_and_day.all?(&:present?)
64-
::Date.new(*year_month_and_day.map(&:to_i))
58+
input = input.symbolize_keys
59+
year_month_and_day = [
60+
default_year || input[:year],
61+
default_month || input[:month],
62+
default_day || input[:day],
63+
]
64+
raise InvalidResponse unless year_month_and_day.all?(&:present?)
65+
::Date.new(*year_month_and_day.map(&:to_i))
6566
when String
66-
::Date.parse(input)
67+
::Date.parse(input)
6768
when ::Date
68-
input
69+
input
6970
else
70-
raise InvalidResponse, "Bad date", caller
71+
raise InvalidResponse, "Bad date", caller
7172
end
73+
end
7274
validate_input(date) if @validate_in_range
7375
date
7476
rescue ArgumentError => e

lib/smart_answer/state.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def save_input_as(name)
3636
__send__ "#{name}=", responses.last
3737
end
3838

39-
private
39+
private
4040

4141
def initialize_copy(orig)
4242
super

lib/smart_answer_files.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def empty?
1818
existing_paths.empty?
1919
end
2020

21-
private
21+
private
2222

2323
def relative_paths
2424
@relative_paths ||= all_paths.collect do |path|

lib/smart_answer_flows/energy-grants-calculator.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def define
7272
validate(:error_perm_prop_house) { |r| ! r.include?('permission,property,social_housing') }
7373
validate(:error_prop_house) { |r| ! r.include?('property,social_housing') }
7474
validate(:error_perm_prop) { |r| ! r.include?('permission,property') }
75-
validate(:error_perm_house) { |r| ! r.include?('permission,social_housing')}
75+
validate(:error_perm_house) { |r| ! r.include?('permission,social_housing') }
7676

7777
next_node do
7878
question :date_of_birth? # Q3
@@ -163,9 +163,9 @@ def define
163163
response == 'esa' ||
164164
response == 'working_tax_credit' ||
165165
response.include?('universal_credit') ||
166-
%w{child_tax_credit esa income_support jsa pension_credit}.all? {|key| response.include? key} ||
167-
%w{child_tax_credit esa income_support pension_credit}.all? {|key| response.include? key} ||
168-
%w{child_tax_credit esa jsa pension_credit}.all? {|key| response.include? key}
166+
%w{child_tax_credit esa income_support jsa pension_credit}.all? { |key| response.include? key } ||
167+
%w{child_tax_credit esa income_support pension_credit}.all? { |key| response.include? key } ||
168+
%w{child_tax_credit esa jsa pension_credit}.all? { |key| response.include? key }
169169
end
170170

171171
next_node do |response|

0 commit comments

Comments
 (0)