From 2269d2679e3c8ce18543fd18b5a17e733282e388 Mon Sep 17 00:00:00 2001 From: Sean <11340230+scarroll32@users.noreply.github.com> Date: Wed, 9 Nov 2022 14:24:29 +0100 Subject: [PATCH] Enable RuboCop `Style/HashSyntax` (#1371) --- .rubocop.yml | 3 +++ Gemfile | 6 +++--- lib/ransack.rb | 2 +- lib/ransack/configuration.rb | 24 +++++++++++------------ lib/ransack/helpers/form_builder.rb | 6 +++--- lib/ransack/nodes/attribute.rb | 4 ++-- lib/ransack/nodes/condition.rb | 12 ++++++------ lib/ransack/nodes/grouping.rb | 6 +++--- lib/ransack/nodes/node.rb | 2 +- lib/ransack/nodes/value.rb | 2 +- lib/ransack/ransacker.rb | 2 +- lib/ransack/search.rb | 4 ++-- lib/ransack/translate.rb | 2 +- spec/ransack/configuration_spec.rb | 18 ++++++++--------- spec/ransack/helpers/form_builder_spec.rb | 16 +++++++-------- spec/ransack/helpers/form_helper_spec.rb | 4 ++-- spec/ransack/translate_spec.rb | 2 +- 17 files changed, 59 insertions(+), 56 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 55715fd..d88676b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -30,6 +30,9 @@ Layout/SpaceInsideParens: Layout/TrailingEmptyLines: Enabled: true +Style/HashSyntax: + Enabled: true + Style/RedundantFileExtensionInRequire: Enabled: true diff --git a/Gemfile b/Gemfile index e405535..1985111 100644 --- a/Gemfile +++ b/Gemfile @@ -28,14 +28,14 @@ when /\// # A path gem 'actionpack', path: "#{rails}/actionpack" gem 'actionview', path: "#{rails}/actionview" when /^v/ # A tagged version - git 'https://github.com/rails/rails.git', :tag => rails do + git 'https://github.com/rails/rails.git', tag: rails do gem 'activesupport' gem 'activemodel' gem 'activerecord', require: false gem 'actionpack' end else - git 'https://github.com/rails/rails.git', :branch => rails do + git 'https://github.com/rails/rails.git', branch: rails do gem 'activesupport' gem 'activemodel' gem 'activerecord', require: false @@ -47,7 +47,7 @@ gem 'mysql2' group :test do gem 'machinist', '~> 1.0.6' gem 'rspec' - gem 'simplecov', :require => false + gem 'simplecov', require: false end gem 'rubocop', require: false diff --git a/lib/ransack.rb b/lib/ransack.rb index 2425a5f..877023f 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -12,7 +12,7 @@ end Ransack.configure do |config| Ransack::Constants::AREL_PREDICATES.each do |name| - config.add_predicate name, :arel_predicate => name + config.add_predicate name, arel_predicate: name end Ransack::Constants::DERIVED_PREDICATES.each do |args| config.add_predicate(*args) diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 1ca0434..99816ee 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -27,15 +27,15 @@ module Ransack self.predicates = PredicateCollection.new self.options = { - :search_key => :q, - :ignore_unknown_conditions => true, - :hide_sort_order_indicators => false, - :up_arrow => '▼'.freeze, - :down_arrow => '▲'.freeze, - :default_arrow => nil, - :sanitize_scope_args => true, - :postgres_fields_sort_option => nil, - :strip_whitespace => true + search_key: :q, + ignore_unknown_conditions: true, + hide_sort_order_indicators: false, + up_arrow: '▼'.freeze, + down_arrow: '▲'.freeze, + default_arrow: nil, + sanitize_scope_args: true, + postgres_fields_sort_option: nil, + strip_whitespace: true } def configure @@ -55,11 +55,11 @@ module Ransack compound_name = name + suffix self.predicates[compound_name] = Predicate.new( opts.merge( - :name => compound_name, - :arel_predicate => arel_predicate_with_suffix( + name: compound_name, + arel_predicate: arel_predicate_with_suffix( opts[:arel_predicate], suffix ), - :compound => true + compound: true ) ) end if compounds diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index bfabf63..0f9d826 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -33,7 +33,7 @@ module Ransack text = args.first i18n = options[:i18n] || {} text ||= object.translate( - method, i18n.reverse_merge(:include_associations => true) + method, i18n.reverse_merge(include_associations: true) ) if object.respond_to? :translate super(method, text, options, &block) end @@ -240,7 +240,7 @@ module Ransack def get_attribute_element(action, base) begin [ - Translate.association(base, :context => object.context), + Translate.association(base, context: object.context), collection_for_base(action, base) ] rescue UntraversableAssociationError @@ -253,7 +253,7 @@ module Ransack [ attr_from_base_and_column(base, c), Translate.attribute( - attr_from_base_and_column(base, c), :context => object.context + attr_from_base_and_column(base, c), context: object.context ) ] end diff --git a/lib/ransack/nodes/attribute.rb b/lib/ransack/nodes/attribute.rb index 64059b1..73b57f2 100644 --- a/lib/ransack/nodes/attribute.rb +++ b/lib/ransack/nodes/attribute.rb @@ -5,8 +5,8 @@ module Ransack attr_reader :name, :ransacker_args - delegate :blank?, :present?, :to => :name - delegate :engine, :to => :context + delegate :blank?, :present?, to: :name + delegate :engine, to: :context def initialize(context, name = nil, ransacker_args = []) super(context) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index d221721..d478fab 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -2,8 +2,8 @@ module Ransack module Nodes class Condition < Node i18n_word :attribute, :predicate, :combinator, :value - i18n_alias :a => :attribute, :p => :predicate, - :m => :combinator, :v => :value + i18n_alias a: :attribute, p: :predicate, + m: :combinator, v: :value attr_accessor :predicate @@ -15,10 +15,10 @@ module Ransack if attributes.size > 0 && predicate condition = self.new(context) condition.build( - :a => attributes, - :p => predicate.name, - :m => combinator, - :v => predicate.wants_array ? Array(values) : [values] + a: attributes, + p: predicate.name, + m: combinator, + v: predicate.wants_array ? Array(values) : [values] ) # TODO: Figure out what to do with multiple types of attributes, # if anything. Tempted to go with "garbage in, garbage out" here. diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 38d794d..1bf7055 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -7,9 +7,9 @@ module Ransack alias :m= :combinator= i18n_word :condition, :and, :or - i18n_alias :c => :condition, :n => :and, :o => :or + i18n_alias c: :condition, n: :and, o: :or - delegate :each, :to => :values + delegate :each, to: :values def initialize(context, combinator = nil) super(context) @@ -22,7 +22,7 @@ module Ransack def translate(key, options = {}) super or Translate.attribute( - key.to_s, options.merge(:context => context) + key.to_s, options.merge(context: context) ) end diff --git a/lib/ransack/nodes/node.rb b/lib/ransack/nodes/node.rb index 3af1e8b..e614e9c 100644 --- a/lib/ransack/nodes/node.rb +++ b/lib/ransack/nodes/node.rb @@ -2,7 +2,7 @@ module Ransack module Nodes class Node attr_reader :context - delegate :contextualize, :to => :context + delegate :contextualize, to: :context class_attribute :i18n_words class_attribute :i18n_aliases self.i18n_words = [] diff --git a/lib/ransack/nodes/value.rb b/lib/ransack/nodes/value.rb index 76b5c93..6b75925 100644 --- a/lib/ransack/nodes/value.rb +++ b/lib/ransack/nodes/value.rb @@ -2,7 +2,7 @@ module Ransack module Nodes class Value < Node attr_accessor :value - delegate :present?, :blank?, :to => :value + delegate :present?, :blank?, to: :value def initialize(context, value = nil) super(context) diff --git a/lib/ransack/ransacker.rb b/lib/ransack/ransacker.rb index 3ae0c20..a677880 100644 --- a/lib/ransack/ransacker.rb +++ b/lib/ransack/ransacker.rb @@ -3,7 +3,7 @@ module Ransack attr_reader :name, :type, :formatter, :args - delegate :call, :to => :@callable + delegate :call, to: :@callable def initialize(klass, name, opts = {}, &block) @klass, @name = klass, name diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index 95980c0..fd9d04f 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -9,10 +9,10 @@ module Ransack attr_reader :base, :context - delegate :object, :klass, :to => :context + delegate :object, :klass, to: :context delegate :new_grouping, :new_condition, :build_grouping, :build_condition, - :translate, :to => :base + :translate, to: :base def initialize(object, params = {}, options = {}) strip_whitespace = options.fetch(:strip_whitespace, Ransack.options[:strip_whitespace]) diff --git a/lib/ransack/translate.rb b/lib/ransack/translate.rb index 6e1e938..0391c8d 100644 --- a/lib/ransack/translate.rb +++ b/lib/ransack/translate.rb @@ -66,7 +66,7 @@ module Ransack [:"ransack.associations.#{i18n_key(context.klass)}.#{key}"] end defaults << context.traverse(key).model_name.human - options = { :count => 1, :default => defaults } + options = { count: 1, default: defaults } I18n.translate(defaults.shift, **options) end diff --git a/spec/ransack/configuration_spec.rb b/spec/ransack/configuration_spec.rb index 1eba074..9d23214 100644 --- a/spec/ransack/configuration_spec.rb +++ b/spec/ransack/configuration_spec.rb @@ -20,7 +20,7 @@ module Ransack Ransack.configure do |config| config.add_predicate( :test_predicate_without_compound, - :compounds => false + compounds: false ) end expect(Ransack.predicates) @@ -138,8 +138,8 @@ module Ransack Ransack.configure do |config| config.add_predicate( :test_array_predicate, - :wants_array => true, - :compounds => true + wants_array: true, + compounds: true ) end @@ -153,11 +153,11 @@ module Ransack Ransack.configure do |config| config.add_predicate( :test_in_predicate, - :arel_predicate => 'in' + arel_predicate: 'in' ) config.add_predicate( :test_not_in_predicate, - :arel_predicate => 'not_in' + arel_predicate: 'not_in' ) end @@ -171,13 +171,13 @@ module Ransack Ransack.configure do |config| config.add_predicate( :test_in_predicate_no_array, - :arel_predicate => 'in', - :wants_array => false + arel_predicate: 'in', + wants_array: false ) config.add_predicate( :test_not_in_predicate_no_array, - :arel_predicate => 'not_in', - :wants_array => false + arel_predicate: 'not_in', + wants_array: false ) end diff --git a/spec/ransack/helpers/form_builder_spec.rb b/spec/ransack/helpers/form_builder_spec.rb index bc92601..804a506 100644 --- a/spec/ransack/helpers/form_builder_spec.rb +++ b/spec/ransack/helpers/form_builder_spec.rb @@ -26,7 +26,7 @@ module Ransack # @s.created_at_eq = date_values # This works in Rails 4.x but not 3.x @s.created_at_eq = [2011, 1, 2, 3, 4, 5] # so we have to do this html = @f.datetime_select( - :created_at_eq, :use_month_numbers => true, :include_seconds => true + :created_at_eq, use_month_numbers: true, include_seconds: true ) date_values.each { |val| expect(html).to include date_select_html(val) } end @@ -70,13 +70,13 @@ module Ransack describe '#sort_link' do it 'sort_link for ransack attribute' do - sort_link = @f.sort_link :name, :controller => 'people' + sort_link = @f.sort_link :name, controller: 'people' expect(sort_link).to match /people\?q(%5B|\[)s(%5D|\])=name\+asc/ expect(sort_link).to match /sort_link/ expect(sort_link).to match /Full Name<\/a>/ end it 'sort_link for common attribute' do - sort_link = @f.sort_link :id, :controller => 'people' + sort_link = @f.sort_link :id, controller: 'people' expect(sort_link).to match /id<\/a>/ end end @@ -99,14 +99,14 @@ module Ransack it 'returns ransackable attributes for associations with :associations' do attributes = Person.ransackable_attributes + Article.ransackable_attributes.map { |a| "articles_#{a}" } - html = @f.attribute_select(:associations => ['articles']) + html = @f.attribute_select(associations: ['articles']) expect(html.split(/\n/).size).to eq(attributes.size) attributes.each do |attribute| expect(html).to match // end @@ -121,19 +121,19 @@ module Ransack end end it 'filters predicates with single-value :only' do - html = @f.predicate_select :only => 'eq' + html = @f.predicate_select only: 'eq' Predicate.names.reject { |k| k =~ /^eq/ }.each do |key| expect(html).not_to match /