Permalink
Browse files

Add Posts feature

  • Loading branch information...
1 parent 07e4889 commit a04ab9462459e8b65f7e047541850382ef5760ca @huacnlee huacnlee committed Oct 27, 2011
Showing with 578 additions and 94 deletions.
  1. +1 −1 Rakefile
  2. +3 −0 app/assets/javascripts/posts.coffee
  3. +33 −0 app/assets/stylesheets/posts.scss
  4. +56 −0 app/assets/stylesheets/scaffolds.css.scss
  5. +90 −0 app/controllers/cpanel/posts_controller.rb
  6. +1 −1 app/controllers/gridfs_controller.rb
  7. +1 −6 app/controllers/home_controller.rb
  8. +42 −0 app/controllers/posts_controller.rb
  9. +14 −0 app/helpers/posts_helper.rb
  10. +53 −0 app/models/post.rb
  11. +1 −0 app/models/user.rb
  12. BIN app/views/.DS_Store
  13. +52 −0 app/views/cpanel/posts/_form.html.erb
  14. +8 −0 app/views/cpanel/posts/edit.html.erb
  15. +28 −0 app/views/cpanel/posts/index.html.erb
  16. +6 −0 app/views/cpanel/posts/new.html.erb
  17. +1 −0 app/views/home/index.html.erb
  18. +6 −3 app/views/layouts/application.html.erb
  19. +2 −1 app/views/layouts/cpanel.html.erb
  20. +9 −0 app/views/posts/_base.html.erb
  21. +41 −0 app/views/posts/_form.html.erb
  22. +2 −0 app/views/posts/edit.html.erb
  23. +27 −0 app/views/posts/index.html.erb
  24. +2 −0 app/views/posts/new.html.erb
  25. +34 −0 app/views/posts/show.html.erb
  26. +1 −1 config.ru
  27. +1 −1 config/application.rb
  28. +10 −29 config/config.yml.default
  29. +1 −1 config/environment.rb
  30. +1 −1 config/environments/development.rb
  31. +1 −1 config/environments/production.rb
  32. +1 −1 config/environments/test.rb
  33. +1 −1 config/initializers/redis.rb
  34. +1 −1 config/initializers/secret_token.rb
  35. +2 −2 config/initializers/session_store.rb
  36. +8 −1 config/locales/zh-CN.yml
  37. +3 −3 config/mongoid.yml
  38. +4 −5 config/mongoid.yml.default
  39. +1 −0 config/redis.yml
  40. +1 −0 config/redis.yml.default
  41. +16 −8 config/routes.rb
  42. +12 −26 db/seeds.rb
View
@@ -4,4 +4,4 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
-Homeland::Application.load_tasks
+RubyChina::Application.load_tasks
@@ -0,0 +1,3 @@
+window.Posts =
+ like : () ->
+ console.log "like"
@@ -0,0 +1,33 @@
+#posts {
+ ul {
+ list-style: none;
+ margin:0;
+ padding:0;
+
+ li {
+ margin:0 -10px;
+ border-top:1px solid #ddd;
+ padding:10px 10px;
+ .info {
+ color:#999;
+ }
+ }
+ }
+ .pagination { margin:0; border-top:1px solid #ddd; padding:10px 10px 0 10px; margin:0 -10px;}
+}
+
+#post_show {
+ h1 { font-size:18px; line-height:150%; text-align: left; }
+ .info {
+ margin-bottom:5px;
+ color:#999;
+ font-size:12px;
+ }
+ .source { color:#666; text-align: right;}
+ .body { margin:15px 0; }
+ .tags { border-top:1px solid #ddd; padding-top:10px; }
+}
+
+#post_author {
+ .inner { text-align: center; }
+}
@@ -0,0 +1,56 @@
+body {
+ background-color: #fff;
+ color: #333;
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px; }
+
+a {
+ color: #000;
+ &:visited {
+ color: #666; }
+ &:hover {
+ color: #fff;
+ background-color: #000; } }
+
+div {
+ &.field, &.actions {
+ margin-bottom: 10px; } }
+
+#notice {
+ color: green; }
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table; }
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff; }
+ ul li {
+ font-size: 12px;
+ list-style: square; } }
@@ -0,0 +1,90 @@
+# coding: utf-8
+class Cpanel::PostsController < Cpanel::ApplicationController
+ # GET /posts
+ # GET /posts.xml
+ def index
+ @posts = Post.unscoped.desc(:_id).includes(:user).paginate :page => params[:page], :per_page => 30
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @posts }
+ end
+ end
+
+ # GET /posts/1
+ # GET /posts/1.xml
+ def show
+ @post = Post.unscoped.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @post }
+ end
+ end
+
+ # GET /posts/new
+ # GET /posts/new.xml
+ def new
+ @post = Post.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @post }
+ end
+ end
+
+ # GET /posts/1/edit
+ def edit
+ @post = Post.unscoped.find(params[:id])
+ @post.tag_list = @post.tags.join(", ")
+ end
+
+ # POST /posts
+ # POST /posts.xml
+ def create
+ @post = Post.new(params[:post])
+
+ respond_to do |format|
+ if @post.save
+ format.html { redirect_to(cpanel_posts_path, :notice => 'Post was successfully created.') }
+ format.xml { render :xml => @post, :status => :created, :location => @post }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /posts/1
+ # PUT /posts/1.xml
+ def update
+ @post = Post.unscoped.find(params[:id])
+ @post.title = params[:post][:title]
+ @post.body = params[:post][:body]
+ @post.tag_list = params[:post][:tag_list]
+ @post.user_id = params[:post][:user_id]
+ @post.state = params[:post][:state]
+
+ respond_to do |format|
+ if @post.update_attributes(params[:post])
+ format.html { redirect_to(cpanel_posts_path, :notice => 'Post was successfully updated.') }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /posts/1
+ # DELETE /posts/1.xml
+ def destroy
+ @post = Post.unscoped.find(params[:id])
+ @post.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(cpanel_posts_path) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -1,7 +1,7 @@
require 'mongo'
class GridfsController < ActionController::Metal
def serve
- gridfs_path = env["PATH_INFO"].gsub("/file/", "")
+ gridfs_path = env["PATH_INFO"].gsub("/uploads/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r')
self.response_body = gridfs_file.read
@@ -5,12 +5,7 @@ class HomeController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:auth_callback]
def index
- if !fragment_exist? "home/last_topics"
- @last_topics = Topic.recent.limit(10)
- end
- if !fragment_exist? "home/actived_topics"
- @actived_topics = Topic.last_actived.limit(10)
- end
+
end
def auth_callback
@@ -0,0 +1,42 @@
+# coding: utf-8
+class PostsController < ApplicationController
+ before_filter :require_user, :only => [:new, :edit, :create, :update, :destroy]
+ def index
+ @posts = Post.normal.recent.paginate :page => params[:page], :per_page => 2
+ end
+
+ def show
+ @post = Post.find(params[:id])
+ @post.hits.incr
+ end
+
+ def new
+ @post = Post.new
+ end
+
+ def edit
+ @post = Post.find(params[:id])
+ @post.tag_list = @post.tags.join(", ")
+ end
+
+ def create
+ @post = Post.new(params[:post])
+ @post.user_id = current_user.id
+
+ if @post.save
+ redirect_to @post, notice: '投稿成功,需等待审核通过以后才能显示到文章列表。'
+ else
+ render action: "new"
+ end
+ end
+
+ def update
+ @post = current_user.posts.find(params[:id])
+
+ if @post.update_attributes(params[:post])
+ redirect_to @post, notice: '文章更新成功。'
+ else
+ render action: "edit"
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module PostsHelper
+ def post_title_tag(post, opts = {})
+ return "" if post.blank?
+ raw "<a href='#{post_path(post.id)}' title='#{post.title}'>#{post.title}</a>"
+ end
+
+ def post_tags_tag(post, opts = {})
+ return "" if post.blank? or post.tags.blank?
+ limit = 5
+ tags = post.tags
+ tags = tags[0..limit-1] if tags.count > limit
+ raw tags.collect { |tag| "<a href='#{tag_posts_path(tag)}' class='tag'>#{tag}</a>" }.join(", ")
+ end
+end
View
@@ -0,0 +1,53 @@
+# coding: utf-8
+class Post
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Mongoid::SoftDelete
+ include Redis::Search
+ include Redis::Objects
+
+ STATE = {
+ :draft => 0,
+ :normal => 1
+ }
+
+ field :title, :type => String
+ field :body, :type => String
+ field :state, :type => Integer, :default => STATE[:draft]
+ field :tags, :type => Array, :default => []
+ # 来源名称
+ field :source
+ # 来源地址
+ field :source_url
+ belongs_to :user
+
+ counter :hits, :default => 0
+
+ attr_protected :state, :user_id
+ attr_accessor :tag_list
+
+ validates_presence_of :title, :body, :tag_list
+
+ scope :normal, where(:state => STATE[:normal])
+ scope :recent, desc(:_id)
+
+ before_save :split_tags
+ def split_tags
+ if !self.tag_list.blank? and self.tags.blank?
+ self.tags = self.tag_list.split(/,|,/).collect { |tag| tag.strip }.uniq
+ end
+ end
+
+ # 给下拉框用
+ def self.state_collection
+ STATE.collect { |s| [s[0], s[1]]}
+ end
+
+ def state_s
+ case self.state
+ when 0 then "<span class='label important'>草稿</span>"
+ else
+ "<span class='label success'>已审核</span>"
+ end
+ end
+end
View
@@ -23,6 +23,7 @@ class User
has_many :notes
has_many :replies
embeds_many :authorizations
+ has_many :posts
attr_accessor :password_confirmation
attr_protected :verified, :replies_count
View
Binary file not shown.
@@ -0,0 +1,52 @@
+<%= form_for([:cpanel,@post]) do |f| %>
+<fieldset>
+ <legend></legend>
+ <%= render 'shared/error_messages', :target => @post %>
+
+ <div class="clearfix">
+ <%= f.label :title %>
+ <div class="input">
+ <%= f.text_field :title, :class => "span10" %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :body %>
+ <div class="input">
+ <%= f.text_area :body, :class => "span13", :style => "height:400px" %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :user_id %>
+ <div class="input">
+ <%= f.text_field :user_id %> <%= @post.user.name if @post.user %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :tag_list %>
+ <div class="input">
+ <%= f.text_field :tag_list, :class => "xxlarge" %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :source %>
+ <div class="input">
+ <%= f.text_field :source, :class => "span3", :placeholder => "某某博客" %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :source_url %>
+ <div class="input">
+ <%= f.text_field :source_url, :class => "xxlarge", :placeholder => "http://" %>
+ </div>
+ </div>
+ <div class="clearfix">
+ <%= f.label :state %>
+ <div class="input">
+ <%= f.select :state, Post.state_collection %>
+ </div>
+ </div>
+ <div class="actions">
+ <button class="btn primary">保存</button> or
+ <%= link_to 'Back', cpanel_posts_path %>
+ </div>
+<% end %>
@@ -0,0 +1,8 @@
+<% content_for :sitemap do %>
+ <a href="<%= cpanel_posts_path %>">帖子</a> &gt; <span class="current">修改</span>
+<% end %>
+<h1>Editing post</h1>
+
+<%= render 'form' %>
+
+
Oops, something went wrong.

0 comments on commit a04ab94

Please sign in to comment.