From 8d8c9f622690cc5f1cefc587b2caf8e26d23a588 Mon Sep 17 00:00:00 2001 From: yafei Lee Date: Sun, 24 Jun 2012 05:13:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B0=E5=BB=BA=E5=8D=9A?= =?UTF-8?q?=E5=AE=A2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +- Gemfile.lock | 4 +- app/assets/javascripts/admin/posts.js.coffee | 22 +++++++ app/assets/stylesheets/admin/posts.css.scss | 57 +++++++++++++++++++ app/assets/stylesheets/global.css.scss | 5 ++ app/controllers/admin/posts_controller.rb | 15 ++++- app/views/admin/posts/_form.html.erb | 14 ++++- config/routes.rb | 7 ++- .../admin/posts_controller_spec.rb | 53 ++++------------- spec/controllers/blogs_controller_spec.rb | 14 ----- 10 files changed, 130 insertions(+), 64 deletions(-) diff --git a/Gemfile b/Gemfile index 04fdd63..a8303f3 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'jquery-rails' # gem 'capistrano' -gem "bluecloth" +#gem "bluecloth" +gem "redcarpet" gem "simple_form" gem 'database_cleaner' diff --git a/Gemfile.lock b/Gemfile.lock index bd89bb4..4c0fbcf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,7 +29,6 @@ GEM i18n (~> 0.6) multi_json (~> 1.0) arel (3.0.2) - bluecloth (2.2.0) bson (1.6.4) bson_ext (1.6.4) bson (~> 1.6.4) @@ -91,6 +90,7 @@ GEM rake (0.9.2.2) rdoc (3.12) json (~> 1.4) + redcarpet (2.1.1) rspec (2.10.0) rspec-core (~> 2.10.0) rspec-expectations (~> 2.10.0) @@ -130,13 +130,13 @@ PLATFORMS ruby DEPENDENCIES - bluecloth bson_ext coffee-rails (~> 3.2.1) database_cleaner jquery-rails mongoid rails (= 3.2.6) + redcarpet rspec-rails (>= 2.8.1) sass-rails (~> 3.2.3) simple_form diff --git a/app/assets/javascripts/admin/posts.js.coffee b/app/assets/javascripts/admin/posts.js.coffee index 7615679..d63051a 100644 --- a/app/assets/javascripts/admin/posts.js.coffee +++ b/app/assets/javascripts/admin/posts.js.coffee @@ -1,3 +1,25 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ +# + +$(document).ready -> + + preview = $('.preview') + content = $('#post_content') + + $('#content').click -> + preview.hide() + content.show() + $(this).addClass('active') + $('#preview').removeClass('active') + + $('#preview').click -> + content.hide() + $(this).addClass('active') + $('#content').removeClass('active') + preview.html('Loading...') + preview.show() + $.post $(this).attr('url'), text: content.val(), (data)-> + preview.html(data) + diff --git a/app/assets/stylesheets/admin/posts.css.scss b/app/assets/stylesheets/admin/posts.css.scss index 45f43f5..7de724a 100644 --- a/app/assets/stylesheets/admin/posts.css.scss +++ b/app/assets/stylesheets/admin/posts.css.scss @@ -1,3 +1,60 @@ // Place all the styles related to the admin/posts controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +h3 { + font-size: 40px; + margin-bottom: 10px; + padding-bottom: 5px; + border-bottom: 1px solid #CCC; +} + +#post_content { + width: 800px; + height: 390px; +} + +div.submit { + display: block; + line-height: 40px; + width: 805px; + background-color: #CCC; + padding-left: 16px; + font-size: 20px; +} + +ul.tab { + margin: 10px 0 10px 0; + overflow: hidden; + li { + float: left; + padding: 5px 5px; + border: 1px solid #CCC; + cursor: pointer; + } + li.active { + border-bottom: none; + border-top: 1px solid #CCC; + } + li:not(.active) { + border-top: none; + border-top-right-radius: none; + border-top-left-radius: none; + } + li#content { + border-top-left-radius: 10px; + border-right: none; + } + li#preview { + border-right-bottom-radius: 10px; + border-top-right-radius: 10px; + } +} + +div.preview { + width: 800px; + min-height: 396px; + border: 1px solid #CCC; + padding: 5px 5px; +} + diff --git a/app/assets/stylesheets/global.css.scss b/app/assets/stylesheets/global.css.scss index 0f996c3..40efb19 100644 --- a/app/assets/stylesheets/global.css.scss +++ b/app/assets/stylesheets/global.css.scss @@ -2,3 +2,8 @@ width: 1100px; margin: 0 auto; } + +label { + display: block; + margin: 10px 0 5px 0; +} diff --git a/app/controllers/admin/posts_controller.rb b/app/controllers/admin/posts_controller.rb index def9502..65e1143 100644 --- a/app/controllers/admin/posts_controller.rb +++ b/app/controllers/admin/posts_controller.rb @@ -1,5 +1,4 @@ class Admin::PostsController < ApplicationController - layout false def new @post = Post.new end @@ -14,8 +13,22 @@ class Admin::PostsController < ApplicationController end def create + @post = Post.new( params[:post] ) + if @post.save + flash[:notice] = "success!" + redirect_to :action=>:index + else + flash[:alert] = "fail!" + render :action=>:new + end end def update end + + def preview + text = params[:text] || "" + md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink=>true) + render :text => md.render(text) + end end diff --git a/app/views/admin/posts/_form.html.erb b/app/views/admin/posts/_form.html.erb index a40f073..65af69e 100644 --- a/app/views/admin/posts/_form.html.erb +++ b/app/views/admin/posts/_form.html.erb @@ -1,5 +1,13 @@ -<%= simple_form_for(@post) do %> +<%= simple_form_for(@post, :url=> admin_posts_path(@post)) do |f| %> <%= f.input :title %> - <%= f.input :content %> - <%= f.select :type %> + + <%= f.input :content, :as=> :text, :label=>false %> +
+ <%= f.input :type %> +
+ <%= f.submit %> +
<% end %> diff --git a/config/routes.rb b/config/routes.rb index 0178772..27166d3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,11 @@ WBlog::Application.routes.draw do root :to => 'blogs#index' resources :blogs, :only=>[:index, :show] namespace :admin do - resources :posts + resources :posts do + collection do + post :preview + end + end end + match '/admin' => 'admin/posts#new' end diff --git a/spec/controllers/admin/posts_controller_spec.rb b/spec/controllers/admin/posts_controller_spec.rb index 79603c9..74d739d 100644 --- a/spec/controllers/admin/posts_controller_spec.rb +++ b/spec/controllers/admin/posts_controller_spec.rb @@ -1,47 +1,16 @@ require 'spec_helper' describe Admin::PostsController do - - describe "GET 'new'" do - it "returns http success" do - get 'new' - response.should be_success - end + it "preview should return ok" do + post :preview + response.body.should == "" + post :preview, text: '123' + response.body.should == "

123

\n" + post :preview, text: <<-EOF +```ruby +puts 'hello world' +``` +EOF + response.body.should == "

ruby\nputs 'hello world'\n

\n" end - - describe "GET 'edit'" do - it "returns http success" do - get 'edit' - response.should be_success - end - end - - describe "GET 'destroy'" do - it "returns http success" do - get 'destroy' - response.should be_success - end - end - - describe "GET 'index'" do - it "returns http success" do - get 'index' - response.should be_success - end - end - - describe "GET 'create'" do - it "returns http success" do - get 'create' - response.should be_success - end - end - - describe "GET 'update'" do - it "returns http success" do - get 'update' - response.should be_success - end - end - end diff --git a/spec/controllers/blogs_controller_spec.rb b/spec/controllers/blogs_controller_spec.rb index 4199976..d6944c9 100644 --- a/spec/controllers/blogs_controller_spec.rb +++ b/spec/controllers/blogs_controller_spec.rb @@ -2,18 +2,4 @@ require 'spec_helper' describe BlogsController do - describe "GET 'index'" do - it "returns http success" do - get 'index' - response.should be_success - end - end - - describe "GET 'show'" do - it "returns http success" do - get 'show' - response.should be_success - end - end - end