Skip to content

Commit

Permalink
Minimal supported Ruby version is now 2.5, update gemspec and RuboCop…
Browse files Browse the repository at this point in the history
… target version accordingly

Refs. decf428
  • Loading branch information
mshibuya committed Aug 29, 2021
1 parent 15e10fb commit 17e20b6
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ AllCops:
- 'spec/dummy_app/db/schema.rb'
- 'spec/dummy_app/tmp/**/*'
- 'vendor/bundle/**/*'
TargetRubyVersion: 2.5

Layout/AccessModifierIndentation:
EnforcedStyle: outdent
Expand Down Expand Up @@ -80,6 +81,9 @@ Style/FrozenStringLiteralComment:
Style/Lambda:
Enabled: false

Style/NumericPredicate:
Enabled: false

Style/RaiseArgs:
EnforcedStyle: compact

Expand Down
4 changes: 0 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
Bundler/OrderedGems:
Exclude:
- 'Gemfile'
- 'gemfiles/rails_5.0.gemfile'
- 'gemfiles/rails_5.1.gemfile'
- 'gemfiles/rails_5.2.gemfile'
- 'gemfiles/rails_6.0.gemfile'
- 'spec/dummy_app/Gemfile'

# Offense count: 1
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def bulk_action

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]))
scope = model_config.abstract_model.scoped
if auth_scope = @authorization_adapter && @authorization_adapter.query(auth_scope_key, model_config.abstract_model)
if auth_scope = @authorization_adapter&.query(auth_scope_key, model_config.abstract_model)
scope = scope.merge(auth_scope)
end
scope = scope.instance_eval(&additional_scope) if additional_scope
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/bulk_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BulkDelete < RailsAdmin::Config::Actions::Base
destroyed = processed_objects.select(&:destroyed?)
not_destroyed = processed_objects - destroyed
destroyed.each do |object|
@auditing_adapter && @auditing_adapter.delete_object(object, @abstract_model, _current_user)
@auditing_adapter&.delete_object(object, @abstract_model, _current_user)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/config/actions/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class Dashboard < RailsAdmin::Config::Actions::Base

register_instance_option :controller do
proc do
@history = @auditing_adapter && @auditing_adapter.latest(@action.auditing_versions_limit) || []
@history = @auditing_adapter&.latest(@action.auditing_versions_limit) || []
if @action.statistics?
@abstract_models = RailsAdmin::Config.visible_models(controller: self).collect(&:abstract_model)

@most_recent_created = {}
@count = {}
@max = 0
@abstract_models.each do |t|
scope = @authorization_adapter && @authorization_adapter.query(:index, t)
scope = @authorization_adapter&.query(:index, t)
current_count = t.count({}, scope)
@max = current_count > @max ? current_count : @max
@count[t.model.name] = current_count
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Delete < RailsAdmin::Config::Actions::Base
elsif request.delete? # DESTROY

redirect_path = nil
@auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user)
@auditing_adapter&.delete_object(@object, @abstract_model, _current_user)
if @object.destroy
flash[:success] = t('admin.flash.successful', name: @model_config.label, action: t('admin.actions.delete.done'))
redirect_path = index_path
Expand Down
4 changes: 2 additions & 2 deletions lib/rails_admin/config/actions/edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class Edit < RailsAdmin::Config::Actions::Base
sanitize_params_for!(request.xhr? ? :modal : :update)

@object.set_attributes(params[@abstract_model.param_key])
@authorization_adapter && @authorization_adapter.authorize(:update, @abstract_model, @object)
@authorization_adapter&.authorize(:update, @abstract_model, @object)
changes = @object.changes
if @object.save
@auditing_adapter && @auditing_adapter.update_object(@object, @abstract_model, _current_user, changes)
@auditing_adapter&.update_object(@object, @abstract_model, _current_user, changes)
respond_to do |format|
format.html { redirect_to_on_success }
format.js { render json: {id: @object.id.to_s, label: @model_config.with(object: @object).object_label} }
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/history_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HistoryIndex < RailsAdmin::Config::Actions::Base
register_instance_option :controller do
proc do
@general = true
@history = @auditing_adapter && @auditing_adapter.listing_for_model(@abstract_model, params[:query], params[:sort], params[:sort_reverse], params[:all], params[:page]) || []
@history = @auditing_adapter&.listing_for_model(@abstract_model, params[:query], params[:sort], params[:sort_reverse], params[:all], params[:page]) || []

render @action.template_name
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/history_show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HistoryShow < RailsAdmin::Config::Actions::Base
register_instance_option :controller do
proc do
@general = false
@history = @auditing_adapter && @auditing_adapter.listing_for_object(@abstract_model, @object, params[:query], params[:sort], params[:sort_reverse], params[:all], params[:page]) || []
@history = @auditing_adapter&.listing_for_object(@abstract_model, @object, params[:query], params[:sort], params[:sort_reverse], params[:all], params[:page]) || []

render @action.template_name
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rails_admin/config/actions/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class New < RailsAdmin::Config::Actions::Base
if request.get? # NEW

@object = @abstract_model.new
@authorization_adapter && @authorization_adapter.attributes_for(:new, @abstract_model).each do |name, value|
@authorization_adapter&.attributes_for(:new, @abstract_model)&.each do |name, value|
@object.send("#{name}=", value)
end
if object_params = params[@abstract_model.param_key]
Expand All @@ -36,10 +36,10 @@ class New < RailsAdmin::Config::Actions::Base
sanitize_params_for!(request.xhr? ? :modal : :create)

@object.set_attributes(params[@abstract_model.param_key])
@authorization_adapter && @authorization_adapter.authorize(:create, @abstract_model, @object)
@authorization_adapter&.authorize(:create, @abstract_model, @object)

if @object.save
@auditing_adapter && @auditing_adapter.create_object(@object, @abstract_model, _current_user)
@auditing_adapter&.create_object(@object, @abstract_model, _current_user)
respond_to do |format|
format.html { redirect_to_on_success }
format.js { render json: {id: @object.id.to_s, label: @model_config.with(object: @object).object_label} }
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.included(base)

def has_option?(name) # rubocop:disable Naming/PredicateName
options = self.class.instance_variable_get('@config_options')
options && options.key?(name)
options&.key?(name)
end

# Register an instance option for this object only
Expand Down
14 changes: 7 additions & 7 deletions lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ def virtual?
table_name, column = f.split '.'
type = nil
elsif f.is_a?(Hash) # <Model|table_name> => <attribute|column>
am = f.keys.first.is_a?(Class) && AbstractModel.new(f.keys.first)
table_name = am && am.table_name || f.keys.first
am = AbstractModel.new(f.keys.first) if f.keys.first.is_a?(Class)
table_name = am&.table_name || f.keys.first
column = f.values.first
property = am && am.properties.detect { |p| p.name == f.values.first.to_sym }
type = property && property.type
property = am&.properties&.detect { |p| p.name == f.values.first.to_sym }
type = property&.type
else # <attribute|column>
am = (association? ? associated_model_config.abstract_model : abstract_model)
table_name = am.table_name
column = f
property = am.properties.detect { |p| p.name == f.to_sym }
type = property && property.type
type = property&.type
end
{column: "#{table_name}.#{column}", type: (type || :string)}
end
Expand Down Expand Up @@ -155,7 +155,7 @@ def virtual?
#
# @see RailsAdmin::AbstractModel.properties
register_instance_option :length do
@length ||= properties && properties.length
@length ||= properties&.length
end

# Accessor for field's length restrictions per validations
Expand Down Expand Up @@ -193,7 +193,7 @@ def virtual?
#
# @see RailsAdmin::AbstractModel.properties
register_instance_option :serial? do
properties && properties.serial?
properties&.serial?
end

register_instance_option :view_helper do
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def resource_url(thumb = false)

def value
attachment = super
attachment if attachment && attachment.attached?
attachment if attachment&.attached?
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions lib/rails_admin/config/fields/types/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ def value
end

register_instance_option :strftime_format do
begin
::I18n.t(date_format, scope: i18n_scope, raise: true)
rescue ::I18n::ArgumentError
"%B %d, %Y %H:%M"
end
::I18n.t(date_format, scope: i18n_scope, raise: true)
rescue ::I18n::ArgumentError
"%B %d, %Y %H:%M"
end

register_instance_option :datepicker_options do
Expand Down
8 changes: 3 additions & 5 deletions lib/rails_admin/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ def self.add_extension(extension_key, extension_definition, options = {})
# Setup all extensions for testing
def self.setup_all_extensions
(AUTHORIZATION_ADAPTERS.values + AUDITING_ADAPTERS.values).each do |klass|
begin
klass.setup if klass.respond_to? :setup
rescue # rubocop:disable Lint/HandleExceptions, Style/RescueStandardError
# ignore errors
end
klass.setup if klass.respond_to? :setup
rescue # rubocop:disable Lint/HandleExceptions, Style/RescueStandardError
# ignore errors
end
end
end
4 changes: 2 additions & 2 deletions lib/rails_admin/extensions/cancancan/authorization_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def query(action, abstract_model)
# records. It should return a hash of attributes which match what the user
# is authorized to create.
def attributes_for(action, abstract_model)
@controller.current_ability.attributes_for(action, abstract_model && abstract_model.model)
@controller.current_ability.attributes_for(action, abstract_model&.model)
end

private

def resolve_action_and_subject(action, abstract_model, model_object)
subject = model_object || abstract_model && abstract_model.model
subject = model_object || abstract_model&.model
if subject
[action, subject]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/extensions/paper_trail/auditing_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def self.setup

def initialize(controller, user_class = 'User', version_class = '::Version')
@controller = controller
@controller.send(:set_paper_trail_whodunnit) if @controller
@controller&.send(:set_paper_trail_whodunnit)
begin
@user_class = user_class.to_s.constantize
rescue NameError
Expand Down
6 changes: 3 additions & 3 deletions lib/rails_admin/extensions/pundit/authorization_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(controller)
# AbstractModel instance that applies. The third argument is the actual model
# instance if it is available.
def authorize(action, abstract_model = nil, model_object = nil)
record = model_object || abstract_model && abstract_model.model
record = model_object || abstract_model&.model
if action && !policy(record).send(action_for_pundit(action))
raise ::Pundit::NotAuthorizedError.new("not allowed to #{action} this #{record}")
end
Expand All @@ -33,7 +33,7 @@ def authorize(action, abstract_model = nil, model_object = nil)
# This takes the same arguments as +authorize+. The difference is that this will
# return a boolean whereas +authorize+ will raise an exception when not authorized.
def authorized?(action, abstract_model = nil, model_object = nil)
record = model_object || abstract_model && abstract_model.model
record = model_object || abstract_model&.model
policy(record).send(action_for_pundit(action)) if action
end

Expand All @@ -50,7 +50,7 @@ def query(_action, abstract_model)
# records. It should return a hash of attributes which match what the user
# is authorized to create.
def attributes_for(action, abstract_model)
record = abstract_model && abstract_model.model
record = abstract_model&.model
policy(record).try(:attributes_for, action) || {}
end

Expand Down
2 changes: 1 addition & 1 deletion rails_admin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/sferik/rails_admin'
spec.name = 'rails_admin'
spec.require_paths = %w(lib)
spec.required_ruby_version = '>= 2.2.2'
spec.required_ruby_version = '>= 2.5.0'
spec.required_rubygems_version = '>= 1.8.11'
spec.summary = 'Admin for Rails'
spec.version = RailsAdmin::Version
Expand Down
12 changes: 6 additions & 6 deletions spec/rails_admin/support/csv_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
it 'exports to ISO-8859-1' do
expect(subject[1]).to eq 'ISO-8859-1'
expect(subject[2].encoding).to eq Encoding::ISO_8859_1
expect(subject[2].unpack('H*').first).
expect(subject[2].unpack1('H*')).
to eq '4e756d6265722c4e616d650a312c4a6f73e80a'
end
end
Expand All @@ -75,7 +75,7 @@
it 'exports to UTF-8 with BOM' do
expect(subject[1]).to eq 'UTF-8'
expect(subject[2].encoding).to eq Encoding::UTF_8
expect(subject[2].unpack('H*').first).
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265722c4e616d650a312ce381aae381bee381880a' # have BOM
end
end
Expand All @@ -86,7 +86,7 @@
it 'exports to Shift_JIS' do
expect(subject[1]).to eq 'Shift_JIS'
expect(subject[2].encoding).to eq Encoding::Shift_JIS
expect(subject[2].unpack('H*').first).
expect(subject[2].unpack1('H*')).
to eq '4e756d6265722c4e616d650a312c82c882dc82a60a'
end
end
Expand All @@ -97,7 +97,7 @@
it 'encodes to expected byte sequence' do
expect(subject[1]).to eq 'UTF-16'
expect(subject[2].encoding).to eq Encoding::UTF_16
expect(subject[2].unpack('H*').first.force_encoding('US-ASCII')).
expect(subject[2].unpack1('H*').force_encoding('US-ASCII')).
to eq 'feff004e0075006d006200650072002c004e0061006d0065000a0031002c306a307e3048000a'
end
end
Expand All @@ -106,15 +106,15 @@
context "when options keys are symbolized" do
let(:options) { {encoding_to: 'UTF-8', generator: {col_sep: '___'}} }
it "uses the column separator specified" do
expect(subject[2].unpack('H*').first).
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a'
end
end

context "when options keys are string" do
let(:options) { {'encoding_to' => 'UTF-8', 'generator' => {'col_sep' => '___'}} }
it "uses the column separator specified" do
expect(subject[2].unpack('H*').first).
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a'
end
end
Expand Down

0 comments on commit 17e20b6

Please sign in to comment.