Add MongoMapper key_i18n support

This commit is contained in:
Wen-Tien Chang 2010-01-08 15:32:44 +08:00
parent a674e46c91
commit 5ab32baf88
7 changed files with 23 additions and 36 deletions

View File

@ -13,8 +13,8 @@ class ApplicationController < ActionController::Base
if @page if @page
@layout = @page.layout @layout = @page.layout
@page_options ||= {} @page_options ||= {}
@page_content = Liquid::Template.parse( @page.send( "content_#{I18n.locale}" ) ).render(@page_options) @page_content = Liquid::Template.parse( @page.content ).render(@page_options)
@layout_content = (@page.layout)? @layout.send( "content_#{I18n.locale}" ) : "{{page_content}}" @layout_content = (@page.layout)? @layout.content : "{{page_content}}"
render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content )
else else
render :text => '404 Not Found' render :text => '404 Not Found'

View File

@ -2,21 +2,11 @@ class Announcement
include MongoMapper::Document include MongoMapper::Document
key :title_en, String key_i18n :title, String
key :title_zh_tw, String key_i18n :content, String
key :content_en, String
key :content_zh_tw, String
def to_liquid def to_liquid
{ "id" => self.id, "title" => self.send("title_#{I18n.locale}"), "content" => self.send("content_#{I18n.locale}") } { "id" => self.id, "title" => self.title, "content" => self.content }
end
def title
self.send("title_#{I18n.locale}")
end
def content
self.send("content_#{I18n.locale}")
end end
end end

View File

@ -3,8 +3,7 @@ class Layout
include MongoMapper::Document include MongoMapper::Document
key :name, String key :name, String
key :content_en, String key_i18n :content, String
key :content_zh_tw, String
validates_presence_of :name validates_presence_of :name

View File

@ -4,8 +4,7 @@ class Page
key :name, String key :name, String
key :parent_page_id, String key :parent_page_id, String
key :content_en, String key_i18n :content, String
key :content_zh_tw, String
key :layout_id, String key :layout_id, String
key :layout_name, String key :layout_name, String

View File

@ -3,8 +3,7 @@ class Snippet
include MongoMapper::Document include MongoMapper::Document
key :name, String key :name, String
key :content_en, String key_i18n :content, String
key :content_zh_tw, String
validates_presence_of :name validates_presence_of :name

View File

@ -4,7 +4,7 @@ module SnippetFilter
snippet = Snippet.find_by_name(snippet_name) snippet = Snippet.find_by_name(snippet_name)
if snippet if snippet
return Liquid::Template.parse( snippet.send( "content_#{I18n.locale}" ) ).render return Liquid::Template.parse( snippet.content ).render
else else
return "nothing" return "nothing"
end end

View File

@ -50,18 +50,18 @@ VALID_LOCALES = ["en", "zh_tw"]
MongoMapper.database = "r4-#{Rails.env}" MongoMapper.database = "r4-#{Rails.env}"
#class CouchFoo::Base module MongoMapper::Document::ClassMethods
#
# def self.property_i18n(property_name, property_type) def key_i18n(key, *options)
# VALID_LOCALES.each do |locale| VALID_LOCALES.each do |locale|
# property "#{property_name.to_s}_#{locale}".to_sym, property_type key "#{key.to_s}_#{locale}".to_sym, *options
# end end
#
# define_method( property_name ) do define_method(key) do
# self.read_attribute("#{property_name.to_s}_#{I18n.locale}") self.send("#{key.to_s}_#{I18n.locale}")
# end end
# end end
#
#end end