Created
September 28, 2011 21:10
-
-
Save robyurkowski/1249263 to your computer and use it in GitHub Desktop.
Globalizing an Engine -- rough notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Globalizing an Engine: | |
# app/views/<model>.rb | |
#- add "require 'globalize3'" | |
#- In class definition: | |
#- Remove 'is_seo_meta if self.table_exists?' | |
#- Add following: | |
if self.respond_to?(:translates) | |
translates :name | |
# Set up support for meta tags through translations. | |
if defined?(::<Model>::Translation) | |
if ::Team::Translation.table_exists? | |
def translation | |
if @translation.nil? or @translation.try(:locale) != ::Globalize.locale | |
@translation = translations.with_locale(::Globalize.locale).first | |
@translation ||= translations.build(:locale => ::Globalize.locale) | |
end | |
@translation | |
end | |
# IF using seo_meta, include this section, and remove is_seo_meta from main section | |
# Instruct the Translation model to have meta tags. | |
::<Model>::Translation.send :is_seo_meta | |
fields = ::SeoMeta.attributes.keys.reject{|f| | |
self.column_names.map(&:to_sym).include?(f) | |
}.map{|a| [a, :"#{a}="]}.flatten | |
delegate *(fields << {:to => :translation}) | |
after_save proc {|m| m.translation.save} | |
# END SEO_META | |
before_create :ensure_locale, :if => proc { |c| | |
::Refinery.i18n_enabled? | |
} | |
end | |
end | |
end | |
#- Remove any validators that require :uniqueness => true | |
#- if mass-assignment an issue, send ::Model::Translation attr_accessible ala Pages | |
#- rails g migration add_translation_table_to_<model> | |
class Add<Model>TranslationTable < ActiveRecord::Migration | |
def self.up | |
# Optional: Force migrated data into a particular locale | |
# ::Globalize::locale = :fr | |
create_table :<underscored_model>_translations do |t| | |
<translated fields for model> | |
t.timestamps | |
end | |
<Model>.translation_class.send :is_seo_meta | |
# Migrate Data | |
all_posts = <Model>.all | |
model_attributes = all_posts.collect {|m| m.untranslated_attributes} | |
all_posts.each do |obj| | |
# Assign the attributes back to the model which will enable globalize3 to translate them. | |
obj.attributes = model_attributes.detect{|a| a['id'] == obj.id} | |
obj.save! | |
end | |
# Create index | |
add_index :<underscored_model>_translations, :<underscored_model>_id, :name => :index_<underscored_model>_translations_on_<underscored_model>_id | |
::SeoMetum.update_all :seo_meta_type => '<Model>::Translation', :seo_meta_type => '<Model>' | |
<Remove translated columns from Model> | |
remove_column <Model>, :title | |
end | |
def self.down | |
# Optional -- force locale | |
# ::Globalize::locale = :fr | |
# Re-add translated columns | |
add_column <Model>, :title, :string | |
<Model>.send :is_seo_meta | |
::SeoMetum.update_all :seo_meta_type => '<Model>', :seo_meta_type => '<Model>::Translation' | |
remove_index :<underscored_model>_translations, :name => :index_<underscored_model>_translations_on_<underscored_model>_id rescue nil | |
# Migrate data Back | |
translated_attribute_names = <Model>.translated_attribute_names - ::SeoMeta.attributes.keys | |
# Find all of the translated attributes for all records in the model. | |
all_translated_attributes = <Model>.all.collect{|m| m.attributes} | |
all_translated_attributes.each do |translated_record| | |
# Create a hash containing the translated column names and their values. | |
translated_attribute_names.inject(fields_to_update={}) do |f, name| | |
f.update({name.to_sym => translated_record[name.to_s]}) | |
end | |
# Now, update the actual model's record with the hash. | |
<Model>.update_all(fields_to_update, {:id => translated_record['id']}) | |
end | |
drop_table :<underscored_model>_translations | |
end | |
end | |
#3. rake db:migrate | |
#4. Override view=admin/pages/_locale_picker, and copy into app/views/shared/admin/ (mkdir -p) | |
#5. Override view=admin/model/_form. | |
<%= form_for [:admin, @page], url_opts do |f| %> | |
<%= render :partial => "/shared/admin/error_messages", | |
:locals => { | |
:object => @page, | |
:include_object_name => true | |
} %> | |
<%= render :partial => "/shared/admin/locale_picker", | |
:locals => { | |
:current_locale => Thread.current[:globalize_locale] | |
} if ::Refinery.i18n_enabled? %> | |
6. Override view=admin/model/_model | |
<span class='title'> | |
<%= <single_obj_var>.name %> | |
<% if ::Refinery.i18n_enabled? and ::Refinery::I18n.frontend_locales.many? and (locales = <single_object_var, i.e. collaborator>.translations.map(&:locale)).present? %> | |
<span class='preview'> | |
<% ([<single_object_var>.translation.try(:locale)] | locales).each do |locale| %> | |
<%= link_to refinery_icon_tag("flags/#{locale}.png", :size => '16x11'), edit_admin_<single_object_var>_path(<single_object_var>, :switch_locale => locale), :class => 'locale' %> | |
<% end %> | |
</span> | |
<% end %> | |
</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment