Skip to content

Commit

Permalink
Add a custom deprecator to SimpleForm
Browse files Browse the repository at this point in the history
Rails 7.1 is deprecating the `ActiveSupport::Deprecation.instance` usage
in favor of defining library-specific deprecators, this implements one
for SimpleForm going forward.
  • Loading branch information
carlosantoniodasilva committed Oct 10, 2023
1 parent 9a76a4e commit 80915d5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
12 changes: 8 additions & 4 deletions lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def self.configured? #:nodoc:
@@configured
end

def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new("5.3", "SimpleForm")
end

## CONFIGURATION OPTIONS

# Method used to tidy up errors.
Expand Down Expand Up @@ -264,21 +268,21 @@ def self.additional_classes_for(component)
## SETUP

def self.default_input_size=(*)
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
SimpleForm.deprecator.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
end

def self.form_class=(value)
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead", caller
SimpleForm.deprecator.warn "[SIMPLE_FORM] SimpleForm.form_class= is deprecated and will be removed in 4.x. Use SimpleForm.default_form_class= instead", caller
@@form_class = value
end

def self.file_methods=(file_methods)
ActiveSupport::Deprecation.warn(FILE_METHODS_DEPRECATION_WARN, caller)
SimpleForm.deprecator.warn(FILE_METHODS_DEPRECATION_WARN, caller)
@@file_methods = file_methods
end

def self.file_methods
ActiveSupport::Deprecation.warn(FILE_METHODS_DEPRECATION_WARN, caller)
SimpleForm.deprecator.warn(FILE_METHODS_DEPRECATION_WARN, caller)
@@file_methods
end

Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/components/label_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def deprecated_component(namespace, wrapper_options)
method = method(namespace)

if method.arity.zero?
ActiveSupport::Deprecation.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: namespace })
SimpleForm.deprecator.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: namespace })

method.call
else
Expand Down
4 changes: 4 additions & 0 deletions lib/simple_form/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ class Railtie < Rails::Railtie
' Use `rails generate simple_form:install` to generate the Simple Form configuration.'
end
end

initializer "simple_form.deprecator" do |app|
app.deprecators[:simple_form] = SimpleForm.deprecator if app.respond_to?(:deprecators)
end
end
end
2 changes: 1 addition & 1 deletion lib/simple_form/wrappers/leaf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def render(input)
method = input.method(@namespace)

if method.arity.zero?
ActiveSupport::Deprecation.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: @namespace })
SimpleForm.deprecator.warn(SimpleForm::CUSTOM_INPUT_DEPRECATION_WARN % { name: @namespace })

method.call
else
Expand Down
2 changes: 1 addition & 1 deletion test/action_view_extensions/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FormHelperTest < ActionView::TestCase

# Remove this test when SimpleForm.form_class is removed in 4.x
test 'SimpleForm allows overriding default form class, but not form class' do
ActiveSupport::Deprecation.silence do
SimpleForm.deprecator.silence do
swap SimpleForm, form_class: "fixed_class", default_form_class: "my_custom_class" do
with_concat_form_for :user, html: { class: "override_class" }
assert_no_select 'form.my_custom_class'
Expand Down
4 changes: 2 additions & 2 deletions test/form_builder/general_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def with_custom_form_for(object, *args, &block)
# COMMON OPTIONS
# Remove this test when SimpleForm.form_class is removed in 4.x
test 'builder adds chosen form class' do
ActiveSupport::Deprecation.silence do
SimpleForm.deprecator.silence do
swap SimpleForm, form_class: :my_custom_class do
with_form_for @user, :name
assert_select 'form.my_custom_class'
Expand All @@ -306,7 +306,7 @@ def with_custom_form_for(object, *args, &block)

# Remove this test when SimpleForm.form_class is removed in 4.x
test 'builder adds chosen form class and default form class' do
ActiveSupport::Deprecation.silence do
SimpleForm.deprecator.silence do
swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
with_form_for @user, :name
assert_select 'form.my_custom_class.my_default_class'
Expand Down
4 changes: 2 additions & 2 deletions test/inputs/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def discovery(value = false)
assert_select 'form input[type=file]#user_avatar.file-upload'
end
end

test 'inputs method without wrapper_options are deprecated' do
discovery do
assert_deprecated do
assert_deprecated('input method now accepts a `wrapper_options` argument.', SimpleForm.deprecator) do
with_form_for @user, :name, as: :deprecated
end

Expand Down

0 comments on commit 80915d5

Please sign in to comment.