Skip to content

Commit d370a2a

Browse files
authored
Fix several RuboCop TODO items (#3478)
Handle the simple and straightforward fixes: - Lint/AmbiguousBlockAssociation - Lint/AssignmentInCondition - Lint/DuplicateMethods - Lint/RequireParentheses - Naming/HeredocDelimiterNaming
1 parent f92bdc5 commit d370a2a

23 files changed

Lines changed: 76 additions & 106 deletions

File tree

.rubocop_todo.yml

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,12 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: IgnoredMethods.
11-
Lint/AmbiguousBlockAssociation:
12-
Exclude:
13-
- "spec/rails_admin/install_generator_spec.rb"
14-
15-
# Offense count: 18
16-
# Configuration parameters: AllowSafeAssignment.
17-
Lint/AssignmentInCondition:
18-
Exclude:
19-
- "app/controllers/rails_admin/main_controller.rb"
20-
- "app/helpers/rails_admin/application_helper.rb"
21-
- "lib/rails_admin/abstract_model.rb"
22-
- "lib/rails_admin/adapters/active_record.rb"
23-
- "lib/rails_admin/config/actions/export.rb"
24-
- "lib/rails_admin/config/actions/index.rb"
25-
- "lib/rails_admin/config/actions/new.rb"
26-
- "lib/rails_admin/config/fields/factories/active_storage.rb"
27-
- "lib/rails_admin/config/fields/factories/association.rb"
28-
- "lib/rails_admin/config/fields/factories/carrierwave.rb"
29-
- "lib/rails_admin/config/fields/factories/devise.rb"
30-
- "lib/rails_admin/config/fields/factories/dragonfly.rb"
31-
- "lib/rails_admin/config/fields/factories/paperclip.rb"
32-
- "lib/rails_admin/config/fields/types/datetime.rb"
33-
- "spec/dummy_app/db/seeds.rb"
34-
359
# Offense count: 51
3610
# Configuration parameters: AllowedMethods.
3711
# AllowedMethods: enums
3812
Lint/ConstantDefinitionInBlock:
3913
Enabled: false
4014

41-
# Offense count: 1
42-
Lint/DuplicateMethods:
43-
Exclude:
44-
- "lib/rails_admin/config.rb"
45-
46-
# Offense count: 1
47-
Lint/RequireParentheses:
48-
Exclude:
49-
- "spec/spec_helper.rb"
50-
5115
# Offense count: 1
5216
Lint/ReturnInVoidContext:
5317
Exclude:
@@ -71,18 +35,6 @@ Naming/AccessorMethodName:
7135
- "app/controllers/rails_admin/main_controller.rb"
7236
- "lib/rails_admin/abstract_model.rb"
7337

74-
# Offense count: 6
75-
# Configuration parameters: ForbiddenDelimiters.
76-
# ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
77-
Naming/HeredocDelimiterNaming:
78-
Exclude:
79-
- "app/controllers/rails_admin/main_controller.rb"
80-
- "lib/rails_admin/adapters/mongoid.rb"
81-
- "lib/rails_admin/config/fields/base.rb"
82-
- "lib/rails_admin/engine.rb"
83-
- "lib/rails_admin/extensions/paper_trail/auditing_adapter.rb"
84-
- "spec/factories.rb"
85-
8638
# Offense count: 2
8739
# Configuration parameters: EnforcedStyleForLeadingUnderscores.
8840
# SupportedStylesForLeadingUnderscores: disallowed, required, optional

app/controllers/rails_admin/main_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ def bulk_action
1313

1414
def list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params, pagination = !(params[:associated_collection] || params[:all] || params[:bulk_ids]))
1515
scope = model_config.scope
16-
if auth_scope = @authorization_adapter&.query(auth_scope_key, model_config.abstract_model)
17-
scope = scope.merge(auth_scope)
18-
end
16+
auth_scope = @authorization_adapter&.query(auth_scope_key, model_config.abstract_model)
17+
scope = scope.merge(auth_scope) if auth_scope
1918
scope = scope.instance_eval(&additional_scope) if additional_scope
2019
get_collection(model_config, scope, pagination)
2120
end

lib/rails_admin/abstract_model.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ def each_associated_children(object)
8686
associations.each do |association|
8787
case association.type
8888
when :has_one
89-
if child = object.send(association.name)
90-
yield(association, [child])
91-
end
89+
child = object.send(association.name)
90+
yield(association, [child]) if child
9291
when :has_many
9392
children = object.send(association.name)
9493
yield(association, Array.new(children))

lib/rails_admin/adapters/active_record.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def new(params = {})
1313
end
1414

1515
def get(id, scope = scoped)
16-
return unless object = scope.where(primary_key => id).first
16+
object = scope.where(primary_key => id).first
17+
return unless object
1718

1819
object.extend(ObjectExtension)
1920
end

lib/rails_admin/adapters/mongoid.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def all(options = {}, scope = nil)
5252
scope
5353
rescue NoMethodError => e
5454
if /page/.match?(e.message)
55-
e = e.exception <<-EOM.gsub(/^\s{12}/, '')
55+
e = e.exception <<-ERROR.gsub(/^\s{12}/, '')
5656
#{e.message}
5757
If you don't have kaminari-mongoid installed, add `gem 'kaminari-mongoid'` to your Gemfile.
58-
EOM
58+
ERROR
5959
end
6060
raise e
6161
end

lib/rails_admin/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class << self
3939
attr_accessor :included_models
4040

4141
# Fields to be hidden in show, create and update views
42-
attr_accessor :default_hidden_fields
42+
attr_reader :default_hidden_fields
4343

4444
# Default items per page value used if a model level option has not
4545
# been configured

lib/rails_admin/config/actions/export.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Export < RailsAdmin::Config::Actions::Base
1414

1515
register_instance_option :controller do
1616
proc do
17-
if format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml
17+
format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml
18+
if format
1819
request.format = format
1920
@schema = HashHelper.symbolize(params[:schema].slice(:except, :include, :methods, :only).permit!.to_h) if params[:schema] # to_json and to_xml expect symbols for keys AND values.
2021
@objects = list_entries(@model_config, :export)

lib/rails_admin/config/actions/index.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class Index < RailsAdmin::Config::Actions::Base
2020

2121
register_instance_option :breadcrumb_parent do
2222
parent_model = bindings[:abstract_model].try(:config).try(:parent)
23-
if am = parent_model && RailsAdmin.config(parent_model).try(:abstract_model)
23+
am = parent_model && RailsAdmin.config(parent_model).try(:abstract_model)
24+
if am
2425
[:index, am]
2526
else
2627
[:dashboard]

lib/rails_admin/config/actions/new.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class New < RailsAdmin::Config::Actions::Base
2121
@authorization_adapter&.attributes_for(:new, @abstract_model)&.each do |name, value|
2222
@object.send("#{name}=", value)
2323
end
24-
if object_params = params[@abstract_model.param_key]
24+
object_params = params[@abstract_model.param_key]
25+
if object_params
2526
sanitize_params_for!(request.xhr? ? :modal : :create)
2627
@object.assign_attributes(@object.attributes.merge(object_params.to_h))
2728
end

lib/rails_admin/config/fields/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ def type
301301
def value
302302
bindings[:object].safe_send(name)
303303
rescue NoMethodError => e
304-
raise e.exception <<-EOM.gsub(/^\s{10}/, '')
304+
raise e.exception <<-ERROR.gsub(/^\s{10}/, '')
305305
#{e.message}
306306
If you want to use a RailsAdmin virtual field(= a field without corresponding instance method),
307307
you should declare 'formatted_value' in the field definition.
308308
field :#{name} do
309309
formatted_value{ bindings[:object].call_some_method }
310310
end
311-
EOM
311+
ERROR
312312
end
313313

314314
# Reader for nested attributes

0 commit comments

Comments
 (0)