From a500f4ffc8277fc17d17428a2abce4173e2aace1 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 17 Dec 2021 13:06:15 +0800 Subject: [PATCH] Switch to use GitHub Actions --- .github/workflows/build.yml | 30 ++++++++++++++++++ .travis.yml | 17 ---------- gemfiles/Gemfile-edge | 7 ----- .../active_record/fetch_by_uniq_key.rb | 31 ++++++++++--------- .../active_record/preloader/association.rb | 5 ++- 5 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .travis.yml delete mode 100644 gemfiles/Gemfile-edge diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..7466c0c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: build +on: push +jobs: + build: + if: "!contains(github.event.head_commit.message, '[skip ci]')" + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - ruby: 3.0 + gemfile: Gemfile + - ruby: 2.7 + gemfile: gemfiles/Gemfile-6-1 + - ruby: 2.7 + gemfile: gemfiles/Gemfile-6-0-paranoia + - ruby: 2.6 + gemfile: gemfiles/Gemfile-6-0 + - ruby: 2.6 + gemfile: gemfiles/Gemfile-5-2 + env: + BUNDLE_GEMFILE: ${{ matrix.gemfile }} + USE_OFFICIAL_GEM_SOURCE: 1 + steps: + - uses: actions/checkout@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rake test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a784a25..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: ruby -rvm: - - 2.5.5 - - 2.6.3 - - 2.7.0 -gemfile: - - gemfiles/Gemfile-5-2 - - gemfiles/Gemfile-6-0 - - gemfiles/Gemfile-6-1 - - gemfiles/Gemfile-edge - - gemfiles/Gemfile-6-0-paranoia -matrix: - allow_failures: - - gemfile: gemfiles/Gemfile-edge -script: - - bundle exec rake test - - bundle exec rubocop diff --git a/gemfiles/Gemfile-edge b/gemfiles/Gemfile-edge deleted file mode 100644 index ed3da3d..0000000 --- a/gemfiles/Gemfile-edge +++ /dev/null @@ -1,7 +0,0 @@ -source 'https://rubygems.org' - -gemspec :path => '..' -# gem "paranoia", "~> 2.2" -gem "pry" -gem 'rails', github: 'rails/rails', branch: 'master' -gem 'arel', github: 'rails/arel', branch: 'master' diff --git a/lib/second_level_cache/active_record/fetch_by_uniq_key.rb b/lib/second_level_cache/active_record/fetch_by_uniq_key.rb index 5d6e1de..26bc560 100644 --- a/lib/second_level_cache/active_record/fetch_by_uniq_key.rb +++ b/lib/second_level_cache/active_record/fetch_by_uniq_key.rb @@ -9,10 +9,10 @@ def fetch_by_uniq_keys(where_values) if obj_id record = begin - find(obj_id) - rescue StandardError - nil - end + find(obj_id) + rescue + nil + end end return record if record_attributes_equal_where_values?(record, where_values) record = where(where_values).first @@ -42,20 +42,21 @@ def fetch_by_uniq_key!(value, uniq_key_name) end private - def cache_uniq_key(where_values) - keys = where_values.collect do |k, v| - v = Digest::MD5.hexdigest(v) if v.respond_to?(:size) && v.size >= 32 - [k, v].join("_") - end - ext_key = keys.join(",") - "#{SecondLevelCache.configure.cache_key_prefix}/uniq_key_#{name}_#{ext_key}" + def cache_uniq_key(where_values) + keys = where_values.collect do |k, v| + v = Digest::MD5.hexdigest(v) if v.respond_to?(:size) && v.size >= 32 + [k, v].join("_") end - def record_attributes_equal_where_values?(record, where_values) - # https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-type_for_attribute - where_values.all? { |k, v| record&.read_attribute(k) == type_for_attribute(k).cast(v) } - end + ext_key = keys.join(",") + "#{SecondLevelCache.configure.cache_key_prefix}/uniq_key_#{name}_#{ext_key}" + end + + def record_attributes_equal_where_values?(record, where_values) + # https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-type_for_attribute + where_values.all? { |k, v| record&.read_attribute(k) == type_for_attribute(k).cast(v) } + end end end end diff --git a/lib/second_level_cache/active_record/preloader/association.rb b/lib/second_level_cache/active_record/preloader/association.rb index 8e5c236..b71fe14 100644 --- a/lib/second_level_cache/active_record/preloader/association.rb +++ b/lib/second_level_cache/active_record/preloader/association.rb @@ -5,11 +5,14 @@ module ActiveRecord module Associations module Preloader module Association - # In Rails 7, override load_query for assign self + # Override load_query method for add Association instance in arguments to LoaderQuery + # https://github.com/rails/rails/blob/7-0-stable/activerecord/lib/active_record/associations/preloader/association.rb#L148 def loader_query ::ActiveRecord::Associations::Preloader::Association::LoaderQuery.new(self, scope, association_key_name) end + # Override load_records_for_keys for use SecondLevelCache before preload association + # https://github.com/rails/rails/blob/8f5b35b6107c28125b571b9842e248b13f804e5c/activerecord/lib/active_record/associations/preloader/association.rb#L7 module LoaderQuery attr_reader :association