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

Add ViewComponent::Deprecation #1277

Merged
merged 5 commits into from
Feb 15, 2022
Merged
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
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ Layout/LineLength:

Layout/SpaceBeforeBrackets:
Enabled: true

Style/FrozenStringLiteralComment:
Exclude:
- "**/*.jbuilder" # not yet supported inside jbuilder templates
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ title: Changelog

## main

* Use a dedicated deprecation instance, silence it while testing

*Max Beizer, Hans Lemuet, Elia Schito*

* Fix Ruby warnings.

*Hans Lemuet*
Expand Down
5 changes: 3 additions & 2 deletions lib/view_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module ViewComponent
autoload :Compiler
autoload :CompileCache
autoload :ComponentError
autoload :Deprecation
autoload :Instrumentation
autoload :Preview
autoload :PreviewTemplateError
Expand All @@ -21,8 +22,8 @@ module ViewComponent

# :nocov:
if defined?(ViewComponent::Engine)
ActiveSupport::Deprecation.warn(
"This manually engine loading is deprecated and will be removed in v3.0.0. " \
ViewComponent::Deprecation.warn(
"Manually loading the engine is deprecated and will be removed in v3.0.0. " \
"Remove `require \"view_component/engine\"`."
)
elsif defined?(Rails::Engine)
Expand Down
5 changes: 1 addition & 4 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,11 @@ def format
# @param variant [Symbol] The variant to be used by the component.
# @return [self]
def with_variant(variant)
ActiveSupport::Deprecation.warn(
"`with_variant` is deprecated and will be removed in ViewComponent v3.0.0."
)

@__vc_variant = variant

self
end
deprecate :with_variant, deprecator: ViewComponent::Deprecation

# The current request. Use sparingly as doing so introduces coupling that
# inhibits encapsulation & reuse, often making testing difficult.
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def compile(raise_errors: false)
end

if subclass_instance_methods.include?(:before_render_check)
ActiveSupport::Deprecation.warn(
ViewComponent::Deprecation.warn(
"`#before_render_check` will be removed in v3.0.0.\n\n" \
"To fix this issue, use `#before_render` instead."
)
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/content_areas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def with(area, content = nil, &block)

class_methods do
def with_content_areas(*areas)
ActiveSupport::Deprecation.warn(
ViewComponent::Deprecation.warn(
"`with_content_areas` is deprecated and will be removed in ViewComponent v3.0.0.\n\n" \
"Use slots (https://viewcomponent.org/guide/slots.html) instead."
)
Expand Down
8 changes: 8 additions & 0 deletions lib/view_component/deprecation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require "active_support/deprecation"

module ViewComponent
DEPRECATION_HORIZON = 3
Deprecation = ActiveSupport::Deprecation.new(DEPRECATION_HORIZON.to_s, "ViewComponent")
end
6 changes: 4 additions & 2 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Engine < Rails::Engine # :nodoc:
)

if options.preview_path.present?
ActiveSupport::Deprecation.warn(
ViewComponent::Deprecation.warn(
"`preview_path` will be removed in v3.0.0. Use `preview_paths` instead."
)
options.preview_paths << options.preview_path
Expand Down Expand Up @@ -155,7 +155,9 @@ class Engine < Rails::Engine # :nodoc:

# :nocov:
unless defined?(ViewComponent::Base)
ActiveSupport::Deprecation.warn(
require "view_component/deprecation"

ViewComponent::Deprecation.warn(
"This manually engine loading is deprecated and will be removed in v3.0.0. " \
"Remove `require \"view_component/engine\"`."
)
Expand Down
13 changes: 8 additions & 5 deletions lib/view_component/polymorphic_slots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ def renders_many(slot_name, callable = nil)
end

def register_polymorphic_slot(slot_name, types, collection:)
unless types.empty?
Spone marked this conversation as resolved.
Show resolved Hide resolved
getter_name = slot_name

define_method(getter_name) do
get_slot(slot_name)
end
end

renderable_hash = types.each_with_object({}) do |(poly_type, poly_callable), memo|
memo[poly_type] = define_slot(
"#{slot_name}_#{poly_type}", collection: collection, callable: poly_callable
)

getter_name = slot_name
setter_name =
if collection
"#{ActiveSupport::Inflector.singularize(slot_name)}_#{poly_type}"
else
"#{slot_name}_#{poly_type}"
end

define_method(getter_name) do
get_slot(slot_name)
end

define_method(setter_name) do |*args, &block|
set_polymorphic_slot(slot_name, poly_type, *args, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Slotable
# class_name: "Header" # class name string, used to instantiate Slot
# )
def with_slot(*slot_names, collection: false, class_name: nil)
ActiveSupport::Deprecation.warn(
ViewComponent::Deprecation.warn(
"`with_slot` is deprecated and will be removed in ViewComponent v3.0.0.\n" \
"Use the new slots API (https://viewcomponent.org/guide/slots.html) instead."
)
Expand Down
6 changes: 6 additions & 0 deletions script/release
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ update_ruby_version() {
-e "s/MINOR = [0-9]+/MINOR = $2/g" \
-e "s/PATCH = [0-9]+/PATCH = $3/g" \
lib/view_component/version.rb

# Update deprecation horizon version
major=$1
sed -E -i '' \
-e "s/DEPRECATION_HORIZON = [0-9]+/DEPRECATION_HORIZON = $((major + 1))/g" \
lib/view_component/deprecation.rb
}

update_gemfiles() {
Expand Down
2 changes: 0 additions & 2 deletions test/sandbox/app/components/jbuilder_component.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# frozen_string_literal: true

json.message @message
json.conent content
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require "view_component/deprecation"
ViewComponent::Deprecation.behavior = :silence

require File.expand_path("../sandbox/config/environment.rb", __FILE__)
require "rails/test_help"

Expand Down