aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-08-15 17:23:42 -0400
committerGitHub <noreply@github.com>2017-08-15 17:23:42 -0400
commitc171890a42a641c6d8a1ef32be13e683d5c3e495 (patch)
treeda5e9e18c2795195e9c61c95e42ddbf911d8901b /activesupport/lib
parent692fab26b142c724f0b824db5029c70a88676808 (diff)
parent7fade3dcbcfc1f5ba6c455c04f04040f077e0d70 (diff)
downloadrails-c171890a42a641c6d8a1ef32be13e683d5c3e495.tar.gz
rails-c171890a42a641c6d8a1ef32be13e683d5c3e495.tar.bz2
rails-c171890a42a641c6d8a1ef32be13e683d5c3e495.zip
Merge pull request #30045 from albertoalmagro/fix-raise-unpermitted-parameters-regression
Load Parameters configurations on :action_controller only once
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/lazy_load_hooks.rb34
1 files changed, 26 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb
index c23b319046..c124416595 100644
--- a/activesupport/lib/active_support/lazy_load_hooks.rb
+++ b/activesupport/lib/active_support/lazy_load_hooks.rb
@@ -27,11 +27,17 @@ module ActiveSupport
base.class_eval do
@load_hooks = Hash.new { |h, k| h[k] = [] }
@loaded = Hash.new { |h, k| h[k] = [] }
+ @run_once = Hash.new { |h, k| h[k] = [] }
end
end
# Declares a block that will be executed when a Rails component is fully
# loaded.
+ #
+ # Options:
+ #
+ # * <tt>:yield</tt> - Yields the object that run_load_hooks to +block+.
+ # * <tt>:run_once</tt> - Given +block+ will run only once.
def on_load(name, options = {}, &block)
@loaded[name].each do |base|
execute_hook(base, options, block)
@@ -40,20 +46,32 @@ module ActiveSupport
@load_hooks[name] << [block, options]
end
- def execute_hook(base, options, block)
- if options[:yield]
- block.call(base)
- else
- base.instance_eval(&block)
- end
- end
-
def run_load_hooks(name, base = Object)
@loaded[name] << base
@load_hooks[name].each do |hook, options|
execute_hook(base, options, hook)
end
end
+
+ private
+
+ def with_execution_control(name, block, once)
+ unless @run_once[name].include?(block)
+ @run_once[name] << block if once
+
+ yield
+ end
+ end
+
+ def execute_hook(base, options, block)
+ with_execution_control(name, block, options[:run_once]) do
+ if options[:yield]
+ block.call(base)
+ else
+ base.instance_eval(&block)
+ end
+ end
+ end
end
extend LazyLoadHooks