diff --git a/app/controllers/orbit_backend_controller.rb b/app/controllers/orbit_backend_controller.rb index feefc98e6..6bc42ec91 100644 --- a/app/controllers/orbit_backend_controller.rb +++ b/app/controllers/orbit_backend_controller.rb @@ -39,11 +39,10 @@ class OrbitBackendController< ApplicationController options = [options] if !options.class.eql?(Array) options.each do |option| if object_class.fields.include?(option) - case object_class.fields[option].type.to_s - when 'BigDecimal', 'Boolean', 'Date', 'DateTime', 'Float', 'Integer', 'String', 'Symbol', 'Time' - (objects = objects.order_by(option, params[:direction])) rescue nil - when 'Object' - objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option) + if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil? + objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option) + else + (objects = objects.order_by(option, params[:direction])) rescue nil end elsif object_class.relations.include?(option) case object_class.relations[option].macro @@ -92,20 +91,11 @@ class OrbitBackendController< ApplicationController end def get_string_value_from_object(object) - s = object[I18n.locale] rescue nil - s = object.i18n_variable unless s rescue nil + s = object.name_translations[I18n.locale.to_s] unless s rescue nil + s = object.title_translations[I18n.locale.to_s] unless s rescue nil s = object.name unless s rescue nil s = object.title unless s rescue nil - if s - case s.class.to_s - when "String" - s.downcase rescue '' - when "I18nVariable" - s[I18n.locale].downcase rescue '' - else - nil - end - end + s.downcase rescue '' end def get_objects_from_referenced_objects(object_class, objects, option) diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake new file mode 100644 index 000000000..b8858ad82 --- /dev/null +++ b/lib/tasks/migrate.rake @@ -0,0 +1,83 @@ +# encoding: utf-8 + +namespace :migrate do + + task :i18n => :environment do + + p 'Start Bulletin' + bulletins = Bulletin.admin_manager_all + i = 1 + bulletins.each do |bulletin| + p "#{i}/#{bulletins.size} - #{bulletin.id}" + title = I18nVariable.first(:conditions => {:key => 'title', :language_value_id => bulletin.id, :language_value_type => bulletin.class}) + bulletin.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + subtitle = I18nVariable.first(:conditions => {:key => 'subtitle', :language_value_id => bulletin.id, :language_value_type => bulletin.class}) + bulletin.subtitle_translations = {'en' => subtitle['en'], 'zh_tw' => subtitle['zh_tw']} if subtitle + text = I18nVariable.first(:conditions => {:key => 'text', :language_value_id => bulletin.id, :language_value_type => bulletin.class}) + bulletin.text_translations = {'en' => text['en'], 'zh_tw' => text['zh_tw']} if text + bulletin.save(:validate => false) + i += 1 + end + p 'End Bulletin' + + p '=====================================================' + + p 'Start BulletinCategory' + categories = BulletinCategory.admin_manager_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 BulletinCategory' + + p '=====================================================' + + p 'Start BulletinFile' + files = BulletinFile.all + i = 1 + files.each do |file| + p "#{i}/#{files.size} - #{file.id}" + description = I18nVariable.first(:conditions => {:language_value_id => file.id, :language_value_type => file.class}) + file.description_translations = {'en' => description['en'], 'zh_tw' => description['zh_tw']} if description + title = I18nVariable.first(:conditions => {:language_value_id => file.id, :language_value_type => file.class}) + file.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + file.save(:validate => false) + i += 1 + end + p 'End BulletinFile' + + p '=====================================================' + + p 'Start BulletinLink' + links = BulletinLink.all + i = 1 + links.each do |link| + p "#{i}/#{links.size} - #{link.id}" + title = I18nVariable.first(:conditions => {:language_value_id => link.id, :language_value_type => link.class}) + link.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + link.save(:validate => false) + i += 1 + end + p 'End BulletinLink' + + p '=====================================================' + + p 'Start UnitListForAnc' + units = UnitListForAnc.all + i = 1 + units.each do |unit| + p "#{i}/#{units.size} - #{unit.id}" + title = I18nVariable.first(:conditions => {:language_value_id => unit.id, :language_value_type => unit.class}) + unit.title_translations = {'en' => title['en'], 'zh_tw' => title['zh_tw']} if title + unit.save(:validate => false) + i += 1 + end + p 'End UnitListForAnc' + + end + +end diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/approvals_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/approvals_controller.rb index 0246ab7ce..f787ff280 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/approvals_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/approvals_controller.rb @@ -26,7 +26,7 @@ class Panel::Announcement::BackEnd::ApprovalsController < OrbitBackendControlle def setting @sys_users = User.all(conditions: {admin: false}).includes(:avatar) @bulletin_categorys = BulletinCategory.all - @options_from_collection_for_select_bulletin_categorys = @bulletin_categorys.collect{|bc| [bc.i18n_variable[I18n.locale],bc.id] } + @options_from_collection_for_select_bulletin_categorys = @bulletin_categorys.collect{|bc| [bc.title,bc.id] } if params.has_key? :category @bulletin_category = BulletinCategory.find params[:category][:id] else diff --git a/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb b/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb index 3f57fd3af..5de4a8260 100644 --- a/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb +++ b/vendor/built_in_modules/announcement/app/helpers/panel/announcement/back_end/bulletins_helper.rb @@ -14,9 +14,9 @@ module Panel::Announcement::BackEnd::BulletinsHelper def show_bulletin_title_at_index (bulletin) if bulletin.is_checked? - link_to bulletin.title[I18n.locale], panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue '' + link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue '' else - bulletin.title[I18n.locale] + bulletin.title end end diff --git a/vendor/built_in_modules/announcement/app/models/announcement_tag.rb b/vendor/built_in_modules/announcement/app/models/announcement_tag.rb index 630779d15..594c221d4 100644 --- a/vendor/built_in_modules/announcement/app/models/announcement_tag.rb +++ b/vendor/built_in_modules/announcement/app/models/announcement_tag.rb @@ -3,7 +3,7 @@ class AnnouncementTag < Tag has_and_belongs_to_many :bulletins field :order,:default => 0 - def get_visible_bulletins(sort = :name) + def get_visible_bulletins(sort = :title) date_now = Time.now self.bulletins.where(:is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, sort) end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index a6b0e55f6..e7ff24dc3 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -15,9 +15,10 @@ class Bulletin is_impressionable :counter_cache => { :column_name => :view_count } - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :subtitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :text, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy + field :title, localize: true + field :subtitle, localize: true + field :text, localize: true + has_and_belongs_to_many :tags, :class_name => "AnnouncementTag" field :postdate , :type => DateTime @@ -55,9 +56,9 @@ class Bulletin accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_links, :allow_destroy => true - validates_presence_of :title + validates :title, :presence => true - before_save :set_key,:check_deadline,:update_status + before_save :check_deadline,:update_status after_save :save_bulletin_links after_save :save_bulletin_files @@ -157,19 +158,6 @@ end end end end - - - def title - @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def subtitle - @subtitle ||= I18nVariable.first(:conditions => {:key => 'subtitle', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def text - @text ||= I18nVariable.first(:conditions => {:key => 'text', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end def self.filter(bulletins) bulletins.each do |bulletin| @@ -191,18 +179,6 @@ end end - def set_key - if title && title.new_record? - title.key = 'title' - end - if subtitle && subtitle.new_record? - subtitle.key = 'subtitle' - end - if text && text.new_record? - text.key = 'text' - end - end - def update_status if !self.is_pending if !self.is_checked diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_category.rb b/vendor/built_in_modules/announcement/app/models/bulletin_category.rb index 2f25c8882..7de9bb2c5 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin_category.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin_category.rb @@ -13,18 +13,15 @@ class BulletinCategory PAYMENT_TYPES = [ "List", "Picture" ] APP_NAME = 'Announcement' - field :key + field :disable, type: Boolean, :default => false field :display - field :disable,type: Boolean,:default => false - - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy + field :key + field :title, localize: true has_many :bulletins - - def pp_object - i18n_variable[I18n.locale] + name end def self.from_id(id) diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_file.rb b/vendor/built_in_modules/announcement/app/models/bulletin_file.rb index a30ec1311..2912fc976 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin_file.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin_file.rb @@ -4,37 +4,11 @@ class BulletinFile include Mongoid::Timestamps mount_uploader :file, AssetUploader - - # field :filetitle - # field :description - has_one :filetitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - has_one :description, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - - # field :to_save, :type => Boolean - field :should_destroy, :type => Boolean + + field :description, localize: true + field :should_destroy, :type => Boolean + field :title, localize: true belongs_to :bulletin - # embedded_in :bulletin - - before_save :set_key - - def filetitle - @filetitle ||= I18nVariable.first(:conditions => {:key => 'filetitle', :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 - - protected - - def set_key - if filetitle && filetitle.new_record? - filetitle.key = 'filetitle' - end - if description && description.new_record? - description.key = 'description' - end - end end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_link.rb b/vendor/built_in_modules/announcement/app/models/bulletin_link.rb index 303631820..47a447893 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin_link.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin_link.rb @@ -4,9 +4,7 @@ class BulletinLink include Mongoid::Timestamps field :url - # field :name - - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy + field :title, localize: true field :should_destroy, :type => Boolean diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb b/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb deleted file mode 100644 index 406642620..000000000 --- a/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb +++ /dev/null @@ -1,14 +0,0 @@ -class AnnouncementTag < Tag - - has_and_belongs_to_many :bulletins - - - def get_visible_bulletins(sort = :name) - date_now = Time.now - self.bulletins.all.where(:is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, sort) - end - - def bulletins - Bulletin.all.any_in(:_id => bulletin_ids) - end -end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/models/unit_list_for_anc.rb b/vendor/built_in_modules/announcement/app/models/unit_list_for_anc.rb index 5261f6a2a..179e95886 100644 --- a/vendor/built_in_modules/announcement/app/models/unit_list_for_anc.rb +++ b/vendor/built_in_modules/announcement/app/models/unit_list_for_anc.rb @@ -5,8 +5,7 @@ class UnitListForAnc field :order field :ut_code field :up_ut_code - - has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true + field :title, localize: true end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletin_categorys/_bulletin_category.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletin_categorys/_bulletin_category.html.erb index 58a9a5f9b..a0f021a8f 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletin_categorys/_bulletin_category.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletin_categorys/_bulletin_category.html.erb @@ -15,6 +15,6 @@ <% @site_valid_locales.each do |locale| %> -