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
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

View File

@ -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

View File

@ -16,20 +16,32 @@
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :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 class="field">
<%= f.label :subtitle %><br />
<%= f.text_area :subtitle, :rows => 10, :cols => 40 %>
</div>
<div class="field">
<%= f.label :subtitle %>
<%= 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 class="field">
<%= f.label :text %><br />
<%= f.text_area :text, :rows => 10, :cols => 40 %>
</div>
<div class="field">
<%= f.label :text %>
<%= 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 class="field">
<%= f.label :postdate %><br />