Changes for multiple i18n_variable in bulletin

This commit is contained in:
Christophe Vilayphiou 2012-02-13 19:34:06 +08:00
parent 1f7dae9bee
commit f57e9f19f7
3 changed files with 73 additions and 37 deletions

View File

@ -9,8 +9,12 @@ class I18nVariable
belongs_to :language_value, polymorphic: true belongs_to :language_value, polymorphic: true
def method_missing(field) def method_missing(*field)
self[field] if field.size > 1
self.write_attribute(field[0].to_s.delete('=').to_sym, field[1])
else
self[field[0]]
end
end end
end end

View File

@ -5,13 +5,10 @@ class Bulletin
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes include Mongoid::MultiParameterAttributes
# field :category_id, :type => Integer has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
field :title has_one :subtitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
# has_one :title_variable, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy has_one :text, :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
field :postdate , :type => Date field :postdate , :type => Date
field :deadline , :type => Date field :deadline , :type => Date
# field :url # field :url
@ -19,6 +16,8 @@ class Bulletin
field :update_user_id field :update_user_id
field :is_top, :type => Boolean, :default => false 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 mount_uploader :image, ImageUploader
@ -31,12 +30,13 @@ class Bulletin
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
accepts_nested_attributes_for :bulletin_links, :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 validates_presence_of :title
after_save :save_bulletin_links before_save :set_key
after_save :save_bulletin_files
def self.search( search = nil, category_id = nil ) def self.search( search = nil, category_id = nil )
@ -82,20 +82,40 @@ class Bulletin
self.is_top self.is_top
end end
def save_bulletin_links def is_hot?
self.bulletin_links.each do |t| self.is_hot
if t.should_destroy
t.destroy
end 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
end end
def save_bulletin_files
self.bulletin_files.each do |t|
if t.should_destroy
t.destroy
end
end
end
end end

View File

@ -17,18 +17,30 @@
</div> </div>
<div class="field"> <div class="field">
<%= f.label :title %><br /> <%= f.label :title %>
<%= f.text_field :title %> <%= f.fields_for :title, (@bulletin.new_record? ? @bulletin.build_title : @bulletin.title ) do |f| %>
<% @site_valid_locales.each do |locale| %>
<td><%= f.text_field locale %></td>
<% end %>
<% end %>
</div> </div>
<div class="field"> <div class="field">
<%= f.label :subtitle %><br /> <%= f.label :subtitle %>
<%= f.text_area :subtitle, :rows => 10, :cols => 40 %> <%= f.fields_for :subtitle, (@bulletin.new_record? ? @bulletin.build_subtitle : @bulletin.subtitle ) do |f| %>
<% @site_valid_locales.each do |locale| %>
<td><%= f.text_area locale, :rows => 10, :cols => 40 %></td>
<% end %>
<% end %>
</div> </div>
<div class="field"> <div class="field">
<%= f.label :text %><br /> <%= f.label :text %>
<%= f.text_area :text, :rows => 10, :cols => 40 %> <%= f.fields_for :text, (@bulletin.new_record? ? @bulletin.build_text : @bulletin.text ) do |f| %>
<% @site_valid_locales.each do |locale| %>
<td><%= f.text_area locale, :rows => 10, :cols => 40 %></td>
<% end %>
<% end %>
</div> </div>
<div class="field"> <div class="field">