From 4ca9fa11f9a9b5604371f260515c28a0f29cd921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20Mar=C3=ADa=20Mart=C3=ADnez=20G=C3=B3mez?= Date: Wed, 8 Aug 2018 10:35:03 +0200 Subject: [PATCH] Deprecate use of private methods in view's helpers Instead of dropping it completely in case someone is relying (probably inadvertenly) on it. --- actionview/CHANGELOG.md | 4 ++-- .../lib/action_view/helpers/form_options_helper.rb | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 9fc120acbc..4c552c635a 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,7 +1,7 @@ -* Stop exposing public methods in view's helpers. +* Deprecate exposing public methods in view's helpers. For example, in methods like `options_from_collection_for_select` - and `collection_select` it was possible to call private methods from + and `collection_select` it is possible to call private methods from the objects used. See [#33546](https://github.com/rails/rails/issues/33546) for details. diff --git a/actionview/lib/action_view/helpers/form_options_helper.rb b/actionview/lib/action_view/helpers/form_options_helper.rb index 0fd68b66d4..2ecba2e337 100644 --- a/actionview/lib/action_view/helpers/form_options_helper.rb +++ b/actionview/lib/action_view/helpers/form_options_helper.rb @@ -794,7 +794,7 @@ def extract_selected_and_disabled(selected) def extract_values_from_collection(collection, value_method, selected) if selected.is_a?(Proc) collection.map do |element| - element.public_send(value_method) if selected.call(element) + public_or_deprecated_send(element, value_method) if selected.call(element) end.compact else selected @@ -802,7 +802,17 @@ def extract_values_from_collection(collection, value_method, selected) end def value_for_collection(item, value) - value.respond_to?(:call) ? value.call(item) : item.public_send(value) + value.respond_to?(:call) ? value.call(item) : public_or_deprecated_send(item, value) + end + + def public_or_deprecated_send(item, value) + begin + item.public_send(value) + rescue NoMethodError + raise unless item.respond_to?(value, true) && !item.respond_to?(value) + ActiveSupport::Deprecation.warn "Using private methods in view's helpers is deprecated" + item.send(value) + end end def prompt_text(prompt) -- GitLab