diff --git a/app/assets/javascripts/angularjs.js.coffee b/app/assets/javascripts/angularjs.js.coffee index 24d09f6..bbf50dd 100644 --- a/app/assets/javascripts/angularjs.js.coffee +++ b/app/assets/javascripts/angularjs.js.coffee @@ -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') diff --git a/app/assets/javascripts/angularjs/comments.js.coffee b/app/assets/javascripts/angularjs/comments.js.coffee index 776cbab..7992838 100644 --- a/app/assets/javascripts/angularjs/comments.js.coffee +++ b/app/assets/javascripts/angularjs/comments.js.coffee @@ -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)-> diff --git a/app/assets/javascripts/angularjs/likes.js.coffee b/app/assets/javascripts/angularjs/likes.js.coffee new file mode 100644 index 0000000..e62de7c --- /dev/null +++ b/app/assets/javascripts/angularjs/likes.js.coffee @@ -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() diff --git a/app/assets/stylesheets/foundation_and_overrides.scss b/app/assets/stylesheets/foundation_and_overrides.scss index 415338b..783ee85 100644 --- a/app/assets/stylesheets/foundation_and_overrides.scss +++ b/app/assets/stylesheets/foundation_and_overrides.scss @@ -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; } } diff --git a/app/assets/stylesheets/like_and_weixin.css.scss b/app/assets/stylesheets/like_and_weixin.css.scss index e69de29..96270fc 100644 --- a/app/assets/stylesheets/like_and_weixin.css.scss +++ b/app/assets/stylesheets/like_and_weixin.css.scss @@ -0,0 +1,5 @@ +.like-button { + span { + margin-left: 0.23rem; + } +} diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 0000000..c44d80a --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..ae4c32a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,5 @@ module ApplicationHelper + def format_date(time) + time.strftime("%Y-%m-%d") + end end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..70150fb --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,6 @@ +class Like + include Mongoid::Document + + belongs_to :post + validates_presence_of :post_id +end diff --git a/app/models/post.rb b/app/models/post.rb index 7d7af65..dccbc22 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,6 +15,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 } diff --git a/app/views/blogs/_comment.html.slim b/app/views/blogs/_comment.html.slim index c4e0159..b94b183 100644 --- a/app/views/blogs/_comment.html.slim +++ b/app/views/blogs/_comment.html.slim @@ -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 diff --git a/app/views/blogs/_post.html.slim b/app/views/blogs/_post.html.slim index cd7b424..4031471 100644 --- a/app/views/blogs/_post.html.slim +++ b/app/views/blogs/_post.html.slim @@ -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 diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index c4b2178..f850a9d 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -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 diff --git a/config/routes.rb b/config/routes.rb index c213f76..5772c96 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb new file mode 100644 index 0000000..b1e01aa --- /dev/null +++ b/spec/controllers/likes_controller_spec.rb @@ -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 diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb new file mode 100644 index 0000000..ae062eb --- /dev/null +++ b/spec/models/like_spec.rb @@ -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