Skip to content

Commit

Permalink
Deprecate p shorthand in favor of partial
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth committed Jul 3, 2022
1 parent a9b5af7 commit f281083
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/nice_partials/monkey_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ def with_nice_partials_t_prefix(lookup_context, block)
end
end

require "active_support/deprecation"
NicePartials::DEPRECATOR = ActiveSupport::Deprecation.new("1.0", "nice_partials")

module NicePartials::RenderingWithAutoContext
attr_reader :partial

def p(*args)
args.empty? ? partial : super
if args.empty?
NicePartials::DEPRECATOR.deprecation_warning :p, :partial # In-branch printing so we don't warn on legit `Kernel.p` calls.
partial
else
super # …we're really Kernel.p
end
end

def render(options = {}, locals = {}, &block)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/_clobberer.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<% p "it's clobbering time" %>
<%# TODO: Remove this file once `p` has been removed post deprecation, but don't change it yet. %>
<%= p.yield :message %>
8 changes: 8 additions & 0 deletions test/renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ class RendererTest < NicePartials::Test

assert_rendered "hello from nice partials"
end

test "deprecates top-level access through p method" do
assert_deprecated /p is deprecated and will be removed from nice_partials \d/, NicePartials::DEPRECATOR do
assert_output "\"it's clobbering time\"\n" do
render("clobberer") { |p| p.content_for :message, "hello from nice partials" }
end
end
end
end

0 comments on commit f281083

Please sign in to comment.