From 5ab32baf88a805b72386a87ce2db9f0948c9f285 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 8 Jan 2010 15:32:44 +0800 Subject: [PATCH] Add MongoMapper key_i18n support --- app/controllers/application_controller.rb | 4 ++-- app/models/announcement.rb | 16 +++----------- app/models/layout.rb | 3 +-- app/models/page.rb | 5 ++--- app/models/snippet.rb | 3 +-- app/models/snippet_filter.rb | 2 +- config/environment.rb | 26 +++++++++++------------ 7 files changed, 23 insertions(+), 36 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c5a4bacf..d2bfd123 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,8 +13,8 @@ class ApplicationController < ActionController::Base if @page @layout = @page.layout @page_options ||= {} - @page_content = Liquid::Template.parse( @page.send( "content_#{I18n.locale}" ) ).render(@page_options) - @layout_content = (@page.layout)? @layout.send( "content_#{I18n.locale}" ) : "{{page_content}}" + @page_content = Liquid::Template.parse( @page.content ).render(@page_options) + @layout_content = (@page.layout)? @layout.content : "{{page_content}}" render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) else render :text => '404 Not Found' diff --git a/app/models/announcement.rb b/app/models/announcement.rb index ea8c2edc..fc8e1fb5 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -2,21 +2,11 @@ class Announcement include MongoMapper::Document - key :title_en, String - key :title_zh_tw, String - key :content_en, String - key :content_zh_tw, String + key_i18n :title, String + key_i18n :content, String def to_liquid - { "id" => self.id, "title" => self.send("title_#{I18n.locale}"), "content" => self.send("content_#{I18n.locale}") } - end - - def title - self.send("title_#{I18n.locale}") - end - - def content - self.send("content_#{I18n.locale}") + { "id" => self.id, "title" => self.title, "content" => self.content } end end \ No newline at end of file diff --git a/app/models/layout.rb b/app/models/layout.rb index 8579b8b5..2c65d556 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -3,8 +3,7 @@ class Layout include MongoMapper::Document key :name, String - key :content_en, String - key :content_zh_tw, String + key_i18n :content, String validates_presence_of :name diff --git a/app/models/page.rb b/app/models/page.rb index d2caa579..25b87a2a 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -3,9 +3,8 @@ class Page key :name, String key :parent_page_id, String - - key :content_en, String - key :content_zh_tw, String + + key_i18n :content, String key :layout_id, String key :layout_name, String diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 46604d2a..6b4e5cea 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -3,8 +3,7 @@ class Snippet include MongoMapper::Document key :name, String - key :content_en, String - key :content_zh_tw, String + key_i18n :content, String validates_presence_of :name diff --git a/app/models/snippet_filter.rb b/app/models/snippet_filter.rb index afa1b8c6..a7414bc1 100644 --- a/app/models/snippet_filter.rb +++ b/app/models/snippet_filter.rb @@ -4,7 +4,7 @@ module SnippetFilter snippet = Snippet.find_by_name(snippet_name) if snippet - return Liquid::Template.parse( snippet.send( "content_#{I18n.locale}" ) ).render + return Liquid::Template.parse( snippet.content ).render else return "nothing" end diff --git a/config/environment.rb b/config/environment.rb index 36f570ec..e8a77c71 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -50,18 +50,18 @@ VALID_LOCALES = ["en", "zh_tw"] MongoMapper.database = "r4-#{Rails.env}" -#class CouchFoo::Base -# -# def self.property_i18n(property_name, property_type) -# VALID_LOCALES.each do |locale| -# property "#{property_name.to_s}_#{locale}".to_sym, property_type -# end -# -# define_method( property_name ) do -# self.read_attribute("#{property_name.to_s}_#{I18n.locale}") -# end -# end -# -#end +module MongoMapper::Document::ClassMethods + + def key_i18n(key, *options) + VALID_LOCALES.each do |locale| + key "#{key.to_s}_#{locale}".to_sym, *options + end + + define_method(key) do + self.send("#{key.to_s}_#{I18n.locale}") + end + end + +end