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
#= require angular-cookies
#= require angular-resource #= require angular-resource
#= require_self #= require_self
#= require_tree ./angularjs #= require_tree ./angularjs
@app = angular.module('app', []) @app = angular.module('app', ['ngCookies'])
@app.config(["$httpProvider", (provider) -> @app.config(["$httpProvider", (provider) ->
provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content') 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" url = $location.absUrl() + "/comments"
$http.get(url).success (data)-> $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';
@import 'foundation-icons'; @import 'foundation-icons';
#like-button { .like-button {
color: #eaa296; color: #eaa296 !important;
background-color: transparent; background-color: transparent !important;
border: 1px solid #e79385; border: 1px solid #e79385 !important;
border-radius: 10rem; border-radius: 10rem;
&:hover { &:hover {
background-color: #FCFF9E; background-color: #FCFF9E !important;
} }
&:focus { &: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 module ApplicationHelper
def format_date(time)
time.strftime("%Y-%m-%d")
end
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_many :comments
has_and_belongs_to_many :labels has_and_belongs_to_many :labels
has_many :likes
validates :title, :presence=>true, :uniqueness=> true validates :title, :presence=>true, :uniqueness=> true
validates :content, :presence=>true, :length => { :minimum=> 30 } validates :content, :presence=>true, :length => { :minimum=> 30 }
validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] } 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 .small-12.large-8.columns
form novalidate='' name='form' form novalidate='' name='form'
.row .row

View File

@ -1,9 +1,9 @@
h2.blog-title hello h2.blog-title #{post.title}
p.ptag p.ptag
| 分类: i.fi-list
span 技术 span 技术
p.ptag p.ptag
| 标签: i.fi-pricetag-multiple
span 生活, 感悟 span 生活, 感悟
.content.markdown .content.markdown
@ -11,6 +11,11 @@ p.ptag
p.ptag p.ptag
| 发表于: | 发表于:
span 2014-2-14 span #{format_date(post.created_at)}
p p ng-controller="LikesController"
button#like-button Like 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 = link_to about_path do
i.fi-torso i.fi-torso
| 关于我 | 关于我
section.main-section section.main-section ng-app="app"
= yield = yield
= render "layouts/footer" = render "layouts/footer"
a.exit-off-canvas a.exit-off-canvas

View File

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