app/models/tr8n/translation_key_source.rb in tr8n-3.1.8 vs app/models/tr8n/translation_key_source.rb in tr8n-3.2.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2010-2011 Michael Berkovich, tr8n.net
+# Copyright (c) 2010-2012 Michael Berkovich, tr8n.net
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
@@ -20,25 +20,36 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
class Tr8n::TranslationKeySource < ActiveRecord::Base
- set_table_name :tr8n_translation_key_sources
+ set_table_name :tr8n_translation_key_sources
after_destroy :clear_cache
belongs_to :translation_source, :class_name => "Tr8n::TranslationSource"
belongs_to :translation_key, :class_name => "Tr8n::TranslationKey"
alias :source :translation_source
alias :key :translation_key
serialize :details
+ def self.cache_key(translation_key_id, translation_source_id)
+ "translation_key_source_#{translation_key_id}_#{translation_source_id}"
+ end
+
+ def cache_key
+ self.class.cache_key(translation_key_id, translation_source_id)
+ end
+
def self.find_or_create(translation_key, translation_source)
- Tr8n::Cache.fetch("translation_key_source_#{translation_key.id}_#{translation_source.id}") do
+ Tr8n::Cache.fetch(cache_key(translation_key.id, translation_source.id)) do
tks = where("translation_key_id = ? and translation_source_id = ?", translation_key.id, translation_source.id).first
- tks || create(:translation_key => translation_key, :translation_source => translation_source)
+ tks ||= begin
+ translation_source.touch
+ create(:translation_key => translation_key, :translation_source => translation_source)
+ end
end
end
def update_details!(options)
return unless options[:caller_key]
@@ -49,9 +60,9 @@
details[options[:caller_key]] = options[:caller]
save
end
def clear_cache
- Tr8n::Cache.delete("translation_key_source_#{translation_key_id}_#{translation_source_id}")
+ Tr8n::Cache.delete(cache_key)
end
end