From b17ebfb9b7c5733ec567b4f7d2086349c6bcde9d Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 4 Jan 2010 15:52:30 +0800 Subject: [PATCH] Change to MongoDB --- app/controllers/application_controller.rb | 4 +-- app/models/announcement.rb | 10 ++++--- app/models/layout.rb | 11 ++++--- app/models/page.rb | 30 +++++++++---------- app/models/snippet.rb | 9 ++++-- app/models/snippet_filter.rb | 2 +- config/environment.rb | 35 ++++++++++++----------- tmp/.gitignore | 0 8 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 tmp/.gitignore diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5fb975d76..c5a4bacf6 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.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' diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 474ccc051..3bf1ffb3f 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -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 } diff --git a/app/models/layout.rb b/app/models/layout.rb index e5408386f..8579b8b5c 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -1,7 +1,10 @@ -class Layout < CouchFoo::Base - - property :name, String - property_i18n :content, String +class Layout + + include MongoMapper::Document + + key :name, String + key :content_en, String + key :content_zh_tw, String validates_presence_of :name diff --git a/app/models/page.rb b/app/models/page.rb index 1b4521a01..d2caa579c 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 - - property :layout_id, String - property :layout_name, String - property :use_engine, String - property :external_link, String - property :position, Integer - property :is_published, Boolean + key :content_en, String + key :content_zh_tw, String + + 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' @@ -20,9 +22,7 @@ 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 diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 4e49667e8..46604d2a0 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -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 diff --git a/app/models/snippet_filter.rb b/app/models/snippet_filter.rb index 18b265d72..afa1b8c6c 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.read_attribute( "content_#{I18n.locale}" ) ).render + return Liquid::Template.parse( snippet.send( "content_#{I18n.locale}" ) ).render else return "nothing" end diff --git a/config/environment.rb b/config/environment.rb index 8a477f580..b6e1eb4d9 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -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 diff --git a/tmp/.gitignore b/tmp/.gitignore new file mode 100644 index 000000000..e69de29bb