Merge branch 'like_feature'

This commit is contained in:
yafeilee 2014-03-31 00:17:18 +08:00
commit 2e8cba59e2
15 changed files with 135 additions and 16 deletions

View File

@ -1,9 +1,10 @@
#= require angular
#= require angular-cookies
#= require angular-resource
#= require_self
#= require_tree ./angularjs
@app = angular.module('app', [])
@app = angular.module('app', ['ngCookies'])
@app.config(["$httpProvider", (provider) ->
provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')

View File

@ -1,4 +1,4 @@
@app.controller 'CommentsController', ($scope, $http, $location, $timeout)->
@app.controller 'CommentsController', ($scope, $http, $location)->
url = $location.absUrl() + "/comments"
$http.get(url).success (data)->

View File

@ -0,0 +1,27 @@
@app.controller 'LikesController', ($scope, $http, $location, $cookies)->
url = $location.absUrl() + "/likes"
$scope.count = 0
$scope.get_count = ->
$http.get url
.success (res)->
$scope.count = res.count
$scope.get_count()
$scope.like = $cookies.like
$scope.submit = ->
$http.post url
.success (res)->
if res.success
$scope.count += 1
$scope.like = $cookies.like = res.id
$scope.cancel = ->
$http.delete url + "/" + $scope.like
# anyway, clear cookie
delete $cookies["like"]
$scope.like = null
$scope.get_count()

View File

@ -1191,15 +1191,15 @@
@import 'foundation';
@import 'foundation-icons';
#like-button {
color: #eaa296;
background-color: transparent;
border: 1px solid #e79385;
.like-button {
color: #eaa296 !important;
background-color: transparent !important;
border: 1px solid #e79385 !important;
border-radius: 10rem;
&:hover {
background-color: #FCFF9E;
background-color: #FCFF9E !important;
}
&:focus {
outline-style: none;
outline-style: none !important;
}
}

View File

@ -0,0 +1,5 @@
.like-button {
span {
margin-left: 0.23rem;
}
}

View File

@ -0,0 +1,29 @@
class LikesController < ApplicationController
layout false
def index
post = Post.find( params[:blog_id] )
render :json=> { success: true, count: post.likes.size }
end
def create
post = Post.find( params[:blog_id] )
like = Like.new
like.post = post
if like.save
render :json=> { success: true, id: like.id.to_s }
else
render :json=> { success: false }
end
end
def destroy
post = Post.find( params[:blog_id] )
like = post.likes.find(params[:id])
if like.destroy
render :json=> { success: true }
else
render :json=> { success: false }
end
end
end

View File

@ -1,2 +1,5 @@
module ApplicationHelper
def format_date(time)
time.strftime("%Y-%m-%d")
end
end

6
app/models/like.rb Normal file
View File

@ -0,0 +1,6 @@
class Like
include Mongoid::Document
belongs_to :post
validates_presence_of :post_id
end

View File

@ -16,6 +16,8 @@ class Post
has_many :comments
has_and_belongs_to_many :labels
has_many :likes
validates :title, :presence=>true, :uniqueness=> true
validates :content, :presence=>true, :length => { :minimum=> 30 }
validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] }

View File

@ -1,4 +1,4 @@
.row ng-app="app" ng-controller="CommentsController"
.row ng-controller="CommentsController"
.small-12.large-8.columns
form novalidate='' name='form'
.row

View File

@ -1,9 +1,9 @@
h2.blog-title hello
h2.blog-title #{post.title}
p.ptag
| 分类:
i.fi-list
span 技术
p.ptag
| 标签:
i.fi-pricetag-multiple
span 生活, 感悟
.content.markdown
@ -11,6 +11,11 @@ p.ptag
p.ptag
| 发表于:
span 2014-2-14
p
button#like-button Like
span #{format_date(post.created_at)}
p ng-controller="LikesController"
button.like-button ng-show="! like " ng-click="submit()"
|{{ count }}
span Like
button.like-button ng-show=" like " ng-click="cancel()"
|{{ count }}
span Liked

View File

@ -51,7 +51,7 @@ html
= link_to about_path do
i.fi-torso
| 关于我
section.main-section
section.main-section ng-app="app"
= yield
= render "layouts/footer"
a.exit-off-canvas

View File

@ -1,10 +1,12 @@
WBlog::Application.routes.draw do
root :to => 'blogs#index'
resources :blogs, :only=>[:index, :show] do
collection do
get :rss
end
resources :comments, only: [:index, :create]
resources :likes, only: [:index, :create, :destroy]
end

View File

@ -0,0 +1,29 @@
require 'spec_helper'
describe LikesController do
it "get index" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH )
get 'index', blog_id: a.id
JSON.parse(response.body)['count'].should == 0
a.likes << Like.new
a.save!
get 'index', blog_id: a.id
JSON.parse(response.body)['count'].should == 1
end
it "post create" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH )
post 'create', blog_id: a.id
a.likes.size.should == 1
end
it "DELETE destroy" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH )
like = Like.new
a.likes << like
a.save!
delete 'destroy', blog_id: a.id, id: like.id
a.reload.likes.size.should == 0
end
end

10
spec/models/like_spec.rb Normal file
View File

@ -0,0 +1,10 @@
require 'spec_helper'
describe Like do
it "add like" do
a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH )
like = Like.new
like.post = a
expect(like.save).to eq(true)
end
end