From 4dec10aba9b306fc2c8b4c899b8e9af8c12d9f51 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 May 2024 15:16:09 +0200 Subject: [PATCH 1/5] Update gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8ec036cb..4eb7b201 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,6 @@ Gemfile.lock .ruby-version .ruby-gemset /tmp -docs/.vitepress/cache +.vitepress/cache node_modules -docs/.vitepress/dist +.vitepress/dist From 2482f5138c9f04880e3296835e4cf60f84ca56e9 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 May 2024 12:36:32 +0200 Subject: [PATCH 2/5] This is the commit message #3: Do not run rails >= 7 on ruby < 3 --- .github/workflows/test.yml | 40 ++++++------ Appraisals | 63 +++++++------------ gemfiles/activerecord_5.1.0.gemfile | 20 ------ gemfiles/activerecord_5.2.2.gemfile | 2 +- gemfiles/activerecord_6.0.0.gemfile | 2 +- gemfiles/activerecord_6.1.0.gemfile | 2 +- gemfiles/activerecord_7.0.0.gemfile | 2 +- ...0.2.gemfile => activerecord_7.1.0.gemfile} | 10 +-- gemfiles/activerecord_main.gemfile | 4 +- 9 files changed, 53 insertions(+), 92 deletions(-) delete mode 100644 gemfiles/activerecord_5.1.0.gemfile rename gemfiles/{activerecord_5.0.2.gemfile => activerecord_7.1.0.gemfile} (51%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 08d0a5af..54e25da1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,40 +14,38 @@ jobs: strategy: fail-fast: false matrix: - ruby: ['2.6', '2.7', '3.0', 'jruby', 'truffleruby'] - gemfile: ['gemfiles/activerecord_5.0.2.gemfile', 'gemfiles/activerecord_5.1.0.gemfile', 'gemfiles/activerecord_5.2.2.gemfile', 'gemfiles/activerecord_6.0.0.gemfile', 'gemfiles/activerecord_6.1.0.gemfile', 'gemfiles/activerecord_main.gemfile'] + ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', 'jruby', 'truffleruby'] + gemfile: ['gemfiles/activerecord_5.2.2.gemfile', 'gemfiles/activerecord_6.0.0.gemfile', 'gemfiles/activerecord_6.1.0.gemfile', 'gemfiles/activerecord_7.0.0.gemfile', 'gemfiles/activerecord_7.1.0.gemfile', 'gemfiles/activerecord_main.gemfile'] include: + - gemfile: 'gemfiles/activerecord_7.1.0.gemfile' + ruby: '3.3' + - gemfile: 'gemfiles/activerecord_7.1.0.gemfile' + ruby: '3.0' - gemfile: 'gemfiles/activerecord_7.0.0.gemfile' - ruby: '3.1' + ruby: '3.3' - gemfile: 'gemfiles/activerecord_7.0.0.gemfile' ruby: '3.0' - gemfile: 'gemfiles/activerecord_6.1.0.gemfile' - ruby: '3.1' + ruby: '3.3' - gemfile: 'gemfiles/activerecord_6.1.0.gemfile' ruby: '3.0' exclude: - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' ruby: '3.0' # rails 5.2 can't run on ruby 3.0 - - gemfile: 'gemfiles/activerecord_5.1.0.gemfile' - ruby: '3.0' # rails 5.1 can't run on ruby 3.0 - - gemfile: 'gemfiles/activerecord_5.0.2.gemfile' - ruby: '3.0' # rails 5.0 can't run on ruby 3.0 - - gemfile: 'gemfiles/activerecord_5.0.2.gemfile' - ruby: '3.0' # rails 5.0 can't run on ruby 3.0 - - gemfile: 'gemfiles/activerecord_5.0.2.gemfile' - ruby: 'truffleruby' # TruffleRuby 21.0 targets Ruby 2.7, same as above - - gemfile: 'gemfiles/activerecord_5.1.0.gemfile' - ruby: 'truffleruby' # TruffleRuby 21.0 targets Ruby 2.7, same as above - - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' - ruby: 'truffleruby' # TruffleRuby 21.0 targets Ruby 2.7, same as above + - gemfile: 'gemfiles/activerecord_7.0.0.gemfile' + ruby: '2.7' + - gemfile: 'gemfiles/activerecord_7.1.0.gemfile' + ruby: '2.7' - gemfile: 'gemfiles/activerecord_main.gemfile' - ruby: '2.6' # rails 7+ requires ruby 3.0+ - - gemfile: 'gemfiles/activerecord_5.0.2.gemfile' - ruby: 'jruby' # this *should* work - there's a test failure; it's not incompatible like the other excludes. could be an issue in Rails 5.0.2? + ruby: '2.7' - gemfile: 'gemfiles/activerecord_6.1.0.gemfile' - ruby: 'jruby' # this *should* work. it seems like there's an issue with rails 6 on jruby. + ruby: 'jruby' + - gemfile: 'gemfiles/activerecord_main.gemfile' + ruby: '2.7' + - gemfile: 'gemfiles/activerecord_main.gemfile' + ruby: '3.0' - gemfile: 'gemfiles/activerecord_main.gemfile' - ruby: 'jruby' # this *should* work. it seems like there's an issue with rails 6 on jruby. + ruby: 'jruby' env: BUNDLE_GEMFILE: ${{ matrix.gemfile }} diff --git a/Appraisals b/Appraisals index d025b2d2..2bb71a21 100644 --- a/Appraisals +++ b/Appraisals @@ -1,37 +1,3 @@ -appraise 'activerecord_5.0.2' do - gem 'activerecord', '~> 5.0.2', require: 'active_record' - gem 'activesupport', '~> 5.0.2', require: 'active_support/all' - gem 'actionpack', '~> 5.0.2', require: 'action_pack' - - gemfile.platforms :jruby do - gem 'activerecord-jdbcsqlite3-adapter' - gem 'jdbc-sqlite3' - gem 'jdbc-postgres' - end - - gemfile.platforms :ruby, :mswin, :mingw do - gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.3.0' - end -end - -appraise 'activerecord_5.1.0' do - gem 'activerecord', '~> 5.1.0', require: 'active_record' - gem 'activesupport', '~> 5.1.0', require: 'active_support/all' - gem 'actionpack', '~> 5.1.0', require: 'action_pack' - - gemfile.platforms :jruby do - gem 'activerecord-jdbcsqlite3-adapter' - gem 'jdbc-sqlite3' - gem 'jdbc-postgres' - end - - gemfile.platforms :ruby, :mswin, :mingw do - gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' - end -end - appraise 'activerecord_5.2.2' do gem 'activerecord', '~> 5.2.2', require: 'active_record' gem 'activesupport', '~> 5.2.2', require: 'active_support/all' @@ -45,7 +11,7 @@ appraise 'activerecord_5.2.2' do gemfile.platforms :ruby, :mswin, :mingw do gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' + gem 'sqlite3', '~> 1.7.3' end end @@ -62,7 +28,7 @@ appraise 'activerecord_6.0.0' do platforms :ruby, :mswin, :mingw do gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' + gem 'sqlite3', '~> 1.7.3' end end @@ -79,7 +45,7 @@ appraise 'activerecord_6.1.0' do platforms :ruby, :mswin, :mingw do gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' + gem 'sqlite3', '~> 1.7.3' end end @@ -96,7 +62,24 @@ appraise 'activerecord_7.0.0' do platforms :ruby, :mswin, :mingw do gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' + gem 'sqlite3', '~> 1.7.3' + end +end + +appraise 'activerecord_7.1.0' do + gem 'actionpack', '~> 7.1.0', require: 'action_pack' + gem 'activerecord', '~> 7.1.0', require: 'active_record' + gem 'activesupport', '~> 7.1.0', require: 'active_support/all' + + platforms :jruby do + gem 'activerecord-jdbcsqlite3-adapter' + gem 'jdbc-sqlite3' + gem 'jdbc-postgres' + end + + platforms :ruby, :mswin, :mingw do + gem 'pg', '~> 1.5.6' + gem 'sqlite3', '~> 1.7.3' end end @@ -114,7 +97,7 @@ appraise 'activerecord_main' do end platforms :ruby, :mswin, :mingw do - gem 'pg', '~> 1.3.4' - gem 'sqlite3', '~> 1.4.2' + gem 'pg', '~> 1.5.6' + gem 'sqlite3', '~> 1.7.3' end end diff --git a/gemfiles/activerecord_5.1.0.gemfile b/gemfiles/activerecord_5.1.0.gemfile deleted file mode 100644 index fe5514f1..00000000 --- a/gemfiles/activerecord_5.1.0.gemfile +++ /dev/null @@ -1,20 +0,0 @@ -# This file was generated by Appraisal - -source "https://rubygems.org" - -gem "activerecord", "~> 5.1.0", require: "active_record" -gem "activesupport", "~> 5.1.0", require: "active_support/all" -gem "actionpack", "~> 5.1.0", require: "action_pack" - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" - gem "jdbc-sqlite3" - gem "jdbc-postgres" -end - -platforms :ruby, :mswin, :mingw do - gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" -end - -gemspec path: "../" diff --git a/gemfiles/activerecord_5.2.2.gemfile b/gemfiles/activerecord_5.2.2.gemfile index 63ce61b4..071167d7 100644 --- a/gemfiles/activerecord_5.2.2.gemfile +++ b/gemfiles/activerecord_5.2.2.gemfile @@ -14,7 +14,7 @@ end platforms :ruby, :mswin, :mingw do gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" diff --git a/gemfiles/activerecord_6.0.0.gemfile b/gemfiles/activerecord_6.0.0.gemfile index e9f11ddd..a2777cd1 100644 --- a/gemfiles/activerecord_6.0.0.gemfile +++ b/gemfiles/activerecord_6.0.0.gemfile @@ -14,7 +14,7 @@ end platforms :ruby, :mswin, :mingw do gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" diff --git a/gemfiles/activerecord_6.1.0.gemfile b/gemfiles/activerecord_6.1.0.gemfile index 88580ffa..fd081871 100644 --- a/gemfiles/activerecord_6.1.0.gemfile +++ b/gemfiles/activerecord_6.1.0.gemfile @@ -14,7 +14,7 @@ end platforms :ruby, :mswin, :mingw do gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" diff --git a/gemfiles/activerecord_7.0.0.gemfile b/gemfiles/activerecord_7.0.0.gemfile index 1f0c169d..c0840521 100644 --- a/gemfiles/activerecord_7.0.0.gemfile +++ b/gemfiles/activerecord_7.0.0.gemfile @@ -14,7 +14,7 @@ end platforms :ruby, :mswin, :mingw do gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" diff --git a/gemfiles/activerecord_5.0.2.gemfile b/gemfiles/activerecord_7.1.0.gemfile similarity index 51% rename from gemfiles/activerecord_5.0.2.gemfile rename to gemfiles/activerecord_7.1.0.gemfile index 8f0cb561..72952c7b 100644 --- a/gemfiles/activerecord_5.0.2.gemfile +++ b/gemfiles/activerecord_7.1.0.gemfile @@ -2,9 +2,9 @@ source "https://rubygems.org" -gem "activerecord", "~> 5.0.2", require: "active_record" -gem "activesupport", "~> 5.0.2", require: "active_support/all" -gem "actionpack", "~> 5.0.2", require: "action_pack" +gem "actionpack", "~> 7.1.0", require: "action_pack" +gem "activerecord", "~> 7.1.0", require: "active_record" +gem "activesupport", "~> 7.1.0", require: "active_support/all" platforms :jruby do gem "activerecord-jdbcsqlite3-adapter" @@ -13,8 +13,8 @@ platforms :jruby do end platforms :ruby, :mswin, :mingw do - gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.3.0" + gem "pg", "~> 1.5.6" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" diff --git a/gemfiles/activerecord_main.gemfile b/gemfiles/activerecord_main.gemfile index ba43821e..cbc6172c 100644 --- a/gemfiles/activerecord_main.gemfile +++ b/gemfiles/activerecord_main.gemfile @@ -15,8 +15,8 @@ platforms :jruby do end platforms :ruby, :mswin, :mingw do - gem "pg", "~> 1.3.4" - gem "sqlite3", "~> 1.4.2" + gem "pg", "~> 1.5.6" + gem "sqlite3", "~> 1.7.3" end gemspec path: "../" From f5542cea7232bae1cbd593687047e63858961289 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 May 2024 15:57:16 +0200 Subject: [PATCH 3/5] Remove jruby and truffleruby --- .github/workflows/test.yml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 54e25da1..ed42f44d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', 'jruby', 'truffleruby'] + ruby: ['2.7', '3.0', '3.1', '3.2', '3.3'] gemfile: ['gemfiles/activerecord_5.2.2.gemfile', 'gemfiles/activerecord_6.0.0.gemfile', 'gemfiles/activerecord_6.1.0.gemfile', 'gemfiles/activerecord_7.0.0.gemfile', 'gemfiles/activerecord_7.1.0.gemfile', 'gemfiles/activerecord_main.gemfile'] include: - gemfile: 'gemfiles/activerecord_7.1.0.gemfile' @@ -31,22 +31,19 @@ jobs: ruby: '3.0' exclude: - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' - ruby: '3.0' # rails 5.2 can't run on ruby 3.0 + ruby: '3.0' + - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' + ruby: '3.1' + - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' + ruby: '3.2' + - gemfile: 'gemfiles/activerecord_5.2.2.gemfile' + ruby: '3.3' - gemfile: 'gemfiles/activerecord_7.0.0.gemfile' ruby: '2.7' - gemfile: 'gemfiles/activerecord_7.1.0.gemfile' ruby: '2.7' - gemfile: 'gemfiles/activerecord_main.gemfile' ruby: '2.7' - - gemfile: 'gemfiles/activerecord_6.1.0.gemfile' - ruby: 'jruby' - - gemfile: 'gemfiles/activerecord_main.gemfile' - ruby: '2.7' - - gemfile: 'gemfiles/activerecord_main.gemfile' - ruby: '3.0' - - gemfile: 'gemfiles/activerecord_main.gemfile' - ruby: 'jruby' - env: BUNDLE_GEMFILE: ${{ matrix.gemfile }} From 54945f7e54d5d3cdba1048caf0611e9ae9df510c Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 May 2024 16:29:00 +0200 Subject: [PATCH 4/5] Fix rubocop --- .github/workflows/test.yml | 2 ++ .rubocop.yml | 1 + .rubocop_todo.yml | 3 +++ .../model_adapters/active_record_adapter.rb | 16 +++++++++++----- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed42f44d..596db697 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,6 +44,8 @@ jobs: ruby: '2.7' - gemfile: 'gemfiles/activerecord_main.gemfile' ruby: '2.7' + - gemfile: 'gemfiles/activerecord_main.gemfile' + ruby: '3.0' env: BUNDLE_GEMFILE: ${{ matrix.gemfile }} diff --git a/.rubocop.yml b/.rubocop.yml index 8585e75c..24763717 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -50,3 +50,4 @@ AllCops: - 'gemfiles/**/*' - 'vendor/**/*' - 'Appraisals' + - 'node_modules/**/*' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f6c643ac..1715d17e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -214,3 +214,6 @@ Style/StringChars: Style/StringConcatenation: Exclude: - 'lib/cancan/rule.rb' + +Lint/SafeNavigationChain: + Enabled: false diff --git a/lib/cancan/model_adapters/active_record_adapter.rb b/lib/cancan/model_adapters/active_record_adapter.rb index 6d17f863..33766c0a 100644 --- a/lib/cancan/model_adapters/active_record_adapter.rb +++ b/lib/cancan/model_adapters/active_record_adapter.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# rubocop:disable Metrics/AbcSize +# rubocop:disable Metrics/CyclomaticComplexity +# rubocop:disable Metrics/PerceivedComplexity module CanCan module ModelAdapters class ActiveRecordAdapter < AbstractAdapter @@ -52,7 +55,7 @@ def parent_child_conditions(parent, child, all_conditions) # Search again in case of polymorphic associations, this time matching on the :has_many side # via the :as option, as well as klass foreign_key ||= parent_class.reflect_on_all_associations(:has_many).find do |has_many_assoc| - !matching_parent_child_polymorphic_association(has_many_assoc, child_class).nil? + matching_parent_child_polymorphic_association(has_many_assoc, child_class) end&.foreign_key&.to_sym foreign_key.nil? ? nil : all_conditions[foreign_key] @@ -61,7 +64,7 @@ def parent_child_conditions(parent, child, all_conditions) def matching_parent_child_polymorphic_association(parent_assoc, child_class) return nil unless parent_assoc.klass == child_class return nil if parent_assoc&.options[:as].nil? - + child_class.reflect_on_all_associations(:belongs_to).find do |child_assoc| # Only match this way for polymorphic associations child_assoc.polymorphic? && child_assoc.name == parent_assoc.options[:as] @@ -72,12 +75,12 @@ def child_association_to_parent(parent, child) child_class = child.is_a?(Class) ? child : child.class parent_class = parent.is_a?(Class) ? parent : parent.class - association = child_class.reflect_on_all_associations(:belongs_to).find do |association| + association = child_class.reflect_on_all_associations(:belongs_to).find do |belongs_to_assoc| # Do not match on polymorphic associations or it will throw an error (klass cannot be determined) - !association.polymorphic? && association.klass == parent.class + !belongs_to_assoc.polymorphic? && belongs_to_assoc.klass == parent.class end - return association unless association.nil? + return association if association parent_class.reflect_on_all_associations(:has_many).each do |has_many_assoc| association ||= matching_parent_child_polymorphic_association(has_many_assoc, child_class) @@ -217,6 +220,9 @@ def sanitize_sql(conditions) end end end +# rubocop:enable Metrics/PerceivedComplexity +# rubocop:enable Metrics/CyclomaticComplexity +# rubocop:enable Metrics/AbcSize ActiveSupport.on_load(:active_record) do send :include, CanCan::ModelAdditions From 4112cbc80f8f552c90ff7fe31f1ad5d406ce946b Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Thu, 23 May 2024 16:41:29 +0200 Subject: [PATCH 5/5] Use rspec instead of rake --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 596db697..49687f7a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -81,10 +81,10 @@ jobs: bundler-cache: true - name: Run tests on sqlite - run: DB=sqlite bundle exec rake + run: DB=sqlite bundle exec rspec - name: Run tests on postgres - run: DB=postgres bundle exec rake + run: DB=postgres bundle exec rspec lint: name: Lint