Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for breaking changes in Rails 8 #1631

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def description
def matches?(subject)
self.subject = ModelReflector.new(subject, name)

if option_verifier.correct_for_string?(
:counter_cache,
counter_cache,
)
if correct_value?
true
else
self.missing_option = "#{name} should have #{description}"
Expand All @@ -34,9 +31,42 @@ def matches?(subject)

attr_accessor :subject, :counter_cache, :name

def correct_value?
expected = normalize_value

if expected.is_a?(Hash)
option_verifier.correct_for_hash?(
:counter_cache,
expected,
)
else
option_verifier.correct_for_string?(
:counter_cache,
expected,
)
end
end

def option_verifier
@_option_verifier ||= OptionVerifier.new(subject)
end

def normalize_value
if Rails::VERSION::MAJOR >= 8
case counter_cache
when true
{ active: true, column: nil }
when String, Symbol
{ active: true, column: counter_cache.to_s }
when Hash
({ active: true }).merge(counter_cache)
else
raise ArgumentError, 'Invalid counter_cache option'
end
else
counter_cache
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,17 @@ def expected_default_value
end

def actual_default_value
attribute_schema = model.attributes_to_define_after_schema_loads[attribute_name.to_s]
attribute_schema = model.respond_to?(:_default_attributes) ? model._default_attributes[attribute_name.to_s] : model.attributes_to_define_after_schema_loads[attribute_name.to_s]

if Kernel.const_defined?('ActiveModel::Attribute::UserProvidedDefault') && attribute_schema.is_a?(::ActiveModel::Attribute::UserProvidedDefault)
attribute_schema = attribute_schema.marshal_dump
end

value = case attribute_schema
in [_, { default: default_value } ]
default_value
in [_, default_value, *]
default_value
in [_, default_value]
default_value
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/unit/rails_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def rails_new
end

def rails_new_command
"bundle exec rails new #{fs.project_directory} --database=#{database.adapter_name} --skip-bundle --skip-javascript --no-rc --skip-bootsnap"
"bundle exec rails new #{fs.project_directory} --database=#{database.adapter_name} --asset-pipeline=sprockets --skip-bundle --skip-javascript --no-rc --skip-bootsnap"
end

def fix_available_locales_warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ def record_having_one_attached(
end

if remove_attachments
reflections.delete("#{attached_name}_attachment")
if respond_to?(:normalized_reflections)
clear_reflections_cache
_reflections.delete("#{attached_name}_attachment".to_sym)
normalized_reflections
else
reflections.delete("#{attached_name}_attachment")
end
end

if invalidate_blobs
Expand Down Expand Up @@ -223,7 +229,13 @@ def record_having_many_attached(
end

if remove_attachments
reflections.delete("#{attached_name}_attachments")
if respond_to?(:normalized_reflections)
clear_reflections_cache
_reflections.delete("#{attached_name}_attachments".to_sym)
normalized_reflections
else
reflections.delete("#{attached_name}_attachments")
end
end

if invalidate_blobs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def unserialized_model
end

def with_serialized_attr(type: nil, coder: YAML)
type_or_coder = rails_version >= 7.1 ? nil : type || coder
args = rails_version >= 7.1 ? [] : [type || coder]

define_model(:example, attr: :string) do
if type
serialize :attr, type_or_coder, type: type, coder: coder
serialize :attr, *args, type: type, coder: coder
else
serialize :attr, type_or_coder, coder: coder
serialize :attr, *args, coder: coder
end
end.new
end
Expand Down
6 changes: 5 additions & 1 deletion spec/unit_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
end
end

ActiveSupport::Deprecation.behavior = :stderr
if Rails::VERSION::MAJOR >= 8
Rails.application.deprecators.behavior = :stderr
else
ActiveSupport::Deprecation.behavior = :stderr
end

Shoulda::Matchers.configure do |config|
config.integrate do |with|
Expand Down
Loading