Skip to content

Commit

Permalink
Handle attribute_method_matchers rename to attribute_method_patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
nvasilevski committed Feb 11, 2022
1 parent ca1e0eb commit d3a844f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
141 changes: 76 additions & 65 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -325,7 +336,7 @@ DEPENDENCIES
net-smtp (= 0.3.1)
nokogiri
pry-byebug
rails
rails (~> 7.0)
rake
rubocop-lsp
rubocop-rspec
Expand All @@ -342,4 +353,4 @@ DEPENDENCIES
xpath

BUNDLED WITH
2.3.3
2.3.4
27 changes: 18 additions & 9 deletions lib/tapioca/dsl/compilers/active_model_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions lib/tapioca/dsl/compilers/active_record_columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions sorbet/rbi/shims/activemodel.rbi
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit d3a844f

Please sign in to comment.