Skip to content

Commit

Permalink
initial stub work on making translatable versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter McGinnis committed Oct 13, 2010
1 parent ca64290 commit 9c55e96
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 18 deletions.
24 changes: 24 additions & 0 deletions config/translatables.yml
Expand Up @@ -36,3 +36,27 @@ choice:
tag:
translatable_attributes: [:name]
views: ['show']
topic:
through_version: true
translatable_attributes: [:title, :short_summary, :description] # extended content is handled separately
views: ['show']
document:
through_version: true
translatable_attributes: [:title, :short_summary, :description] # extended content is handled separately
views: ['show']
audio_recording:
through_version: true
translatable_attributes: [:title, :description] # extended content is handled separately
views: ['show']
still_image:
through_version: true
translatable_attributes: [:title, :description] # extended content is handled separately
views: ['show']
video:
through_version: true
translatable_attributes: [:title, :description] # extended content is handled separately
views: ['show']
web_link:
through_version: true
translatable_attributes: [:title, :description] # extended content is handled separately
views: ['show']
Expand Up @@ -3,31 +3,78 @@
class AddKeteTranslatableContentFields < ActiveRecord::Migration
def self.up
Kete.translatables.keys.each do |name|
table_name = name.tableize.to_sym
add_column table_name, :locale, :string, :default => I18n.default_locale.to_s
add_column table_name, :original_locale, :string, :default => I18n.default_locale.to_s
if Kete.translatables[name]['additional_columns'] && Kete.translatables[name]['additional_columns'].any?
Kete.translatables[name]['additional_columns'].each do |column|
column_name = column.first.to_sym
options = column.last
args = [table_name, column_name, options['type'].to_sym]
args << { :default => options['default'] } unless options['default'].nil?
table_name = resolve_to_table(name)

add_locale_columns_to(table_name)

add_locale_columns_to(resolve_to_table(name, :without_versions => true)) if through_version?(name)

add_additional_columns_for(name, table_name)
end
end

def self.down
Kete.translatables.keys.each do |name|
table_name = resolve_to_table(name)

remove_locale_columns_to(table_name)

remove_locale_columns_to(resolve_to_table(name, :without_versions => true)) if through_version?(name)

remove_additional_columns_for(name, table_name)
end
end

private

def add_additional_columns_for(name, table_name)
if Kete.translatables[name]['additional_columns'] && Kete.translatables[name]['additional_columns'].any?
Kete.translatables[name]['additional_columns'].each do |column|
column_name = column.first.to_sym
options = column.last
args = [table_name, column_name, options['type'].to_sym]
args << { :default => options['default'] } unless options['default'].nil?
add_column *args

if through_version(name)
args[0] = resolve_to_table(name, :without_versions => true)
add_column *args
end
end
end
end

def self.down
Kete.translatables.keys.each do |name|
table_name = name.tableize.to_sym
remove_column(table_name, :original_locale) rescue nil
remove_column(table_name, :locale) rescue nil
if Kete.translatables[name]['additional_columns'] && Kete.translatables[name]['additional_columns'].any?
Kete.translatables[name]['additional_columns'].each do |column|
remove_column(table_name, column.first.to_sym) rescue nil
def remove_additional_columns_for(name, table_name)
if Kete.translatables[name]['additional_columns'] && Kete.translatables[name]['additional_columns'].any?
Kete.translatables[name]['additional_columns'].each do |column|
remove_column(table_name, column.first.to_sym) rescue nil

if through_version(name)
remove_column(resolve_to_table(name, :without_versions => true), column.first.to_sym) rescue nil
end
end
end
end

def add_locale_columns_to(table_name)
add_column table_name, :locale, :string, :default => I18n.default_locale.to_s
add_column table_name, :original_locale, :string, :default => I18n.default_locale.to_s
end

def remove_locale_columns_to(table_name)
remove_column(table_name, :locale) rescue nil
remove_column(table_name, :original_locale) rescue nil
end

def through_version?(name)
Kete.translatables[name]['through_version'].present? ? Kete.translatables[name]['through_version'] || false
end

def resolve_to_table(name, options = { })
include_versions = options[:without_versions].present? ? options[:without_versions] : false

table_name = through_version?(name) && include_versions ? name + '_versions' : name.tableize

table_name = table_name.to_sym
end
end
@@ -0,0 +1,28 @@
# for ZOOM_CLASSES, extended_content is translatable_attribute
# however, in mongodb we store indivdual values for the extended fields
# and then we swap in actual translatable extended field values via individual
# extended field setter methods (method missing methods)
# the advantage is that we only have to tell mongo_translatable that extended_content
# is translatable at the time of system startup
# 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

type = klass == Topic ? topic_type : ContentType.find_by_name(klass.name)

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)

update_translation_for_methods_if_necessary_with(type_translatable_attributes)

klass::Translation.translatable_attributes + type_translatable_attributes
end
end
end
@@ -0,0 +1,21 @@
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)
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
end
end
10 changes: 9 additions & 1 deletion rails/init.rb
Expand Up @@ -11,7 +11,15 @@
Kete.translatables.each do |name, options|
args = [:mongo_translate, *options['translatable_attributes']]
args << { :redefine_find => options['redefine_find'] }
name.camelize.constantize.send(*args)

class_name = name.camelize
if options[:through_version]
class_name.constantize.send :include, TranslationFromVersion
class_name += "::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

0 comments on commit 9c55e96

Please sign in to comment.