diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml new file mode 100644 index 000000000..99eaaa203 --- /dev/null +++ b/.github/workflows/rubocop.yml @@ -0,0 +1,20 @@ +name: rubocop + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v2 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.7.2 + - name: Install gems + run: bundle install --jobs 4 --retry 3 + - name: Run RuboCop + run: bundle exec rubocop --parallel diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 000000000..c5d36102b --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,42 @@ +AllCops: + # TargetRubyVersion value is based on `required_ruby_version = '>= 2.3'` in the ransack.gemspec + TargetRubyVersion: 2.3 + + DisabledByDefault: true + +Layout/EmptyLineAfterMagicComment: + Enabled: true + +Layout/EmptyLineBetweenDefs: + Enabled: true + +Layout/EmptyLines: + Enabled: true + +Layout/FirstArrayElementIndentation: + EnforcedStyle: consistent + +Layout/SpaceAfterComma: + Enabled: true + +Layout/SpaceInsideBlockBraces: + Enabled: true + +Layout/SpaceInsideHashLiteralBraces: + Enabled: true + +Layout/SpaceInsideParens: + Enabled: true + +Layout/TrailingEmptyLines: + Enabled: true + +Style/RedundantReturn: + Enabled: true + +Style/SelfAssignment: + Enabled: true + +Style/Semicolon: + Enabled: true + diff --git a/Gemfile b/Gemfile index ea198d7d2..633fd2e6f 100644 --- a/Gemfile +++ b/Gemfile @@ -49,3 +49,8 @@ group :test do gem 'rspec', '~> 3' gem 'simplecov', :require => false end + +# RuboCop 0.81.0 is the last version which supports Ruby 2.3. +# Once Ransack required_ruby_version is bumped, RuboCop version can be bumped. +# https://github.com/rubocop-hq/rubocop/pull/7869 +gem 'rubocop', '=0.81.0', require: false diff --git a/lib/polyamorous/polyamorous.rb b/lib/polyamorous/polyamorous.rb index 26f3322c2..8b9074dd9 100644 --- a/lib/polyamorous/polyamorous.rb +++ b/lib/polyamorous/polyamorous.rb @@ -11,7 +11,7 @@ module Polyamorous require 'polyamorous/join' require 'polyamorous/swapping_reflection_class' - ar_version = ::ActiveRecord::VERSION::STRING[0,3] + ar_version = ::ActiveRecord::VERSION::STRING[0, 3] %w(join_association join_dependency reflection).each do |file| require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}" end diff --git a/lib/ransack.rb b/lib/ransack.rb index 75b3830ca..fa509d4ef 100644 --- a/lib/ransack.rb +++ b/lib/ransack.rb @@ -7,7 +7,7 @@ module Ransack extend Configuration - class UntraversableAssociationError < StandardError; end; + class UntraversableAssociationError < StandardError; end end Ransack.configure do |config| diff --git a/lib/ransack/adapters/active_record/ransack/constants.rb b/lib/ransack/adapters/active_record/ransack/constants.rb index 8ef6a03e6..dede8161f 100644 --- a/lib/ransack/adapters/active_record/ransack/constants.rb +++ b/lib/ransack/adapters/active_record/ransack/constants.rb @@ -97,7 +97,7 @@ module Constants arel_predicate: proc { |v| v ? EQ : NOT_EQ }, compounds: false, type: :boolean, - validator: proc { |v| BOOLEAN_VALUES.include?(v)}, + validator: proc { |v| BOOLEAN_VALUES.include?(v) }, formatter: proc { |v| nil } } ], diff --git a/lib/ransack/constants.rb b/lib/ransack/constants.rb index 5437710f6..3a193b4a1 100644 --- a/lib/ransack/constants.rb +++ b/lib/ransack/constants.rb @@ -36,7 +36,7 @@ module Constants 'lt'.freeze, 'lteq'.freeze, 'gt'.freeze, 'gteq'.freeze, 'in'.freeze, 'not_in'.freeze - ].freeze + ].freeze A_S_I = ['a'.freeze, 's'.freeze, 'i'.freeze].freeze EQ = 'eq'.freeze @@ -52,4 +52,3 @@ module Constants RANSACK_SLASH_SEARCHES_SLASH_SEARCH = 'ransack/searches/search'.freeze end end - diff --git a/lib/ransack/helpers.rb b/lib/ransack/helpers.rb index 6e0c616bb..799e53e74 100644 --- a/lib/ransack/helpers.rb +++ b/lib/ransack/helpers.rb @@ -1,2 +1,2 @@ require 'ransack/helpers/form_builder' -require 'ransack/helpers/form_helper' \ No newline at end of file +require 'ransack/helpers/form_helper' diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 2ee3c25f7..bfabf631b 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -45,9 +45,9 @@ def submit(value = nil, options = {}) end def attribute_select(options = nil, html_options = nil, action = nil) - options = options || {} - html_options = html_options || {} - action = action || Constants::SEARCH + options ||= {} + html_options ||= {} + action ||= Constants::SEARCH default = options.delete(:default) raise ArgumentError, formbuilder_error_message( "#{action}_select") unless object.respond_to?(:context) diff --git a/lib/ransack/nodes/attribute.rb b/lib/ransack/nodes/attribute.rb index 0bfc44055..64059b147 100644 --- a/lib/ransack/nodes/attribute.rb +++ b/lib/ransack/nodes/attribute.rb @@ -30,7 +30,7 @@ def associated_collection? def type if ransacker - return ransacker.type + ransacker.type else context.type_for(self) end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index eb26b6a31..d2217216e 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -127,7 +127,6 @@ def combinator=(val) alias :m= :combinator= alias :m :combinator - # == build_attribute # # This method was originally called from Nodes::Grouping#new_condition @@ -263,7 +262,6 @@ def attr_value_for_attribute(attr) attr.attr end - def default_type predicate.type || (attributes.first && attributes.first.type) end diff --git a/lib/ransack/nodes/sort.rb b/lib/ransack/nodes/sort.rb index 923558351..5fc90b43f 100644 --- a/lib/ransack/nodes/sort.rb +++ b/lib/ransack/nodes/sort.rb @@ -9,7 +9,7 @@ class Sort < Node class << self def extract(context, str) return unless str - attr, direction = str.split(/\s+/,2) + attr, direction = str.split(/\s+/, 2) self.new(context).build(name: attr, dir: direction) end end diff --git a/lib/ransack/nodes/value.rb b/lib/ransack/nodes/value.rb index 3c80ef771..f232d6034 100644 --- a/lib/ransack/nodes/value.rb +++ b/lib/ransack/nodes/value.rb @@ -50,7 +50,7 @@ def cast_to_date(val) y, m, d = *[val].flatten m ||= 1 d ||= 1 - Date.new(y,m,d) rescue nil + Date.new(y, m, d) rescue nil end end diff --git a/ransack.gemspec b/ransack.gemspec index 8dfe875b0..bf1331281 100644 --- a/ransack.gemspec +++ b/ransack.gemspec @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- + $:.push File.expand_path("../lib", __FILE__) require "ransack/version" @@ -6,8 +7,8 @@ Gem::Specification.new do |s| s.name = "ransack" s.version = Ransack::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["Ernie Miller", "Ryan Bigg", "Jon Atack","Sean Carroll"] - s.email = ["ernie@erniemiller.org", "radarlistener@gmail.com", "jonnyatack@gmail.com","sfcarroll@gmail.com"] + s.authors = ["Ernie Miller", "Ryan Bigg", "Jon Atack", "Sean Carroll"] + s.email = ["ernie@erniemiller.org", "radarlistener@gmail.com", "jonnyatack@gmail.com", "sfcarroll@gmail.com"] s.homepage = "https://github.com/activerecord-hackery/ransack" s.summary = %q{Object-based searching for Active Record and Mongoid (currently).} s.description = %q{Ransack is the successor to the MetaSearch gem. It improves and expands upon MetaSearch's functionality, but does not have a 100%-compatible API.} diff --git a/spec/blueprints/articles.rb b/spec/blueprints/articles.rb index 42e2f14be..98a05e37d 100644 --- a/spec/blueprints/articles.rb +++ b/spec/blueprints/articles.rb @@ -2,4 +2,4 @@ person title body -end \ No newline at end of file +end diff --git a/spec/blueprints/comments.rb b/spec/blueprints/comments.rb index 1ecc7eb69..f0cc07dcf 100644 --- a/spec/blueprints/comments.rb +++ b/spec/blueprints/comments.rb @@ -2,4 +2,4 @@ article person body -end \ No newline at end of file +end diff --git a/spec/blueprints/notes.rb b/spec/blueprints/notes.rb index f6702ca15..a2f63ee5f 100644 --- a/spec/blueprints/notes.rb +++ b/spec/blueprints/notes.rb @@ -2,4 +2,4 @@ note notable_type { "Article" } notable_id -end \ No newline at end of file +end diff --git a/spec/blueprints/tags.rb b/spec/blueprints/tags.rb index f0fb3d869..4a7d1898b 100644 --- a/spec/blueprints/tags.rb +++ b/spec/blueprints/tags.rb @@ -1,3 +1,3 @@ Tag.blueprint do name { Sham.tag_name } -end \ No newline at end of file +end diff --git a/spec/helpers/ransack_helper.rb b/spec/helpers/ransack_helper.rb index dfdaceb4c..f80e883c1 100644 --- a/spec/helpers/ransack_helper.rb +++ b/spec/helpers/ransack_helper.rb @@ -6,4 +6,4 @@ def quote_table_name(table) def quote_column_name(column) ActiveRecord::Base.connection.quote_column_name(column) end -end \ No newline at end of file +end diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 3ede567d1..851819640 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -464,9 +464,9 @@ def self.simple_escaping? Comment.create(article: Article.create(title: 'Avenger'), person: Person.create(salary: 100_000)), Comment.create(article: Article.create(title: 'Avenge'), person: Person.create(salary: 50_000)), ] - expect(Comment.ransack(article_title_cont: 'aven',s: 'person_salary desc').result).to eq(comments) + expect(Comment.ransack(article_title_cont: 'aven', s: 'person_salary desc').result).to eq(comments) expect(Comment.joins(:person).ransack(s: 'persons_salarydesc', article_title_cont: 'aven').result).to eq(comments) - expect(Comment.joins(:person).ransack(article_title_cont: 'aven',s: 'persons_salary desc').result).to eq(comments) + expect(Comment.joins(:person).ransack(article_title_cont: 'aven', s: 'persons_salary desc').result).to eq(comments) end it 'allows sort by `only_sort` field' do @@ -545,7 +545,6 @@ def self.simple_escaping? ) end - it 'should allow passing ransacker arguments to a ransacker' do s = Person.ransack( c: [{ diff --git a/spec/ransack/adapters/active_record/context_spec.rb b/spec/ransack/adapters/active_record/context_spec.rb index 6fb135b80..893b9be33 100644 --- a/spec/ransack/adapters/active_record/context_spec.rb +++ b/spec/ransack/adapters/active_record/context_spec.rb @@ -9,7 +9,6 @@ module ActiveRecord describe Context do subject { Context.new(Person) } - it 'has an Active Record alias tracker method' do expect(subject.alias_tracker) .to be_an ::ActiveRecord::Associations::AliasTracker @@ -81,7 +80,7 @@ module ActiveRecord end it 'build correlated subquery for multiple conditions (default scope)' do - search = Search.new(Person, { comments_body_not_eq: 'some_title'}) + search = Search.new(Person, { comments_body_not_eq: 'some_title' }) # Was # SELECT "people".* FROM "people" WHERE "people"."id" NOT IN ( diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 19f82aa33..d8edd081e 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -186,7 +186,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link desc/ } @@ -202,7 +202,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -216,7 +216,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link desc/ } @@ -232,7 +232,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -258,7 +258,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link desc/ } @@ -274,7 +274,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+asc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -289,7 +289,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link/ } @@ -306,7 +306,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -321,7 +321,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link/ } @@ -338,7 +338,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -353,7 +353,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/ ) } it { should match /sort_link/ } @@ -370,7 +370,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+asc/ ) } end @@ -385,7 +385,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } it { should match /sort_link/ } @@ -402,7 +402,7 @@ module Helpers ) } it { - should match( /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + should match(/people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ ) } end @@ -643,13 +643,13 @@ module Helpers before do Ransack.configure do |c| c.hide_sort_order_indicators = false - c.custom_arrows = { default_arrow: "defaultarrow"} + c.custom_arrows = { default_arrow: "defaultarrow" } end end after do Ransack.configure do |c| - c.custom_arrows = { default_arrow: nil} + c.custom_arrows = { default_arrow: nil } end end diff --git a/spec/ransack/nodes/grouping_spec.rb b/spec/ransack/nodes/grouping_spec.rb index ced03a09f..2f23baf7f 100644 --- a/spec/ransack/nodes/grouping_spec.rb +++ b/spec/ransack/nodes/grouping_spec.rb @@ -80,7 +80,7 @@ module Nodes 'a' => { '0' => { 'name' => 'with_arguments', - 'ransacker_args' => [1,2] + 'ransacker_args' => [1, 2] } }, 'p' => 'eq', @@ -90,7 +90,7 @@ module Nodes 'a' => { '0' => { 'name' => 'with_arguments', - 'ransacker_args' => [3,4] + 'ransacker_args' => [3, 4] } }, 'p' => 'eq', diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index ea79004e9..ce5839109 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -422,7 +422,7 @@ module Ransack context "defining custom predicates" do describe "with 'not_in' arel predicate" do before do - Ransack.configure {|c| c.add_predicate "not_in_csv", arel_predicate: "not_in", formatter: proc { |v| v.split(",") } } + Ransack.configure { |c| c.add_predicate "not_in_csv", arel_predicate: "not_in", formatter: proc { |v| v.split(",") } } end it 'generates a value IS NOT NULL query' do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index ee09a0282..38ae219d5 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -87,7 +87,6 @@ class Person < ActiveRecord::Base ) end - ransacker :sql_literal_id do Arel.sql('people.id') end @@ -110,7 +109,6 @@ class Person < ActiveRecord::Base Arel.sql(query) end - def self.ransackable_attributes(auth_object = nil) if auth_object == :admin super - ['only_sort']