Class: RailsDbLocalize::TranslationCache
- Inherits:
-
Object
- Object
- RailsDbLocalize::TranslationCache
- Defined in:
- lib/rails_db_localize/translation_cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
For debug purpose only.
Class Method Summary collapse
Instance Method Summary collapse
- #get_translation_for(model_class, model_id, field, lang, default = nil) ⇒ Object
- #prefetch_collections(*collections) ⇒ Object
Instance Attribute Details
#cache ⇒ Object (readonly)
For debug purpose only
33 34 35 |
# File 'lib/rails_db_localize/translation_cache.rb', line 33 def cache @cache end |
Class Method Details
.instance ⇒ Object
7 8 9 |
# File 'lib/rails_db_localize/translation_cache.rb', line 7 def instance @instance ||= self.new end |
.reinit ⇒ Object
3 4 5 |
# File 'lib/rails_db_localize/translation_cache.rb', line 3 def reinit @instance = nil end |
Instance Method Details
#get_translation_for(model_class, model_id, field, lang, default = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rails_db_localize/translation_cache.rb', line 63 def get_translation_for model_class, model_id, field, lang, default=nil ck = RailsDbLocalize::Translation.generate_ck(model_class, model_id) if @cache[:cks][ck] #Prefetch was done. Check if there's a value @cache[:data][model_class.to_s].try(:[], model_id).try(:[], lang.to_s).try(:[], field.to_s) || default else #Do the query if no prefetch was done RailsDbLocalize::Translation.where( resource_type: model_class.to_s, resource_id: model_id, field: field, lang: lang ).select(:content).first.try(:content) || default # BTW We don't cache it now since a cache is done into ActiveRecord core... end end |
#prefetch_collections(*collections) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rails_db_localize/translation_cache.rb', line 35 def prefetch_collections *collections # empty string is used here to be sure there's at least one element # for IN clause in SQL (otherwise it will fail miserabily) in_clause = collections.inject([""]) do |arr, collection| klass = if collection.respond_to?(:klass) collection.klass #is a ActiveRecord::Relation else # Tricky part. sometimes using "all" the collection is already # fetched. And there's not really anything to do to get the scope again. except mapping of the primary # key but seems tricky, so I prefer raise an error. if collection.is_a?(Array) raise "You're trying to get translations from a collection which is already fetched (SQL request already sent). " + "This is happening when you are using map/each/all on the request. Please use the translation cache fetching before the request is sent." else collection # This is the model end end arr + collection.pluck(klass.primary_key).map do |x| RailsDbLocalize::Translation.generate_ck(klass, x) end end is_cached(*in_clause) encache RailsDbLocalize::Translation.where("compound_key IN (?)", in_clause).pluck(:resource_type, :resource_id, :field, :lang, :content) end |