Skip to content

Commit

Permalink
Translation and version
Browse files Browse the repository at this point in the history
  • Loading branch information
Breccan committed Oct 21, 2010
1 parent 987a57b commit 1cdaf03
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ coverage
rdoc
pkg
*.gemspec
.rvmrc

## PROJECT::SPECIFIC
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
# and we can go on declaring new extended fields dynamically
module ExtendedContentTranslation
unless included_modules.include? ExtendedContentTranslation

# override so that we may have extended fields be translatable attributes
def translatable_attributes
# klass is ::Version classes parent namespace wise
klass = original_class
def self.included(base)

type = klass == Topic ? topic_type : ContentType.find_by_name(klass.name)
#Send define method because scopes are not playing nicely together
base.send(:define_method, :translatable_attributes) do
# klass is ::Version classes parent namespace wise
klass = self.class

fields = type.mapped_fields.select { |f| ['text', 'textarea', 'choice', 'autocomplete'].include?(f.f_type) }
type_translatable_attributes = fields.collect { |f| f.label_for_params.to_sym }

klass::Translation.update_keys_if_necessary_with(type_translatable_attributes)
type = klass == Topic ? topic_type : ContentType.find_by_class_name(klass.name)

update_translation_for_methods_if_necessary_with(type_translatable_attributes)
fields = type.mapped_fields.select { |f| ['text', 'textarea', 'choice', 'autocomplete'].include?(f.ftype) }
type_translatable_attributes = fields.collect { |f| f.label_for_params.to_sym }

klass::Translation.translatable_attributes + type_translatable_attributes
klass::Translation.update_keys_if_necessary_with(type_translatable_attributes)

update_translation_for_methods_if_necessary_with(type_translatable_attributes)

self.class.translatable_attributes + type_translatable_attributes
end
end
end
end

Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
require 'replace_results_with_translations'
require 'redefine_find_with_result_replacement'
# For classes where their ::Version subclass
# is what has mongo_translatable declaration is on
module TranslationFromVersion
unless included_modules.include? TranslationFromVersion
def self.included(base)
base.extend(ReplaceResultsWithTranslations)
base.extend(RedefineFindWithResultReplacement)
base.extend(ClassMethods)
base.extend(InstanceMethods)
end

# an instance of the ::Version subclass is what has a translation
# mapped to it for instances of this class
# these methods, if appropriate, pulls the translation for the requested locale
# for the version of the original class instance
module ClassMethods

end
module InstanceMethods

#Get the current translation or the latest
def translation_for(locale)
translation = self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale, :version => self.version)
translation = self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale, :version => self.version.to_s)
translation ||= self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale)
return translation
end
Expand Down Expand Up @@ -55,4 +37,3 @@ def translate(options = {})
end
end
end
end
20 changes: 15 additions & 5 deletions rails/init.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'mongo_translatable'
require 'kete_translatable_content'
require 'kete_translatable_content/extensions/extended_content_translation'
require 'kete_translatable_content/extensions/translation_from_version'

config.to_prepare do

Expand All @@ -10,16 +12,20 @@

Kete.translatables.each do |name, options|
args = [:mongo_translate, *options['translatable_attributes']]
args << { :redefine_find => options['redefine_find'] }
args << { :redefine_find => options['redefine_find'],
:through_version => options["through_version"]}

class_name = name.camelize
if options[:through_version]

class_name.constantize.send(*args)

if options["through_version"]
class_name.constantize.send :include, TranslationFromVersion
class_name += "::Version"
trans_class = class_name + "::Translation"
trans_class.constantize.send :update_keys_if_necessary_with, [:version]
end

class_name.constantize.send(*args)
class_name.constantize.send(:include, ExtendedContentTranslation) if options[:through_version]

end

# precedence over a plugin or gem's (i.e. an engine's) app/views
Expand Down Expand Up @@ -62,4 +68,8 @@ def clear_locale_cache(options = { })
end
end
end
Kete.translatables.each do |name, options|
class_name = name.camelize
class_name.constantize.send :include, ExtendedContentTranslation
end
end

0 comments on commit 1cdaf03

Please sign in to comment.