Module: SupportTableCache::RelationOverride Private
- Defined in:
- lib/support_table_cache/relation_override.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Instance Method Summary collapse
-
#fetch_by(attributes) ⇒ ActiveRecord::Base?
private
Same as find_by, but performs a safety check to confirm the query will hit the cache.
-
#fetch_by!(attributes) ⇒ ActiveRecord::Base
private
Same as find_by!, but performs a safety check to confirm the query will hit the cache.
-
#find_by(*args) ⇒ ActiveRecord::Base?
private
Override for the find_by method that looks in the cache first.
-
#find_by!(*args) ⇒ ActiveRecord::Base
private
Override for the find_by! method that looks in the cache first.
Instance Method Details
#fetch_by(attributes) ⇒ ActiveRecord::Base?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Same as find_by, but performs a safety check to confirm the query will hit the cache.
69 70 71 72 73 74 75 |
# File 'lib/support_table_cache/relation_override.rb', line 69 def fetch_by(attributes) find_by_attribute_names = support_table_find_by_attribute_names(attributes) unless klass.support_table_cache_by_attributes.any? { |attribute_names, _ci| attribute_names == find_by_attribute_names } raise ArgumentError.new("#{name} does not cache queries by #{find_by_attribute_names.to_sentence}") end find_by(attributes) end |
#fetch_by!(attributes) ⇒ ActiveRecord::Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Same as find_by!, but performs a safety check to confirm the query will hit the cache.
83 84 85 86 87 88 89 |
# File 'lib/support_table_cache/relation_override.rb', line 83 def fetch_by!(attributes) value = fetch_by(attributes) if value.nil? raise ActiveRecord::RecordNotFound.new("Couldn't find #{klass.name}", klass.name) end value end |
#find_by(*args) ⇒ ActiveRecord::Base?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override for the find_by method that looks in the cache first.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/support_table_cache/relation_override.rb', line 11 def find_by(*args) return super unless klass.include?(SupportTableCache) cache = klass.send(:current_support_table_cache) return super unless cache # Skip caching for has_many or has_many :through associations return super if is_a?(ActiveRecord::Associations::CollectionProxy) return super if select_values.present? cache_key = nil attributes = ((args.size == 1 && args.first.is_a?(Hash)) ? args.first.stringify_keys : {}) # Apply any attributes from the current relation chain if scope_attributes.present? attributes = scope_attributes.stringify_keys.merge(attributes) end if attributes.present? support_table_cache_by_attributes.each do |attribute_names, case_sensitive, where| where&.each do |name, value| if attributes.include?(name) && attributes[name] == value attributes.delete(name) else return super end end cache_key = SupportTableCache.cache_key(klass, attributes, attribute_names, case_sensitive) break if cache_key end end if cache_key cache.fetch(cache_key, expires_in: support_table_cache_ttl) { super } else super end end |
#find_by!(*args) ⇒ ActiveRecord::Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override for the find_by! method that looks in the cache first.
56 57 58 59 60 61 62 |
# File 'lib/support_table_cache/relation_override.rb', line 56 def find_by!(*args) value = find_by(*args) unless value raise ActiveRecord::RecordNotFound.new("Couldn't find #{klass.name}", klass.name) end value end |