From 314f6d52191fc35de02d3800782d4c4aa40c3939 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 11 Jan 2010 17:09:50 +0800 Subject: [PATCH] Add reroute middleware hack --- .../admin/announcements_controller.rb | 13 ++++--- app/controllers/announcements_controller.rb | 2 +- app/controllers/application_controller.rb | 7 +++- app/controllers/pages_controller.rb | 7 ++-- app/models/announcement.rb | 3 +- app/models/layout.rb | 4 +-- app/models/page.rb | 34 ++++++------------- app/models/snippet.rb | 4 +-- app/views/admin/announcements/_form.html.erb | 2 ++ app/views/admin/announcements/index.html.erb | 2 +- app/views/admin/pages/_form.html.erb | 4 +-- app/views/layouts/admin.html.erb | 2 +- config/environment.rb | 3 +- lib/reroute_middleware.rb | 25 ++++++++++++++ 14 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 lib/reroute_middleware.rb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index cb57e653..b29f47a2 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -1,21 +1,24 @@ class Admin::AnnouncementsController < ApplicationController + before_filter :require_entry_name, :only => [:index, :new] + layout "admin" def index - @announcements = Announcement.all + @announcements = Announcement.find(:all, :conditions => { :entry_name => params[:entry_name] }) respond_to do |format| - format.html # index.html.erb + format.html format.xml { render :xml => @announcements } end end def new @announcement = Announcement.new - + @announcement.entry_name = params[:entry_name] + respond_to do |format| - format.html # new.html.erb + format.htm format.xml { render :xml => @announcement } end end @@ -26,7 +29,7 @@ class Admin::AnnouncementsController < ApplicationController def create @announcement = Announcement.new(params[:announcement]) - + respond_to do |format| if @announcement.save flash[:notice] = 'Announcement was successfully created.' diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index ab09b121..e232732b 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,7 +1,7 @@ class AnnouncementsController < ApplicationController def index - @announcements = Announcement.all + @announcements = Announcement.find(:all, :conditions => { :entry_name => params[:entry_name] }) respond_to do |format| format.html { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d2bfd123..34867abd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -30,5 +30,10 @@ class ApplicationController < ActionController::Base # set locale based on session or default I18n.locale = session[:locale] || I18n.default_locale end - + + def require_entry_name + render :text => 'missing entry_name' if params[:entry_name].blank? + return + end + end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 2877225e..542deaec 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -9,14 +9,11 @@ class PagesController < ApplicationController end end - def show + def show @page = Page.find_by_name(params[:page_name]) if @page && !@page.external_link.blank? - # redirect_to @page.external_link - elsif @page && !@page.use_engine.blank? - #model_class = Kernel.const_get( "Announcement" ) # page.use_engine - redirect_to announcements_path + redirect_to @page.external_link else render_liquid_page end diff --git a/app/models/announcement.rb b/app/models/announcement.rb index fc8e1fb5..48808246 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -2,11 +2,12 @@ class Announcement include MongoMapper::Document + key :entry_name, String, :required => true, :index => true key_i18n :title, String key_i18n :content, String 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 \ No newline at end of file diff --git a/app/models/layout.rb b/app/models/layout.rb index 2c65d556..b261e88b 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -2,9 +2,7 @@ class Layout include MongoMapper::Document - key :name, String + key :name, String, :required => true, :index => true key_i18n :content, String - validates_presence_of :name - end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index 5210eadb..eb01a554 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,43 +1,31 @@ class Page include MongoMapper::Document - key :name, String - key :parent_page_name, String + key :name, String, :required => true, :index => true + key :parent_page_name, String, :required => true, :index => true - key_i18n :title, String + key_i18n :title, String, :required => true key_i18n :content, String - key :layout_id, String - key :layout_name, String + key :layout_name, String, :required => true key :external_link, String - key :position, Integer - key :is_published, Boolean + key :position, Integer, :required => true + key :is_published, Boolean, :required => true, :default => true, :index => true - key :is_component, Boolean 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 def self.find_by_name(page_name) Page.find(:first, :conditions => { :name => page_name, :is_published => true }) end - protected - - def setup_layout_id - if self.layout_name.blank? - self.layout_id = nil - else - self.layout_id = Layout.find_by_name( self.layout_name ).id - end + def layout + Layout.find_by_name(self.layout_name) end - + + protected + def setup_default_value if self.position.blank? max_page = Page.find(:last, :order => 'position') diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 6b4e5cea..68b3c648 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -2,9 +2,7 @@ class Snippet include MongoMapper::Document - key :name, String + key :name, String, :required => true, :index => true key_i18n :content, String - - validates_presence_of :name end \ No newline at end of file diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 281fe876..ace3e853 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -1,3 +1,5 @@ +<%= f.hidden_field :entry_name %> +

<%= f.label "title_zh_tw", "Title (zh_tw)" %> <%= f.text_field "title_zh_tw" %> diff --git a/app/views/admin/announcements/index.html.erb b/app/views/admin/announcements/index.html.erb index 22b50926..6342b10b 100644 --- a/app/views/admin/announcements/index.html.erb +++ b/app/views/admin/announcements/index.html.erb @@ -18,4 +18,4 @@
-<%= link_to 'New announcement', new_admin_announcement_path %> \ No newline at end of file +<%= link_to 'New announcement', new_admin_announcement_path( :entry_name => params[:entry_name] ) %> \ No newline at end of file diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index d89e8dc6..b02b387b 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -42,8 +42,8 @@

- <%= f.label :is_component, "Is component" %> -<%= f.radio_button :is_component, true %>Yes <%= f.radio_button :is_component, false %> No +<%= f.label :component_name, "Component Name" %> +<%= f.text_field :component_name %>

diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index e91b2dc7..b134dcda 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -23,7 +23,7 @@

  • <%= link_to t(:page, :scope => :admin), admin_pages_path %>
  • <%= link_to t(:snippet, :scope => :admin), admin_snippets_path %>
  • <%= link_to t(:layout, :scope => :admin), admin_layouts_path %>
  • -
  • <%= link_to t(:announcement, :scope => :admin), admin_announcements_path %>
  • +
  • <%= link_to t(:announcement, :scope => :admin), admin_announcements_path( :entry_name => 'news' ) %>
  • diff --git a/config/environment.rb b/config/environment.rb index 1f165efd..97516e62 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -42,6 +42,8 @@ Rails::Initializer.run do |config| # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] config.i18n.default_locale = "zh_tw" + config.middleware.use :RerouteMiddleware + end #VALID_LOCALES = ["en", "zh_tw"] @@ -49,7 +51,6 @@ VALID_LOCALES = ["en", "zh_tw"] MongoMapper.database = "r4-#{Rails.env}" - module MongoMapper::Document::ClassMethods def key_i18n(key, *options) diff --git a/lib/reroute_middleware.rb b/lib/reroute_middleware.rb new file mode 100644 index 00000000..88fe6a3d --- /dev/null +++ b/lib/reroute_middleware.rb @@ -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 \ No newline at end of file