From 49dd3a1770e57272f9b182ca54e6fd1e844b153f Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Thu, 26 Jul 2012 05:07:32 +0800 Subject: [PATCH] Remove i18n_variable for controllers, helpers and models --- app/controllers/admin/assets_controller.rb | 2 +- app/helpers/admin/asset_helper.rb | 2 +- app/helpers/admin/item_helper.rb | 2 +- app/helpers/application_helper.rb | 6 +- app/models/ad_image.rb | 4 +- app/models/asset.rb | 27 +--- app/models/asset_category.rb | 3 +- app/models/item.rb | 2 +- app/models/meta/meta.rb | 5 +- app/models/page.rb | 12 +- app/models/page_part.rb | 23 +-- app/models/site.rb | 29 +--- app/models/user/attribute.rb | 2 +- app/models/user/attribute_field.rb | 2 +- app/models/user/role.rb | 2 +- lib/tasks/migrate.rake | 176 ++++++++++++++++++++- 16 files changed, 196 insertions(+), 103 deletions(-) diff --git a/app/controllers/admin/assets_controller.rb b/app/controllers/admin/assets_controller.rb index 58f4a746..4489ce81 100644 --- a/app/controllers/admin/assets_controller.rb +++ b/app/controllers/admin/assets_controller.rb @@ -30,7 +30,7 @@ class Admin::AssetsController < OrbitBackendController def create @asset = Asset.new(params[:asset]) - @asset.filename = @asset.title[I18n.locale] rescue nil + @asset.filename = @asset.title rescue nil if @asset.filename && @asset.save respond_to do |format| format.js { diff --git a/app/helpers/admin/asset_helper.rb b/app/helpers/admin/asset_helper.rb index 818a2340..7ed6fa15 100644 --- a/app/helpers/admin/asset_helper.rb +++ b/app/helpers/admin/asset_helper.rb @@ -3,7 +3,7 @@ module Admin::AssetHelper def show_all_fields(asset, field) a = [] @site_valid_locales.each do |locale| - a << asset.send(field)[locale] rescue nil + a << asset.send("#{field}_translations")[locale] rescue nil end a.join(' - ') end diff --git a/app/helpers/admin/item_helper.rb b/app/helpers/admin/item_helper.rb index 78e911b9..e30e44ca 100644 --- a/app/helpers/admin/item_helper.rb +++ b/app/helpers/admin/item_helper.rb @@ -13,7 +13,7 @@ module Admin::ItemHelper ret << "
  • " # ret << "" ret << "
    " - ret << (link_to node.i18n_variable[I18n.locale], dest, :class => 'js_history') + ret << (link_to node.title, dest, :class => 'js_history') ret << "
    " ret << (link_to t('admin.edit'), eval("edit_admin_#{node._type.downcase}_path(node)"), :class => 'js_history') if node._type.eql?('Page') ret << (link_to t('admin.new_page'), new_admin_page_path(:parent_id => node.id), :class => 'new_page js_history') if node._type.eql?('Page') diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0ed5a1bd..0c6229b3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -122,11 +122,11 @@ module ApplicationHelper def page_title(page) res = "" - page_title = page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale] + page_title = page.page_title ? page.page_title : page.title if page.root? && @site.title - res << @site.title[I18n.locale] + res << @site.title elsif @site.title && @site.title_always_on - res << @site.title[I18n.locale] + ' - ' + page_title + res << @site.title + ' - ' + page_title else res << page_title end diff --git a/app/models/ad_image.rb b/app/models/ad_image.rb index 9d7fe55b..869cf031 100644 --- a/app/models/ad_image.rb +++ b/app/models/ad_image.rb @@ -4,8 +4,8 @@ class AdImage mount_uploader :file, ImageUploader - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :context, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy + field :title, localize: true + field :context, localize: true field :direct_to_after_click,type: Boolean diff --git a/app/models/asset.rb b/app/models/asset.rb index e20fcb9e..617f0496 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -6,10 +6,8 @@ class Asset mount_uploader :data, FileAssetUploader field :filename - field :description - - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :description, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy + field :description, localize: true + field :title, localize: true validates_presence_of :title, :data, :description @@ -17,29 +15,8 @@ class Asset belongs_to :assetable, polymorphic: true has_and_belongs_to_many :tags, :class_name => "AssetTag" - before_save :set_key - - def title - @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def description - @description ||= I18nVariable.first(:conditions => {:key => 'description', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - def sorted_tags tags.order_by(I18n.locale, :asc) end - - protected - - def set_key - if title && title.new_record? - title.key = 'title' - end - if description && description.new_record? - description.key = 'description' - end - end end diff --git a/app/models/asset_category.rb b/app/models/asset_category.rb index 89bceaf7..f12575ce 100644 --- a/app/models/asset_category.rb +++ b/app/models/asset_category.rb @@ -4,8 +4,7 @@ class AssetCategory field :key field :display - - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy + field :title, localize: true has_many :assets diff --git a/app/models/item.rb b/app/models/item.rb index 8dd5c0b4..60f737fe 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -10,8 +10,8 @@ class Item field :is_published, :type => Boolean, :default => false field :enabled_for, :type => Array, :default => nil field :menu_enabled_for, :type => Array, :default => nil + field :title, localize: true - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } diff --git a/app/models/meta/meta.rb b/app/models/meta/meta.rb index cd713d83..7a34212e 100644 --- a/app/models/meta/meta.rb +++ b/app/models/meta/meta.rb @@ -4,16 +4,15 @@ class Meta include Mongoid::Timestamps field :key + field :title, localize: true field :value, :default => nil - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy - def get_name_content [self.key, get_content] end def get_content - self.value ? self.value : self.i18n_variable[I18n.locale] + self.value ? self.value : self.title end end diff --git a/app/models/page.rb b/app/models/page.rb index 970df4cf..cb86c842 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -9,10 +9,10 @@ class Page < Item field :category field :tag field :view_count, :type => Integer, :default => 0 + field :page_title, localize: true belongs_to :design belongs_to :module_app - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy has_many :page_parts, :autosave => true, :dependent => :destroy has_many :page_contexts, :autosave => true, :dependent => :destroy has_many :page_metas, :autosave => true, :dependent => :destroy @@ -21,10 +21,6 @@ class Page < Item before_save :create_parts, :set_key # embeds_many :custom_images, :class_name => 'Image', as: :design_image - - def title - @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end protected @@ -47,10 +43,4 @@ class Page < Item end end - def set_key - if title && title.new_record? - title.key = 'title' - end - end - end diff --git a/app/models/page_part.rb b/app/models/page_part.rb index 52eafdf3..5f4b1dd6 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -4,34 +4,15 @@ class PagePart include Mongoid::Timestamps field :name - field :content + field :content, localize: true field :kind field :public_r_tag field :public_r_tag_object_id, :default => nil field :public_r_tag_option, :default => nil + field :page_title, localize: true field :widget_path - - has_one :i18n_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy belongs_to :page belongs_to :module_app - before_save :set_key - - def i18n_variable - @i18n_variable ||= I18nVariable.first(:conditions => {:key => 'i18n_variable', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def title - @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - protected - - def set_key - title.key = 'title' if title && (title.key.blank? rescue true) - i18n_variable.key = 'i18n_variable' if i18n_variable && (i18n_variable.key.blank? rescue true) - end - end \ No newline at end of file diff --git a/app/models/site.rb b/app/models/site.rb index 4bd4e19f..103e7249 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -20,12 +20,11 @@ class Site field :search,:type => Hash - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :footer, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :sub_menu, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_many :site_metas, :autosave => true, :dependent => :destroy + field :title, localize: true + field :footer, localize: true + field :sub_menu, localize: true - before_save :set_key + has_many :site_metas, :autosave => true, :dependent => :destroy def generate_keys private_key = OpenSSL::PKey::RSA.generate(2048) @@ -42,18 +41,6 @@ class Site res.eql?('local out of date') ? false : true end - def title - @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def footer - @footer ||= I18nVariable.first(:conditions => {:key => 'footer', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def sub_menu - @sub_menu ||= I18nVariable.first(:conditions => {:key => 'sub_menu', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - METAS.each do |meta| define_method meta do fetch_meta = self.site_metas.where(key: meta).limit(1) @@ -68,13 +55,5 @@ class Site end end end - - protected - - def set_key - title.key = 'title' if title && (title.key.blank? rescue true) - footer.key = 'footer' if footer && (footer.key.blank? rescue true) - sub_menu.key = 'sub_menu' if sub_menu && (sub_menu.key.blank? rescue true) - end end diff --git a/app/models/user/attribute.rb b/app/models/user/attribute.rb index af8a1106..faa5876a 100644 --- a/app/models/user/attribute.rb +++ b/app/models/user/attribute.rb @@ -6,9 +6,9 @@ class Attribute field :key field :built_in, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false + field :title, localize: true has_many :attribute_fields, :autosave => true, :dependent => :destroy - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :attribute_fields, :allow_destroy => true def is_built_in? diff --git a/app/models/user/attribute_field.rb b/app/models/user/attribute_field.rb index 42bbf910..2a227d27 100644 --- a/app/models/user/attribute_field.rb +++ b/app/models/user/attribute_field.rb @@ -9,9 +9,9 @@ class AttributeField field :list_options, :type => Array field :built_in, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false + field :title, localize: true belongs_to :attribute - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_many :attribute_values # validates_uniqueness_of :key diff --git a/app/models/user/role.rb b/app/models/user/role.rb index 86f7266f..64db9ac2 100644 --- a/app/models/user/role.rb +++ b/app/models/user/role.rb @@ -6,9 +6,9 @@ class Role field :key field :built_in, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false + field :title, localize: true has_many :sub_roles, :autosave => true, :dependent => :destroy - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy has_many :users accepts_nested_attributes_for :i18n_variable, :allow_destroy => true accepts_nested_attributes_for :sub_roles, :allow_destroy => true diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index b6781500..a13e0ff3 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -145,7 +145,7 @@ namespace :migrate do p '=====================================================' p 'Start PageContext' - page_contexts = PageContext.admin_manager_all + page_contexts = PageContext.all i = 1 page_contexts.each do |page_context| p "#{i}/#{page_contexts.size} - #{page_context.id}" @@ -160,12 +160,13 @@ namespace :migrate do p '=====================================================' p 'Start WebLink' - links = WebLink.admin_manager_all + links = WebLink.all i = 1 links.each do |link| p "#{i}/#{links.size} - #{link.id}" - name = I18nVariable.first(:conditions => {:key => 'name', :language_value_id => link.id, :language_value_type => link.class}) - link.name_translations = {'en' => name['en'], 'zh_tw' => name['zh_tw']} if name + # the original key was 'name' + title = I18nVariable.first(:conditions => {:key => 'name', :language_value_id => link.id, :language_value_type => link.class}) + link.name_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title context = I18nVariable.first(:conditions => {:key => 'context', :language_value_id => link.id, :language_value_type => link.class}) link.context_translations = {'en' => context['en'], 'zh_tw' => context['zh_tw']} if context link.save(:validate => false) @@ -187,6 +188,173 @@ namespace :migrate do end p 'End WebLinkCategory' + p '=====================================================' + p '=====================================================' + + p 'Start Meta' + metas = Meta.all + i = 1 + metas.each do |meta| + p "#{i}/#{metas.size} - #{meta.id}" + title = I18nVariable.first(:conditions => {:language_value_id => meta.id, :language_value_type => meta.class}) + meta.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + meta.save(:validate => false) + i += 1 + end + p 'End Meta' + + p '=====================================================' + p '=====================================================' + + p 'Start Attribute' + attributes = Attribute.all + i = 1 + attributes.each do |attribute| + p "#{i}/#{attributes.size} - #{attribute.id}" + title = I18nVariable.first(:conditions => {:language_value_id => attribute.id, :language_value_type => attribute.class}) + attribute.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + attribute.save(:validate => false) + i += 1 + end + p 'End Attribute' + + p '=====================================================' + + p 'Start AttributeField' + attribute_fields = AttributeField.all + i = 1 + attribute_fields.each do |attribute_field| + p "#{i}/#{attribute_fields.size} - #{attribute_field.id}" + title = I18nVariable.first(:conditions => {:language_value_id => attribute_field.id, :language_value_type => attribute_field.class}) + attribute_field.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + attribute_field.save(:validate => false) + i += 1 + end + p 'End AttributeField' + + p '=====================================================' + + p 'Start Role' + roles = Role.all + i = 1 + roles.each do |role| + p "#{i}/#{roles.size} - #{role.id}" + title = I18nVariable.first(:conditions => {:language_value_id => role.id, :language_value_type => role.class}) + role.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + role.save(:validate => false) + i += 1 + end + p 'End Role' + + p '=====================================================' + p '=====================================================' + + p 'Start AdImage' + ad_images = AdImage.all + i = 1 + ad_images.each do |image| + p "#{i}/#{ad_images.size} - #{image.id}" + # no key is set and title = context, this is due to original error in the code + title = I18nVariable.first(:conditions => {:language_value_id => image.id, :language_value_type => image.class}) + image.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + image.context_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + image.save(:validate => false) + i += 1 + end + p 'End AdImage' + + p '=====================================================' + p '=====================================================' + + p 'Start Asset' + assets = Asset.all + i = 1 + assets.each do |asset| + p "#{i}/#{assets.size} - #{asset.id}" + title = I18nVariable.first(:conditions => {:key => 'title', :language_value_id => asset.id, :language_value_type => asset.class}) + asset.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + description = I18nVariable.first(:conditions => {:key => 'description', :language_value_id => asset.id, :language_value_type => asset.class}) + asset.description_translations = {'en' => description['en'], 'zh_tw' => description['zh_tw']} if description + asset.save(:validate => false) + i += 1 + end + p 'End Asset' + + p '=====================================================' + + p 'Start AssetCategory' + categories = AssetCategory.all + i = 1 + categories.each do |category| + p "#{i}/#{categories.size} - #{category.id}" + title = I18nVariable.first(:conditions => {:language_value_id => category.id, :language_value_type => category.class}) + category.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + category.save(:validate => false) + i += 1 + end + p 'End AssetCategory' + + p '=====================================================' + p '=====================================================' + + p 'Start Item' + items = Item.all + i = 1 + items.each do |item| + p "#{i}/#{items.size} - #{item.id}" + title = I18nVariable.first(:conditions => {:language_value_id => item.id, :language_value_type => item.class}) + item.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + item.save(:validate => false) + i += 1 + end + p 'End Item' + + p '=====================================================' + + p 'Start Page' + pages = Page.all + i = 1 + pages.each do |item| + p "#{i}/#{pages.size} - #{item.id}" + # the key was originally 'title', changed to 'page_title' to avoit conflict with parent + title = I18nVariable.first(:conditions => {:key => 'title', :language_value_id => item.id, :language_value_type => item.class}) + item.page_title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + item.save(:validate => false) + i += 1 + end + p 'End Page' + + p '=====================================================' + + p 'Start PagePart' + page_parts = PagePart.all + i = 1 + page_parts.each do |part| + p "#{i}/#{page_parts.size} - #{part.id}" + title = I18nVariable.first(:conditions => {:key => 'title', :language_value_id => part.id, :language_value_type => part.class}) + part.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + content = I18nVariable.first(:conditions => {:key => 'i18n_variable', :language_value_id => part.id, :language_value_type => part.class}) + part.content_translations = {'en' => content['en'], 'zh_tw' => content['zh_tw']} if content + part.save(:validate => false) + i += 1 + end + p 'End PagePart' + + p '=====================================================' + p '=====================================================' + + p 'Start Site' + site = Site.first + title = I18nVariable.first(:conditions => {:key => 'title', :language_value_id => part.id, :language_value_type => part.class}) + site.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + footer = I18nVariable.first(:conditions => {:key => 'footer', :language_value_id => site.id, :language_value_type => site.class}) + site.footer_translations = {'en' => footer['en'], 'zh_tw' => footer['zh_tw']} if footer + sub_menu = I18nVariable.first(:conditions => {:key => 'sub_menu', :language_value_id => site.id, :language_value_type => site.class}) + site.sub_menu_translations = {'en' => sub_menu['en'], 'zh_tw' => sub_menu['zh_tw']} if sub_menu + site.save(:validate => false) + end + p 'End Site' + end end