Skip to content

Commit

Permalink
Support Rails edge's custom deprecators (#2213)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Aug 21, 2023
1 parent be167e1 commit bc4b334
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/eighty-cheetahs-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/view-components': patch
---

Support Rails edge's custom deprecators

<!-- Changed components: _none_ -->
2 changes: 1 addition & 1 deletion app/components/primer/beta/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Button < Primer::Component
renders_one :tooltip, lambda { |**system_arguments|
raise ArgumentError, "Buttons with a tooltip must have a unique `id` set on the `Button`." if @id.blank? && !Rails.env.production?

ActiveSupport::Deprecation.warn("Buttons with visible text should not use a `label` tooltip. Consider using Primer::Beta::IconButton instead.") if system_arguments[:type] == :label
::Primer::ViewComponents.deprecation.warn("Buttons with visible text should not use a `label` tooltip. Consider using Primer::Beta::IconButton instead.") if system_arguments[:type] == :label
system_arguments[:for_id] = @id
system_arguments[:type] = :description

Expand Down
2 changes: 1 addition & 1 deletion app/components/primer/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def deprecated_component_warning(new_class: nil, version: nil)
message += " and will be removed in v#{version}." if version
message += " Use #{new_class.name} instead." if new_class

ActiveSupport::Deprecation.warn(message)
::Primer::ViewComponents.deprecation.warn(message)
end

def validate_aria_label
Expand Down
2 changes: 1 addition & 1 deletion app/lib/primer/fetch_or_fallback_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_va
if allowed_values.include?(given_value)
given_value
elsif deprecated_values&.include?(given_value)
ActiveSupport::Deprecation.warn("#{given_value} is deprecated and will be removed in a future version.") unless Rails.env.production? || silence_deprecations?
::Primer::ViewComponents.deprecation.warn("#{given_value} is deprecated and will be removed in a future version.") unless Rails.env.production? || silence_deprecations?

given_value
else
Expand Down
18 changes: 18 additions & 0 deletions lib/primer/view_components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@
module Primer
# :nodoc:
module ViewComponents
DEPRECATION_HORIZON = "1.0"

# primer/view_components root directory.
def self.root
Pathname(File.expand_path(File.join("..", ".."), __dir__))
end

# Skip coverage here because only one branch will execute depending on what
# Rails version you're running.

# :nocov:
def self.deprecation
@deprecation ||=
if Rails.application.respond_to?(:deprecators)
Rails.application.deprecators[:primer_view_components] ||= ActiveSupport::Deprecation.new(
DEPRECATION_HORIZON, "primer_view_components"
)
else
ActiveSupport::Deprecation.instance
end
end
# :nocov:
end
end
2 changes: 1 addition & 1 deletion test/lib/fetch_or_fallback_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_accepts_deprecated_values

def test_warns_of_deprecation_if_not_silenced
with_silence_deprecations(false) do
ActiveSupport::Deprecation.expects(:warn).with("3 is deprecated and will be removed in a future version.").once
::Primer::ViewComponents.deprecation.expects(:warn).with("3 is deprecated and will be removed in a future version.").once
assert_equal(fetch_or_fallback([1, 2], 3, deprecated_values: [3]), 3)
end
end
Expand Down

0 comments on commit bc4b334

Please sign in to comment.