From e50780ab33527780924fd56961a5283aea731e43 Mon Sep 17 00:00:00 2001 From: Daniel Sudmann Date: Thu, 7 Jun 2018 10:14:01 +0200 Subject: [PATCH 1/3] Replace the method aliasing with prepend to prevent the stack overflow on inherited ActiveRecord models when their parents are cached_enumerations --- lib/cached_enumeration/cached_enumeration.rb | 48 ++++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/cached_enumeration/cached_enumeration.rb b/lib/cached_enumeration/cached_enumeration.rb index c196723..92acca9 100644 --- a/lib/cached_enumeration/cached_enumeration.rb +++ b/lib/cached_enumeration/cached_enumeration.rb @@ -145,15 +145,29 @@ def alias_all_method(base_singleton) end end - def alias_first_method(base_singleton) - base_singleton.__send__(:define_method, :cached_first) do |limit = nil| + module FirstWithCacheEnumeration + def first(limit=nil) + cache_enumeration.cached? && current_scope.nil? ? cached_first(limit) : super(limit) + end + + private + def cached_first(limit=nil) limit.nil? ? cache_enumeration.all.first : cache_enumeration.all.take(limit) end - base_singleton.__send__(:define_method, :first_with_cache_enumeration) do |limit = nil| - cache_enumeration.cached? && current_scope.nil? ? cached_first(limit) : first_without_cache_enumeration(limit) + end + + def alias_first_method(base_singleton) + base_singleton.__send__(:prepend, FirstWithCacheEnumeration) + end + + module FindWithCacheEnumeration + def find(*args) + if cache_enumeration.cached? && current_scope.nil? && args.length == 1 && args.first.respond_to?(:to_i) + by_id(args.first).tap{|res| raise ActiveRecord::RecordNotFound if res.nil? } + else + super(*args) + end end - base_singleton.__send__(:alias_method, :first_without_cache_enumeration, :first) - base_singleton.__send__(:alias_method, :first, :first_with_cache_enumeration) end def create_by_methods(base_singleton) @@ -170,28 +184,22 @@ def create_by_methods(base_singleton) base_singleton.__send__(:alias_method, "find_by_#{att}", "by_#{att}") end if @options[:hashed].include?("id") - base_singleton.__send__(:define_method, :find_with_cache_enumeration) do |*args| - if cache_enumeration.cached? && current_scope.nil? && args.length == 1 && args.first.respond_to?(:to_i) - by_id(args.first).tap{|res| raise ActiveRecord::RecordNotFound if res.nil? } - else - find_without_cache_enumeration(*args) - end - end - base_singleton.__send__(:alias_method, :find_without_cache_enumeration, :find) - base_singleton.__send__(:alias_method, :find, :find_with_cache_enumeration) + base_singleton.__send__(:prepend, FindWithCacheEnumeration) end end - def patch_const_missing(base_singleton) - base_singleton.__send__(:define_method, :const_missing_with_cache_enumeration) do |const_name| + module ConstMissingWithCacheEnumeration + def const_missing(const_name) if cache_enumeration.cached? || cache_enumeration.caching? - const_missing_without_cache_enumeration(const_name) + super(const_name) elsif cache_enumeration.cache! self.const_get(const_name) end end - base_singleton.__send__(:alias_method, :const_missing_without_cache_enumeration, :const_missing) - base_singleton.__send__(:alias_method, :const_missing, :const_missing_with_cache_enumeration) + end + + def patch_const_missing(base_singleton) + base_singleton.__send__(:prepend, ConstMissingWithCacheEnumeration) end end From 71918faff307e5c76288c5f2e2dc3031970a20e6 Mon Sep 17 00:00:00 2001 From: Daniel Sudmann Date: Thu, 7 Jun 2018 10:14:53 +0200 Subject: [PATCH 2/3] bump version --- lib/cached_enumeration/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cached_enumeration/version.rb b/lib/cached_enumeration/version.rb index 0084e81..87703cf 100644 --- a/lib/cached_enumeration/version.rb +++ b/lib/cached_enumeration/version.rb @@ -1,3 +1,3 @@ module CachedEnumeration - VERSION = "3.2.1" + VERSION = "3.2.2" end From 2aafcc31995e23b94e495d99f1dc84c501c11039 Mon Sep 17 00:00:00 2001 From: Daniel Sudmann Date: Thu, 7 Jun 2018 10:25:02 +0200 Subject: [PATCH 3/3] set ruby version --- .ruby_version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ruby_version diff --git a/.ruby_version b/.ruby_version new file mode 100644 index 0000000..35cee72 --- /dev/null +++ b/.ruby_version @@ -0,0 +1 @@ +2.4.3