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 if @page
@layout = @page.layout @layout = @page.layout
@page_options ||= {} @page_options ||= {}
@page_content = Liquid::Template.parse( @page.read_attribute( "content_#{I18n.locale}" ) ).render(@page_options) @page_content = Liquid::Template.parse( @page.send( "content_#{I18n.locale}" ) ).render(@page_options)
@layout_content = (@page.layout)? @layout.read_attribute( "content_#{I18n.locale}" ) : "{{page_content}}" @layout_content = (@page.layout)? @layout.send( "content_#{I18n.locale}" ) : "{{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

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

View File

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

View File

@ -1,16 +1,18 @@
class Page < CouchFoo::Base class Page
include MongoMapper::Document
property :name, String key :name, String
property :parent_page_id, 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 key :layout_id, String
property :use_engine, String key :layout_name, String
property :external_link, String key :use_engine, String
property :position, Integer key :external_link, String
property :is_published, Boolean key :position, Integer
key :is_published, Boolean
belongs_to :layout belongs_to :layout
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id' has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
@ -20,9 +22,7 @@ class Page < CouchFoo::Base
before_save :setup_layout_id before_save :setup_layout_id
before_validation :setup_default_value before_validation :setup_default_value
default_sort :position
def self.find_by_name(page_name) def self.find_by_name(page_name)
Page.find(:first, :conditions => { :name => page_name, :is_published => true }) Page.find(:first, :conditions => { :name => page_name, :is_published => true })
end end
@ -39,7 +39,7 @@ class Page < CouchFoo::Base
def setup_default_value def setup_default_value
if self.position.blank? 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 self.position = (max_page)? max_page.position.to_i + 1 : 1
end end

View File

@ -1,7 +1,10 @@
class Snippet < CouchFoo::Base class Snippet
property :name, String include MongoMapper::Document
property_i18n :content, String
key :name, String
key :content_en, 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.read_attribute( "content_#{I18n.locale}" ) ).render return Liquid::Template.parse( snippet.send( "content_#{I18n.locale}" ) ).render
else else
return "nothing" return "nothing"
end end

View File

@ -21,6 +21,7 @@ Rails::Initializer.run do |config|
# config.gem "aws-s3", :lib => "aws/s3" # config.gem "aws-s3", :lib => "aws/s3"
config.gem "liquid" config.gem "liquid"
config.gem "mongo_mapper"
# Only load the plugins named here, in the order given (default is alphabetical). # 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 # :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. # 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.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
config.i18n.default_locale = "zh_tw" config.i18n.default_locale = "en"
end end
#VALID_LOCALES = ["en", "zh_tw"]
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 MongoMapper.database = "r4-#{Rails.env}"
def self.property_i18n(property_name, property_type)
VALID_LOCALES.each do |locale| #class CouchFoo::Base
property "#{property_name.to_s}_#{locale}".to_sym, property_type #
end # def self.property_i18n(property_name, property_type)
# VALID_LOCALES.each do |locale|
define_method( property_name ) do # property "#{property_name.to_s}_#{locale}".to_sym, property_type
self.read_attribute("#{property_name.to_s}_#{I18n.locale}") # end
end #
end # define_method( property_name ) do
# self.read_attribute("#{property_name.to_s}_#{I18n.locale}")
end # end
# end
#
#end

0
tmp/.gitignore vendored Normal file
View File