Add reroute middleware hack
This commit is contained in:
parent
9f1ba2ae66
commit
314f6d5219
|
@ -1,21 +1,24 @@
|
||||||
class Admin::AnnouncementsController < ApplicationController
|
class Admin::AnnouncementsController < ApplicationController
|
||||||
|
|
||||||
|
before_filter :require_entry_name, :only => [:index, :new]
|
||||||
|
|
||||||
layout "admin"
|
layout "admin"
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@announcements = Announcement.all
|
@announcements = Announcement.find(:all, :conditions => { :entry_name => params[:entry_name] })
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html # index.html.erb
|
format.html
|
||||||
format.xml { render :xml => @announcements }
|
format.xml { render :xml => @announcements }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@announcement = Announcement.new
|
@announcement = Announcement.new
|
||||||
|
@announcement.entry_name = params[:entry_name]
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html # new.html.erb
|
format.htm
|
||||||
format.xml { render :xml => @announcement }
|
format.xml { render :xml => @announcement }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class AnnouncementsController < ApplicationController
|
class AnnouncementsController < ApplicationController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@announcements = Announcement.all
|
@announcements = Announcement.find(:all, :conditions => { :entry_name => params[:entry_name] })
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html {
|
format.html {
|
||||||
|
|
|
@ -31,4 +31,9 @@ class ApplicationController < ActionController::Base
|
||||||
I18n.locale = session[:locale] || I18n.default_locale
|
I18n.locale = session[:locale] || I18n.default_locale
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def require_entry_name
|
||||||
|
render :text => 'missing entry_name' if params[:entry_name].blank?
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,10 +13,7 @@ class PagesController < ApplicationController
|
||||||
@page = Page.find_by_name(params[:page_name])
|
@page = Page.find_by_name(params[:page_name])
|
||||||
|
|
||||||
if @page && !@page.external_link.blank?
|
if @page && !@page.external_link.blank?
|
||||||
# redirect_to @page.external_link
|
redirect_to @page.external_link
|
||||||
elsif @page && !@page.use_engine.blank?
|
|
||||||
#model_class = Kernel.const_get( "Announcement" ) # page.use_engine
|
|
||||||
redirect_to announcements_path
|
|
||||||
else
|
else
|
||||||
render_liquid_page
|
render_liquid_page
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,11 +2,12 @@ class Announcement
|
||||||
|
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
|
|
||||||
|
key :entry_name, String, :required => true, :index => true
|
||||||
key_i18n :title, String
|
key_i18n :title, String
|
||||||
key_i18n :content, String
|
key_i18n :content, String
|
||||||
|
|
||||||
def to_liquid
|
def to_liquid
|
||||||
{ "id" => self.id, "title" => self.title, "content" => self.content }
|
{ "entry_name" => self.entry_name, "id" => self.id.to_s, "title" => self.title, "content" => self.content }
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
|
@ -2,9 +2,7 @@ class Layout
|
||||||
|
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
|
|
||||||
key :name, String
|
key :name, String, :required => true, :index => true
|
||||||
key_i18n :content, String
|
key_i18n :content, String
|
||||||
|
|
||||||
validates_presence_of :name
|
|
||||||
|
|
||||||
end
|
end
|
|
@ -1,42 +1,30 @@
|
||||||
class Page
|
class Page
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
|
|
||||||
key :name, String
|
key :name, String, :required => true, :index => true
|
||||||
key :parent_page_name, String
|
key :parent_page_name, String, :required => true, :index => true
|
||||||
|
|
||||||
key_i18n :title, String
|
key_i18n :title, String, :required => true
|
||||||
key_i18n :content, String
|
key_i18n :content, String
|
||||||
|
|
||||||
key :layout_id, String
|
key :layout_name, String, :required => true
|
||||||
key :layout_name, String
|
|
||||||
key :external_link, String
|
key :external_link, String
|
||||||
key :position, Integer
|
key :position, Integer, :required => true
|
||||||
key :is_published, Boolean
|
key :is_published, Boolean, :required => true, :default => true, :index => true
|
||||||
|
|
||||||
key :is_component, Boolean
|
|
||||||
key :component_name, String
|
key :component_name, String
|
||||||
|
|
||||||
belongs_to :layout
|
|
||||||
|
|
||||||
validates_presence_of :name
|
|
||||||
validates_presence_of :position
|
|
||||||
|
|
||||||
before_save :setup_layout_id
|
|
||||||
before_validation :setup_default_value
|
before_validation :setup_default_value
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
protected
|
def layout
|
||||||
|
Layout.find_by_name(self.layout_name)
|
||||||
|
end
|
||||||
|
|
||||||
def setup_layout_id
|
protected
|
||||||
if self.layout_name.blank?
|
|
||||||
self.layout_id = nil
|
|
||||||
else
|
|
||||||
self.layout_id = Layout.find_by_name( self.layout_name ).id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup_default_value
|
def setup_default_value
|
||||||
if self.position.blank?
|
if self.position.blank?
|
||||||
|
|
|
@ -2,9 +2,7 @@ class Snippet
|
||||||
|
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
|
|
||||||
key :name, String
|
key :name, String, :required => true, :index => true
|
||||||
key_i18n :content, String
|
key_i18n :content, String
|
||||||
|
|
||||||
validates_presence_of :name
|
|
||||||
|
|
||||||
end
|
end
|
|
@ -1,3 +1,5 @@
|
||||||
|
<%= f.hidden_field :entry_name %>
|
||||||
|
|
||||||
<p id="title_zh_tw_block">
|
<p id="title_zh_tw_block">
|
||||||
<%= f.label "title_zh_tw", "Title (zh_tw)" %>
|
<%= f.label "title_zh_tw", "Title (zh_tw)" %>
|
||||||
<%= f.text_field "title_zh_tw" %>
|
<%= f.text_field "title_zh_tw" %>
|
||||||
|
|
|
@ -18,4 +18,4 @@
|
||||||
|
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<%= link_to 'New announcement', new_admin_announcement_path %>
|
<%= link_to 'New announcement', new_admin_announcement_path( :entry_name => params[:entry_name] ) %>
|
|
@ -42,8 +42,8 @@
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :is_component, "Is component" %>
|
<%= f.label :component_name, "Component Name" %>
|
||||||
<%= f.radio_button :is_component, true %>Yes <%= f.radio_button :is_component, false %> No
|
<%= f.text_field :component_name %>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
<li><%= link_to t(:page, :scope => :admin), admin_pages_path %></li>
|
<li><%= link_to t(:page, :scope => :admin), admin_pages_path %></li>
|
||||||
<li><%= link_to t(:snippet, :scope => :admin), admin_snippets_path %></li>
|
<li><%= link_to t(:snippet, :scope => :admin), admin_snippets_path %></li>
|
||||||
<li><%= link_to t(:layout, :scope => :admin), admin_layouts_path %></li>
|
<li><%= link_to t(:layout, :scope => :admin), admin_layouts_path %></li>
|
||||||
<li><%= link_to t(:announcement, :scope => :admin), admin_announcements_path %></li>
|
<li><%= link_to t(:announcement, :scope => :admin), admin_announcements_path( :entry_name => 'news' ) %></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,8 @@ Rails::Initializer.run do |config|
|
||||||
# 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 = "zh_tw"
|
||||||
|
|
||||||
|
config.middleware.use :RerouteMiddleware
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#VALID_LOCALES = ["en", "zh_tw"]
|
#VALID_LOCALES = ["en", "zh_tw"]
|
||||||
|
@ -49,7 +51,6 @@ VALID_LOCALES = ["en", "zh_tw"]
|
||||||
|
|
||||||
MongoMapper.database = "r4-#{Rails.env}"
|
MongoMapper.database = "r4-#{Rails.env}"
|
||||||
|
|
||||||
|
|
||||||
module MongoMapper::Document::ClassMethods
|
module MongoMapper::Document::ClassMethods
|
||||||
|
|
||||||
def key_i18n(key, *options)
|
def key_i18n(key, *options)
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
class RerouteMiddleware
|
||||||
|
|
||||||
|
def initialize(app)
|
||||||
|
@app = app
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
|
||||||
|
#Rails.logger.debug env.to_yaml
|
||||||
|
return @app.call(env) if env['REQUEST_URI'] =~ /^\/admin/
|
||||||
|
|
||||||
|
env['REQUEST_URI'] =~ /^\/([\w]*)/
|
||||||
|
parsed_entry_name = $1
|
||||||
|
|
||||||
|
entry = Page.find_by_name( parsed_entry_name )
|
||||||
|
|
||||||
|
if entry && entry.component_name
|
||||||
|
env['REQUEST_URI'] = env['REQUEST_URI'].sub!(parsed_entry_name, entry.component_name)
|
||||||
|
env['QUERY_STRING'] = (env['QUERY_STRING'].blank?)? "entry_name=#{parsed_entry_name}" : "entry_name=#{parsed_entry_name}&#{env['QUERY_STRING']}"
|
||||||
|
end
|
||||||
|
|
||||||
|
@app.call(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Reference in New Issue