Change to MongoDB

This commit is contained in:
Wen-Tien Chang 2010-01-04 15:52:30 +08:00
parent fd8f9cace5
commit b17ebfb9b7
8 changed files with 56 additions and 45 deletions

View File

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

View File

@ -1,8 +1,10 @@
require 'couch_foo'
class Announcement < CouchFoo::Base
class Announcement
property_i18n :title, String
property_i18n :content, String
include MongoMapper::Document
key :title_en, String
key :content_en, String
key :content_zh_tw, String
def to_liquid
{ "id" => self.id, "title" => self.title, "content" => self.content }

View File

@ -1,7 +1,10 @@
class Layout < CouchFoo::Base
class Layout
property :name, String
property_i18n :content, String
include MongoMapper::Document
key :name, String
key :content_en, String
key :content_zh_tw, String
validates_presence_of :name

View File

@ -1,16 +1,18 @@
class Page < CouchFoo::Base
class Page
include MongoMapper::Document
property :name, String
property :parent_page_id, String
key :name, String
key :parent_page_id, String
property_i18n :content, String
key :content_en, String
key :content_zh_tw, String
property :layout_id, String
property :layout_name, String
property :use_engine, String
property :external_link, String
property :position, Integer
property :is_published, Boolean
key :layout_id, String
key :layout_name, String
key :use_engine, String
key :external_link, String
key :position, Integer
key :is_published, Boolean
belongs_to :layout
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
@ -21,8 +23,6 @@ class Page < CouchFoo::Base
before_save :setup_layout_id
before_validation :setup_default_value
default_sort :position
def self.find_by_name(page_name)
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
end
@ -39,7 +39,7 @@ class Page < CouchFoo::Base
def setup_default_value
if self.position.blank?
max_page = Page.last( :use_key => 'position')
max_page = Page.find(:last, :order => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
end

View File

@ -1,7 +1,10 @@
class Snippet < CouchFoo::Base
class Snippet
property :name, String
property_i18n :content, String
include MongoMapper::Document
key :name, String
key :content_en, String
key :content_zh_tw, String
validates_presence_of :name

View File

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

View File

@ -21,6 +21,7 @@ Rails::Initializer.run do |config|
# config.gem "aws-s3", :lib => "aws/s3"
config.gem "liquid"
config.gem "mongo_mapper"
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
@ -39,26 +40,28 @@ Rails::Initializer.run do |config|
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
config.i18n.default_locale = "zh_tw"
config.i18n.default_locale = "en"
end
#VALID_LOCALES = ["en", "zh_tw"]
VALID_LOCALES = ["en", "zh_tw"]
require 'couch_foo'
CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "r4")
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
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

0
tmp/.gitignore vendored Normal file
View File