From 207b0c5886106663f18fb53d3dfc32f3e859b918 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Thu, 26 Jul 2012 04:10:15 +0800 Subject: [PATCH] Remove i18n_variable for web_resource --- lib/tasks/migrate.rake | 17 +++++ .../back_end/web_links_controller.rb | 70 ------------------- .../front_end/web_links_controller.rb | 2 +- .../widget/web_links_controller.rb | 2 +- .../web_resource/app/models/web_link.rb | 39 ++--------- .../app/models/web_link_category.rb | 4 +- .../app/models/web_resource_tag.rb | 2 +- .../web_link_categorys/_form.html.erb | 4 +- .../_web_link_category.html.erb | 2 +- .../back_end/web_links/_form.html.erb | 8 +-- .../back_end/web_links/_sort_headers.html.erb | 2 +- .../back_end/web_links/_web_link.html.erb | 4 +- .../back_end/web_links/show.html.erb | 57 --------------- .../front_end/web_links/index.html.erb | 6 +- .../front_end/web_links/show.html.erb | 57 --------------- .../widget/web_links/_web_links.html.erb | 2 +- .../widget/web_links/index.html.erb | 6 +- 17 files changed, 46 insertions(+), 238 deletions(-) diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index da664bab..b9cbb605 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -156,6 +156,23 @@ namespace :migrate do end p 'End PageContext' + p '=====================================================' + p '=====================================================' + + p 'Start WebLink' + links = WebLink.admin_manager_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 + 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) + i += 1 + end + p 'End WebLink' + end end diff --git a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb index 40db805f..ef96e7d3 100644 --- a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb +++ b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb @@ -161,75 +161,5 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController module_app = ModuleApp.first(:conditions => {:key => 'web_resource'}) @tags = Tag.all(:conditions => {:module_app_id => module_app.id}) end - - def get_sorted_and_filtered_web_links - web_links = WebLink.all - case params[:sort] - when 'category' - category_ids = web_links.distinct(:web_link_category_id) - categories = WebLinkCategory.find(category_ids) rescue nil - if categories - h = Hash.new - categories.each { |category| h[category.i18n_variable[I18n.locale]] = category.id } - sorted = params[:direction].eql?('asc') ? h.sort : h.sort.reverse! - sorted_categorys = sorted.collect {|a| web_links.where(:web_link_category_id => a[1]).entries } - web_links = sorted_categorys.flatten! - end - when 'name' - h = Array.new - web_links.each { |web_link| h << [web_link.name[I18n.locale].downcase, web_link] } - sorted = params[:direction].eql?('asc') ? h.sort : h.sort.reverse! - web_links = sorted.collect {|a| a[1] } - when 'status' - web_links = web_links.order_by(:is_top, params[:direction]).order_by(:is_hot, params[:direction]).order_by(:is_hidden, params[:direction]).order_by(:is_pending, params[:direction]).order_by(:is_checked, params[:direction]).order_by(:is_rejected, params[:direction]) - when 'tags' - a = Array.new - WebResourceTag.all.order_by(I18n.locale, params[:direction]).each { |tag| a << tag.web_links } - a.flatten! - a.uniq! - tmp = Array.new - web_links.where(:tag_ids => []).each { |web_link| tmp << [web_link.name[I18n.locale].downcase, web_link] } - sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse! - sorted_names = sorted.collect {|a| a[1] } - a = params[:direction].eql?('asc') ? (sorted_names + a) : (a + sorted_names) - web_links = a.flatten - end - # if @filter - # @filter.each do |key, value| - # case key - # when 'status' - # a = Array.new - # web_links.each do |web_link| - # value.each do |v| - # case v - # when 'pending' - # a << web_link if web_link.is_checked.nil? - # when 'rejected' - # a << web_link if web_link.is_checked.eql?(false) - # else - # a << web_link if web_link[v] - # end - # end - # end - # web_links = a.uniq - # when 'categories' - # a = Array.new - # web_links.each do |web_link| - # a << web_link if value.include?(web_link.web_link_category.id.to_s) - # end - # web_links = a.uniq - # when 'tags' - # a = Array.new - # web_links.each do |web_link| - # web_link.tags.each do |tag| - # a << web_link if value.include?(tag.id.to_s) - # end - # end - # web_links = a.uniq - # end if value.size > 0 - # end - # end - Kaminari.paginate_array(web_links).page(params[:page]).per(10) - end end diff --git a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/front_end/web_links_controller.rb b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/front_end/web_links_controller.rb index a254c971..9b0698a8 100644 --- a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/front_end/web_links_controller.rb +++ b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/front_end/web_links_controller.rb @@ -12,7 +12,7 @@ class Panel::WebResource::FrontEnd::WebLinksController < OrbitWidgetController date_now = Time.now - @web_links = WebLink.where( :is_hidden => false ).desc(:is_top, :name).page(params[:page]).per(10) + @web_links = WebLink.where( :is_hidden => false ).desc(:is_top, :title).page(params[:page]).per(10) get_categorys end diff --git a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb index fcb0cb1b..a9fe23d8 100644 --- a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb +++ b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb @@ -28,7 +28,7 @@ class Panel::WebResource::Widget::WebLinksController < OrbitWidgetController # deadline - # @web_link = WebLink.where( :is_hidden => false ).desc(:is_top, :name).first + # @web_link = WebLink.where( :is_hidden => false ).desc(:is_top, :title).first @web_links = WebLink.widget_datas.page(params[:page]).per(5) diff --git a/vendor/built_in_modules/web_resource/app/models/web_link.rb b/vendor/built_in_modules/web_resource/app/models/web_link.rb index 4143c38b..be1f9682 100644 --- a/vendor/built_in_modules/web_resource/app/models/web_link.rb +++ b/vendor/built_in_modules/web_resource/app/models/web_link.rb @@ -6,8 +6,8 @@ class WebLink include Mongoid::MultiParameterAttributes - has_one :name, :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 has_and_belongs_to_many :tags, :class_name => "WebResourceTag" @@ -21,19 +21,17 @@ class WebLink belongs_to :web_link_category - validates_presence_of :name - - before_save :set_key + validates_presence_of :title def self.search( category_id = nil ) if category_id.to_s.size > 0 - find(:all, :conditions => {web_link_category_id: category_id}).desc( :is_top, :name ) + find(:all, :conditions => {web_link_category_id: category_id}).desc( :is_top, :title ) else - find(:all).desc( :is_top, :name) + find(:all).desc( :is_top, :title) end @@ -42,39 +40,16 @@ class WebLink def self.widget_datas - where( :is_hidden => false ).desc(:is_top, :name) + where( :is_hidden => false ).desc(:is_top, :title) end def is_top? self.is_top - end - - def name - @name ||= I18nVariable.first(:conditions => {:key => 'name', :language_value_id => self.id, :language_value_type => self.class}) rescue nil - end - - def context - @context ||= I18nVariable.first(:conditions => {:key => 'context', :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 + end def sorted_tags tags.order_by(I18n.locale, :asc) end - protected - - def set_key - if name && name.new_record? - name.key = 'name' - end - if context && context.new_record? - context.key = 'context' - end - end - end \ No newline at end of file diff --git a/vendor/built_in_modules/web_resource/app/models/web_link_category.rb b/vendor/built_in_modules/web_resource/app/models/web_link_category.rb index 53a10a1a..57f01981 100644 --- a/vendor/built_in_modules/web_resource/app/models/web_link_category.rb +++ b/vendor/built_in_modules/web_resource/app/models/web_link_category.rb @@ -12,12 +12,12 @@ class WebLinkCategory field :key - has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy + field :title, localize: true has_many :web_links def pp_object - i18n_variable[I18n.locale] + title end end \ No newline at end of file diff --git a/vendor/built_in_modules/web_resource/app/models/web_resource_tag.rb b/vendor/built_in_modules/web_resource/app/models/web_resource_tag.rb index bb97b351..9a7f3562 100644 --- a/vendor/built_in_modules/web_resource/app/models/web_resource_tag.rb +++ b/vendor/built_in_modules/web_resource/app/models/web_resource_tag.rb @@ -3,7 +3,7 @@ class WebResourceTag < Tag has_and_belongs_to_many :web_links - def get_visible_links(sort = :name) + def get_visible_links(sort = :title) self.web_links.where(:is_hidden => false).desc(:is_top, sort) end diff --git a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_form.html.erb b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_form.html.erb index 2e0f61ff..4b19235d 100644 --- a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_form.html.erb +++ b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_form.html.erb @@ -10,12 +10,12 @@
- <%= f.fields_for :i18n_variable, (@web_link_category.new_record? ? @web_link_category.build_i18n_variable : @web_link_category.i18n_variable) do |f| %> + <%= f.fields_for :title_translations do |f| %> <% @site_valid_locales.each do |locale| %>
<%= label_tag "name-#{locale}", "Name-#{I18nVariable.from_locale(locale)}", :class => 'control-label' %>
- <%= f.text_field locale, :class => 'input-xxlarge' %> + <%= f.text_field locale, :class => 'input-xxlarge', :value => (@web_link_category.title_translations[locale] rescue nil) %>
<% end %> diff --git a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_web_link_category.html.erb b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_web_link_category.html.erb index 8d018df4..4d1198c3 100644 --- a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_web_link_category.html.erb +++ b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_link_categorys/_web_link_category.html.erb @@ -14,6 +14,6 @@ <% end -%> <% @site_valid_locales.each do |locale| %> - <%= web_link_category.i18n_variable[locale] rescue nil %> + <%= web_link_category.title_translations[locale] rescue nil %> <% end %> \ No newline at end of file diff --git a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_form.html.erb b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_form.html.erb index 60574b94..1274b884 100644 --- a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_form.html.erb +++ b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_form.html.erb @@ -69,17 +69,17 @@
">
<%= f.label :name ,t("web_resource.name")%> - <%= f.fields_for :name, (@web_link.new_record? ? @web_link.build_name : @web_link.name ) do |f| %> + <%= f.fields_for :title_translations do |f| %> <%= I18nVariable.from_locale(locale) %> - <%= f.text_field locale, :class=>'post-title' %> + <%= f.text_field locale, :class=>'post-title', :value => (@web_link.title_translations[locale] rescue nil) %> <% end %>
<%= f.label :describe,t("web_resource.describe") %> - <%= f.fields_for :context, (@web_link.new_record? ? @web_link.build_context : @web_link.context ) do |f| %> + <%= f.fields_for :context_translations do |f| %> <%= I18nVariable.from_locale(locale) %> - <%= f.text_area locale, :style=>"width:100%" %> + <%= f.text_area locale, :style=>"width:100%", :value => (@web_link.context_translations[locale] rescue nil) %> <% end %>
diff --git a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_sort_headers.html.erb b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_sort_headers.html.erb index 8873ed20..a53e38bd 100644 --- a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_sort_headers.html.erb +++ b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_sort_headers.html.erb @@ -1,4 +1,4 @@ <%= render_sort_bar(true, ['status', ['is_top', 'is_hot', 'is_hidden', 'is_pending', 'is_checked', 'is_rejected'], 'span1', 'bulletin.status'], ['category', 'bulletin_category', 'span2', 'bulletin.category'], - ['name', 'name','span3', 'bulletin.title'], + ['title', 'title','span3', 'bulletin.title'], ['tags', 'tags', 'span2', 'bulletin.tags']).html_safe %> \ No newline at end of file diff --git a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_web_link.html.erb b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_web_link.html.erb index 85a6ad93..d34dc2b3 100644 --- a/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_web_link.html.erb +++ b/vendor/built_in_modules/web_resource/app/views/panel/web_resource/back_end/web_links/_web_link.html.erb @@ -19,9 +19,9 @@
- <%= web_link.web_link_category.i18n_variable[I18n.locale] rescue nil %> + <%= web_link.web_link_category.title rescue nil %> - <%= link_to web_link.name[I18n.locale], panel_web_resource_back_end_web_link_path(web_link) %> + <%= link_to web_link.title, panel_web_resource_back_end_web_link_path(web_link) %>