From d3a844f0b884aa576f82a2bb3c5a446b6d743668 Mon Sep 17 00:00:00 2001 From: Nikita Vasilevsky Date: Fri, 11 Feb 2022 00:08:40 +0000 Subject: [PATCH] Handle attribute_method_matchers rename to attribute_method_patterns --- Gemfile | 2 +- Gemfile.lock | 141 ++++++++++-------- .../dsl/compilers/active_model_attributes.rb | 27 ++-- .../dsl/compilers/active_record_columns.rb | 10 +- sorbet/rbi/shims/activemodel.rbi | 2 + 5 files changed, 105 insertions(+), 77 deletions(-) diff --git a/Gemfile b/Gemfile index 88232238cf..5fe92f74f8 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,7 @@ group(:development, :test) do gem("smart_properties", require: false) gem("frozen_record", require: false) gem("sprockets", require: false) - gem("rails", require: false) + gem("rails", "~> 7.0", require: false) gem("state_machines", require: false) gem("activerecord-typedstore", require: false) gem("sqlite3") diff --git a/Gemfile.lock b/Gemfile.lock index 2f335cad69..67d6aaf333 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,75 +23,81 @@ GEM specs: aasm (5.2.0) concurrent-ruby (~> 1.0) - actioncable (6.1.4.4) - actionpack (= 6.1.4.4) - activesupport (= 6.1.4.4) + actioncable (7.0.2.2) + actionpack (= 7.0.2.2) + activesupport (= 7.0.2.2) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailbox (6.1.4.4) - actionpack (= 6.1.4.4) - activejob (= 6.1.4.4) - activerecord (= 6.1.4.4) - activestorage (= 6.1.4.4) - activesupport (= 6.1.4.4) + actionmailbox (7.0.2.2) + actionpack (= 7.0.2.2) + activejob (= 7.0.2.2) + activerecord (= 7.0.2.2) + activestorage (= 7.0.2.2) + activesupport (= 7.0.2.2) mail (>= 2.7.1) - actionmailer (6.1.4.4) - actionpack (= 6.1.4.4) - actionview (= 6.1.4.4) - activejob (= 6.1.4.4) - activesupport (= 6.1.4.4) + net-imap + net-pop + net-smtp + actionmailer (7.0.2.2) + actionpack (= 7.0.2.2) + actionview (= 7.0.2.2) + activejob (= 7.0.2.2) + activesupport (= 7.0.2.2) mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp rails-dom-testing (~> 2.0) - actionpack (6.1.4.4) - actionview (= 6.1.4.4) - activesupport (= 6.1.4.4) - rack (~> 2.0, >= 2.0.9) + actionpack (7.0.2.2) + actionview (= 7.0.2.2) + activesupport (= 7.0.2.2) + rack (~> 2.0, >= 2.2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (6.1.4.4) - actionpack (= 6.1.4.4) - activerecord (= 6.1.4.4) - activestorage (= 6.1.4.4) - activesupport (= 6.1.4.4) + actiontext (7.0.2.2) + actionpack (= 7.0.2.2) + activerecord (= 7.0.2.2) + activestorage (= 7.0.2.2) + activesupport (= 7.0.2.2) + globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (6.1.4.4) - activesupport (= 6.1.4.4) + actionview (7.0.2.2) + activesupport (= 7.0.2.2) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (6.1.4.4) - activesupport (= 6.1.4.4) + activejob (7.0.2.2) + activesupport (= 7.0.2.2) globalid (>= 0.3.6) - activemodel (6.1.4.4) - activesupport (= 6.1.4.4) + activemodel (7.0.2.2) + activesupport (= 7.0.2.2) activemodel-serializers-xml (1.0.2) activemodel (> 5.x) activesupport (> 5.x) builder (~> 3.1) - activerecord (6.1.4.4) - activemodel (= 6.1.4.4) - activesupport (= 6.1.4.4) + activerecord (7.0.2.2) + activemodel (= 7.0.2.2) + activesupport (= 7.0.2.2) activerecord-typedstore (1.4.0) activerecord (>= 5.2) activeresource (6.0.0) activemodel (>= 6.0) activemodel-serializers-xml (~> 1.0) activesupport (>= 6.0) - activestorage (6.1.4.4) - actionpack (= 6.1.4.4) - activejob (= 6.1.4.4) - activerecord (= 6.1.4.4) - activesupport (= 6.1.4.4) - marcel (~> 1.0.0) + activestorage (7.0.2.2) + actionpack (= 7.0.2.2) + activejob (= 7.0.2.2) + activerecord (= 7.0.2.2) + activesupport (= 7.0.2.2) + marcel (~> 1.0) mini_mime (>= 1.1.0) - activesupport (6.1.4.4) + activesupport (7.0.2.2) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - zeitwerk (~> 2.3) ansi (1.5.0) ar_transaction_changes (1.1.8) activerecord (>= 5.2.0) @@ -156,7 +162,7 @@ GEM ar_transaction_changes (~> 1.1) io-wait (0.2.1) language_server-protocol (3.16.0.3) - loofah (2.13.0) + loofah (2.14.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -173,6 +179,14 @@ GEM builder minitest (>= 5.0) ruby-progressbar + net-imap (0.2.3) + digest + net-protocol + strscan + net-pop (0.1.1) + digest + net-protocol + timeout net-protocol (0.1.2) io-wait timeout @@ -197,32 +211,32 @@ GEM rack (2.2.3) rack-test (1.1.0) rack (>= 1.0, < 3) - rails (6.1.4.4) - actioncable (= 6.1.4.4) - actionmailbox (= 6.1.4.4) - actionmailer (= 6.1.4.4) - actionpack (= 6.1.4.4) - actiontext (= 6.1.4.4) - actionview (= 6.1.4.4) - activejob (= 6.1.4.4) - activemodel (= 6.1.4.4) - activerecord (= 6.1.4.4) - activestorage (= 6.1.4.4) - activesupport (= 6.1.4.4) + rails (7.0.2.2) + actioncable (= 7.0.2.2) + actionmailbox (= 7.0.2.2) + actionmailer (= 7.0.2.2) + actionpack (= 7.0.2.2) + actiontext (= 7.0.2.2) + actionview (= 7.0.2.2) + activejob (= 7.0.2.2) + activemodel (= 7.0.2.2) + activerecord (= 7.0.2.2) + activestorage (= 7.0.2.2) + activesupport (= 7.0.2.2) bundler (>= 1.15.0) - railties (= 6.1.4.4) - sprockets-rails (>= 2.0.0) + railties (= 7.0.2.2) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.4.2) loofah (~> 2.3) - railties (6.1.4.4) - actionpack (= 6.1.4.4) - activesupport (= 6.1.4.4) + railties (7.0.2.2) + actionpack (= 7.0.2.2) + activesupport (= 7.0.2.2) method_source - rake (>= 0.13) + rake (>= 12.2) thor (~> 1.0) + zeitwerk (~> 2.5) rainbow (3.1.1) rake (13.0.6) rbi (0.0.12) @@ -279,12 +293,9 @@ GEM sprockets (4.0.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.4.2) - actionpack (>= 5.2) - activesupport (>= 5.2) - sprockets (>= 3.0.0) sqlite3 (1.4.2) state_machines (0.5.0) + strscan (3.0.1) thor (1.2.1) timeout (0.2.0) tzinfo (2.0.4) @@ -325,7 +336,7 @@ DEPENDENCIES net-smtp (= 0.3.1) nokogiri pry-byebug - rails + rails (~> 7.0) rake rubocop-lsp rubocop-rspec @@ -342,4 +353,4 @@ DEPENDENCIES xpath BUNDLED WITH - 2.3.3 + 2.3.4 diff --git a/lib/tapioca/dsl/compilers/active_model_attributes.rb b/lib/tapioca/dsl/compilers/active_model_attributes.rb index 58a1b2e40a..9a29ecd951 100644 --- a/lib/tapioca/dsl/compilers/active_model_attributes.rb +++ b/lib/tapioca/dsl/compilers/active_model_attributes.rb @@ -62,26 +62,35 @@ def gather_constants sig { params(constant: ::ActiveModel::Attributes::ClassMethods).returns(T::Array[[::String, ::String]]) } def attribute_methods_for(constant) - constant.attribute_method_matchers.flat_map do |matcher| + patterns = if constant.respond_to?(:attribute_method_patterns) + # https://github.com/rails/rails/pull/44367 + T.unsafe(constant).attribute_method_patterns + else + constant.attribute_method_matchers + end + patterns.flat_map do |pattern| constant.attribute_types.map do |name, value| - next unless handle_method_matcher?(matcher) + next unless handle_method_pattern?(pattern) - [matcher.method_name(name), type_for(value)] + [pattern.method_name(name), type_for(value)] end.compact end end sig do - params(matcher: ::ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher) + params(pattern: T.untyped) .returns(T::Boolean) end - def handle_method_matcher?(matcher) - target = if matcher.respond_to?(:method_missing_target) + def handle_method_pattern?(pattern) + target = if pattern.respond_to?(:method_missing_target) # Pre-Rails 6.0, the field is named "method_missing_target" - T.unsafe(matcher).method_missing_target - else + T.unsafe(pattern).method_missing_target + elsif pattern.respond_to?(:target) # Rails 6.0+ has renamed the field to "target" - matcher.target + pattern.target + else + # https://github.com/rails/rails/pull/44367/files + T.unsafe(pattern).proxy_target end HANDLED_METHOD_TARGETS.include?(target.to_s) diff --git a/lib/tapioca/dsl/compilers/active_record_columns.rb b/lib/tapioca/dsl/compilers/active_record_columns.rb index de11e6fb59..f807edeb5c 100644 --- a/lib/tapioca/dsl/compilers/active_record_columns.rb +++ b/lib/tapioca/dsl/compilers/active_record_columns.rb @@ -115,8 +115,14 @@ def decorate(root, constant) constant.attribute_aliases.each do |attribute_name, column_name| attribute_name = attribute_name.to_s column_name = column_name.to_s - new_method_names = constant.attribute_method_matchers.map { |m| m.method_name(attribute_name) } - old_method_names = constant.attribute_method_matchers.map { |m| m.method_name(column_name) } + patterns = if constant.respond_to?(:attribute_method_patterns) + # https://github.com/rails/rails/pull/44367 + T.unsafe(constant).attribute_method_patterns + else + constant.attribute_method_matchers + end + new_method_names = patterns.map { |m| m.method_name(attribute_name) } + old_method_names = patterns.map { |m| m.method_name(column_name) } methods_to_add = new_method_names - old_method_names add_methods_for_attribute(mod, constant, column_name, attribute_name, methods_to_add) diff --git a/sorbet/rbi/shims/activemodel.rbi b/sorbet/rbi/shims/activemodel.rbi index 6040aa17de..7a2e761a61 100644 --- a/sorbet/rbi/shims/activemodel.rbi +++ b/sorbet/rbi/shims/activemodel.rbi @@ -1,6 +1,8 @@ # typed: true module ActiveModel::Attributes::ClassMethods + requires_ancestor { Kernel } + sig { returns(T::Hash[String, ActiveModel::Type::Value]) } def attribute_types; end