From f57e9f19f769d661f1499f4ff4f162d6348cf37a Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Mon, 13 Feb 2012 19:34:06 +0800 Subject: [PATCH] Changes for multiple i18n_variable in bulletin --- app/models/i18n_variable.rb | 8 ++- .../announcement/app/models/bulletin.rb | 64 ++++++++++++------- .../back_end/bulletins/_form.html.erb | 38 +++++++---- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/app/models/i18n_variable.rb b/app/models/i18n_variable.rb index b5983d35..741d7987 100644 --- a/app/models/i18n_variable.rb +++ b/app/models/i18n_variable.rb @@ -9,8 +9,12 @@ class I18nVariable belongs_to :language_value, polymorphic: true - def method_missing(field) - self[field] + def method_missing(*field) + if field.size > 1 + self.write_attribute(field[0].to_s.delete('=').to_sym, field[1]) + else + self[field[0]] + end end end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 4b621faf..0f12f179 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -5,13 +5,10 @@ class Bulletin include Mongoid::Timestamps include Mongoid::MultiParameterAttributes - # field :category_id, :type => Integer - field :title - # has_one :title_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - # has_one :subtitle_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - # has_one :text_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy - field :subtitle - field :text + 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 :postdate , :type => Date field :deadline , :type => Date # field :url @@ -19,6 +16,8 @@ class Bulletin field :update_user_id field :is_top, :type => Boolean, :default => false + field :is_hot, :type => Boolean, :default => false + field :is_hidden, :type => Boolean, :default => false mount_uploader :image, ImageUploader @@ -31,12 +30,13 @@ class Bulletin accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_links, :allow_destroy => true + accepts_nested_attributes_for :title, :allow_destroy => true + accepts_nested_attributes_for :subtitle, :allow_destroy => true + accepts_nested_attributes_for :text, :allow_destroy => true - # validates_presence_of :title_variable validates_presence_of :title - after_save :save_bulletin_links - after_save :save_bulletin_files + before_save :set_key def self.search( search = nil, category_id = nil ) @@ -82,20 +82,40 @@ class Bulletin self.is_top end - def save_bulletin_links - self.bulletin_links.each do |t| - if t.should_destroy - t.destroy - end + def is_hot? + self.is_hot + end + + def is_hidden? + self.is_hidden + 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 + + protected + + def set_key + if title.new_record? + title.key = 'title' + end + if subtitle.new_record? + subtitle.key = 'subtitle' + end + if text.new_record? + text.key = 'text' end end - def save_bulletin_files - self.bulletin_files.each do |t| - if t.should_destroy - t.destroy - end - end - end end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb index b222d77e..62be89cc 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb @@ -16,20 +16,32 @@ <% end %> -
- <%= f.label :title %>
- <%= f.text_field :title %> -
+
+ <%= f.label :title %> + <%= f.fields_for :title, (@bulletin.new_record? ? @bulletin.build_title : @bulletin.title ) do |f| %> + <% @site_valid_locales.each do |locale| %> + <%= f.text_field locale %> + <% end %> + <% end %> +
-
- <%= f.label :subtitle %>
- <%= f.text_area :subtitle, :rows => 10, :cols => 40 %> -
- -
- <%= f.label :text %>
- <%= f.text_area :text, :rows => 10, :cols => 40 %> -
+
+ <%= f.label :subtitle %> + <%= f.fields_for :subtitle, (@bulletin.new_record? ? @bulletin.build_subtitle : @bulletin.subtitle ) do |f| %> + <% @site_valid_locales.each do |locale| %> + <%= f.text_area locale, :rows => 10, :cols => 40 %> + <% end %> + <% end %> +
+ +
+ <%= f.label :text %> + <%= f.fields_for :text, (@bulletin.new_record? ? @bulletin.build_text : @bulletin.text ) do |f| %> + <% @site_valid_locales.each do |locale| %> + <%= f.text_area locale, :rows => 10, :cols => 40 %> + <% end %> + <% end %> +
<%= f.label :postdate %>