Remove i18n_variable for controllers, helpers and models

This commit is contained in:
Christophe Vilayphiou 2012-07-26 05:07:32 +08:00
parent 94337a58d5
commit acb7fe0dea
16 changed files with 196 additions and 103 deletions

View File

@ -30,7 +30,7 @@ class Admin::AssetsController < OrbitBackendController
def create def create
@asset = Asset.new(params[:asset]) @asset = Asset.new(params[:asset])
@asset.filename = @asset.title[I18n.locale] rescue nil @asset.filename = @asset.title rescue nil
if @asset.filename && @asset.save if @asset.filename && @asset.save
respond_to do |format| respond_to do |format|
format.js { format.js {

View File

@ -3,7 +3,7 @@ module Admin::AssetHelper
def show_all_fields(asset, field) def show_all_fields(asset, field)
a = [] a = []
@site_valid_locales.each do |locale| @site_valid_locales.each do |locale|
a << asset.send(field)[locale] rescue nil a << asset.send("#{field}_translations")[locale] rescue nil
end end
a.join(' - ') a.join(' - ')
end end

View File

@ -13,7 +13,7 @@ module Admin::ItemHelper
ret << "<li id='#{node.id}' class='#{no_nested}'>" ret << "<li id='#{node.id}' class='#{no_nested}'>"
# ret << "<i class='icons-moves'></i>" # ret << "<i class='icons-moves'></i>"
ret << "<div class='with_action'><i class='icons-moves'></i>" ret << "<div class='with_action'><i class='icons-moves'></i>"
ret << (link_to node.i18n_variable[I18n.locale], dest, :class => 'js_history') ret << (link_to node.title, dest, :class => 'js_history')
ret << "<div class='quick-edit hide'>" ret << "<div class='quick-edit hide'>"
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.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') 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')

View File

@ -122,11 +122,11 @@ module ApplicationHelper
def page_title(page) def page_title(page)
res = "<title>" res = "<title>"
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 if page.root? && @site.title
res << @site.title[I18n.locale] res << @site.title
elsif @site.title && @site.title_always_on elsif @site.title && @site.title_always_on
res << @site.title[I18n.locale] + ' - ' + page_title res << @site.title + ' - ' + page_title
else else
res << page_title res << page_title
end end

View File

@ -4,8 +4,8 @@ class AdImage
mount_uploader :file, ImageUploader mount_uploader :file, ImageUploader
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy field :title, localize: true
has_one :context, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy field :context, localize: true
field :direct_to_after_click,type: Boolean field :direct_to_after_click,type: Boolean

View File

@ -6,10 +6,8 @@ class Asset
mount_uploader :data, FileAssetUploader mount_uploader :data, FileAssetUploader
field :filename field :filename
field :description field :description, localize: true
field :title, localize: true
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
validates_presence_of :title, :data, :description validates_presence_of :title, :data, :description
@ -17,29 +15,8 @@ class Asset
belongs_to :assetable, polymorphic: true belongs_to :assetable, polymorphic: true
has_and_belongs_to_many :tags, :class_name => "AssetTag" 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 def sorted_tags
tags.order_by(I18n.locale, :asc) tags.order_by(I18n.locale, :asc)
end 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 end

View File

@ -4,8 +4,7 @@ class AssetCategory
field :key field :key
field :display field :display
field :title, localize: true
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :assets has_many :assets

View File

@ -10,8 +10,8 @@ class Item
field :is_published, :type => Boolean, :default => false field :is_published, :type => Boolean, :default => false
field :enabled_for, :type => Array, :default => nil field :enabled_for, :type => Array, :default => nil
field :menu_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_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }

View File

@ -4,16 +4,15 @@ class Meta
include Mongoid::Timestamps include Mongoid::Timestamps
field :key field :key
field :title, localize: true
field :value, :default => nil field :value, :default => nil
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
def get_name_content def get_name_content
[self.key, get_content] [self.key, get_content]
end end
def get_content def get_content
self.value ? self.value : self.i18n_variable[I18n.locale] self.value ? self.value : self.title
end end
end end

View File

@ -9,10 +9,10 @@ class Page < Item
field :category field :category
field :tag field :tag
field :view_count, :type => Integer, :default => 0 field :view_count, :type => Integer, :default => 0
field :page_title, localize: true
belongs_to :design belongs_to :design
belongs_to :module_app 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_parts, :autosave => true, :dependent => :destroy
has_many :page_contexts, :autosave => true, :dependent => :destroy has_many :page_contexts, :autosave => true, :dependent => :destroy
has_many :page_metas, :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 before_save :create_parts, :set_key
# embeds_many :custom_images, :class_name => 'Image', as: :design_image # 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 protected
@ -47,10 +43,4 @@ class Page < Item
end end
end end
def set_key
if title && title.new_record?
title.key = 'title'
end
end
end end

View File

@ -4,34 +4,15 @@ class PagePart
include Mongoid::Timestamps include Mongoid::Timestamps
field :name field :name
field :content field :content, localize: true
field :kind field :kind
field :public_r_tag field :public_r_tag
field :public_r_tag_object_id, :default => nil field :public_r_tag_object_id, :default => nil
field :public_r_tag_option, :default => nil field :public_r_tag_option, :default => nil
field :page_title, localize: true
field :widget_path 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 :page
belongs_to :module_app 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 end

View File

@ -18,12 +18,11 @@ class Site
field :school field :school
field :department field :department
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy field :title, localize: true
has_one :footer, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy field :footer, localize: true
has_one :sub_menu, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy field :sub_menu, localize: true
has_many :site_metas, :autosave => true, :dependent => :destroy
before_save :set_key has_many :site_metas, :autosave => true, :dependent => :destroy
def generate_keys def generate_keys
private_key = OpenSSL::PKey::RSA.generate(2048) private_key = OpenSSL::PKey::RSA.generate(2048)
@ -40,18 +39,6 @@ class Site
res.eql?('local out of date') ? false : true res.eql?('local out of date') ? false : true
end 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| METAS.each do |meta|
define_method meta do define_method meta do
fetch_meta = self.site_metas.where(key: meta).limit(1) fetch_meta = self.site_metas.where(key: meta).limit(1)
@ -66,13 +53,5 @@ class Site
end end
end 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 end

View File

@ -6,9 +6,9 @@ class Attribute
field :key field :key
field :built_in, :type => Boolean, :default => false field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false
field :title, localize: true
has_many :attribute_fields, :autosave => true, :dependent => :destroy 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 accepts_nested_attributes_for :attribute_fields, :allow_destroy => true
def is_built_in? def is_built_in?

View File

@ -9,9 +9,9 @@ class AttributeField
field :list_options, :type => Array field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false
field :title, localize: true
belongs_to :attribute belongs_to :attribute
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :attribute_values has_many :attribute_values
# validates_uniqueness_of :key # validates_uniqueness_of :key

View File

@ -6,9 +6,9 @@ class Role
field :key field :key
field :built_in, :type => Boolean, :default => false field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false
field :title, localize: true
has_many :sub_roles, :autosave => true, :dependent => :destroy has_many :sub_roles, :autosave => true, :dependent => :destroy
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :users has_many :users
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
accepts_nested_attributes_for :sub_roles, :allow_destroy => true accepts_nested_attributes_for :sub_roles, :allow_destroy => true

View File

@ -145,7 +145,7 @@ namespace :migrate do
p '=====================================================' p '====================================================='
p 'Start PageContext' p 'Start PageContext'
page_contexts = PageContext.admin_manager_all page_contexts = PageContext.all
i = 1 i = 1
page_contexts.each do |page_context| page_contexts.each do |page_context|
p "#{i}/#{page_contexts.size} - #{page_context.id}" p "#{i}/#{page_contexts.size} - #{page_context.id}"
@ -160,12 +160,13 @@ namespace :migrate do
p '=====================================================' p '====================================================='
p 'Start WebLink' p 'Start WebLink'
links = WebLink.admin_manager_all links = WebLink.all
i = 1 i = 1
links.each do |link| links.each do |link|
p "#{i}/#{links.size} - #{link.id}" p "#{i}/#{links.size} - #{link.id}"
name = I18nVariable.first(:conditions => {:key => 'name', :language_value_id => link.id, :language_value_type => link.class}) # the original key was 'name'
link.name_translations = {'en' => name['en'], 'zh_tw' => name['zh_tw']} if 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}) 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.context_translations = {'en' => context['en'], 'zh_tw' => context['zh_tw']} if context
link.save(:validate => false) link.save(:validate => false)
@ -187,6 +188,173 @@ namespace :migrate do
end end
p 'End WebLinkCategory' 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
end end