From e1060b93afb07265165c5a92e1f04bb1517b62fd Mon Sep 17 00:00:00 2001 From: yafeilee Date: Thu, 21 Apr 2016 22:11:18 +0800 Subject: [PATCH] Like feature done --- Gemfile | 1 + Gemfile.lock | 5 +++-- app/assets/javascripts/application.js | 1 + app/assets/javascripts/jquery.atwho.js | 11 ++--------- app/assets/javascripts/like.js.coffee | 19 +++++++++++++++++++ app/controllers/archives_controller.rb | 2 +- app/controllers/likes_controller.rb | 9 --------- app/models/post.rb | 4 ++++ app/views/blogs/_post.html.slim | 5 +++-- 9 files changed, 34 insertions(+), 23 deletions(-) mode change 100755 => 100644 app/assets/javascripts/jquery.atwho.js create mode 100644 app/assets/javascripts/like.js.coffee diff --git a/Gemfile b/Gemfile index 45e1fcb..4c497ba 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ gem 'font-awesome-sass' gem 'carrierwave' gem 'kaminari', git: 'git@github.com:amatsuda/kaminari.git' gem 'turbolinks', '~> 5.x' +gem 'js_cookie_rails' gem 'jbuilder' gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index d5255e9..e80c6f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,7 +47,6 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.4.0) - angularjs-rails (1.5.0) arel (7.0.0) babel-source (5.8.35) babel-transpiler (0.7.0) @@ -135,6 +134,8 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + js_cookie_rails (1.0.1) + railties (>= 3.1) json (1.8.3) listen (3.0.6) rb-fsevent (>= 0.9.3) @@ -318,7 +319,6 @@ PLATFORMS ruby DEPENDENCIES - angularjs-rails byebug capybara carrierwave @@ -337,6 +337,7 @@ DEPENDENCIES html_truncator jbuilder jquery-rails + js_cookie_rails kaminari! listen (~> 3.0.5) mina diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 37173a7..69eacbe 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -2,6 +2,7 @@ //= require jquery_ujs //= require turbolinks //= require foundation +//= require js.cookie //= require 'jquery.html5-fileupload' //= require_tree . diff --git a/app/assets/javascripts/jquery.atwho.js b/app/assets/javascripts/jquery.atwho.js old mode 100755 new mode 100644 index 1576122..cb55699 --- a/app/assets/javascripts/jquery.atwho.js +++ b/app/assets/javascripts/jquery.atwho.js @@ -8,13 +8,6 @@ */ -/* -本插件操作 textarea 或者 input 内的插入符 -只实现了获得插入符在文本框中的位置,我设置 -插入符的位置. -*/ - - (function() { (function(factory) { if (typeof define === 'function' && define.amd) { @@ -85,7 +78,7 @@ / \ < I really [[HATE] IE []]> \_endRange end-point. - + " > -1" mean the start end-point will be the same or right to the end end-point * simplelly, all in the end. */ @@ -99,7 +92,7 @@ I really[ [HATE] IE ]> <-[ I reall[y [HATE] IE ]> - + will return how many unit have moved. */ diff --git a/app/assets/javascripts/like.js.coffee b/app/assets/javascripts/like.js.coffee new file mode 100644 index 0000000..0f04445 --- /dev/null +++ b/app/assets/javascripts/like.js.coffee @@ -0,0 +1,19 @@ +$(document).on 'turbolinks:load', -> + + $('.like-button').click -> + if $(this).hasClass('liked') + $.ajax + url: $(this).data('url') + '/' + Cookies.get('like') + type: 'DELETE' + success: (res)=> + $(this).removeClass('liked') + $(this).children('.count').text(res.count) + Cookies.remove('like') + else + $.ajax + url: $(this).data('url') + type: 'POST' + success: (res)=> + $(this).addClass('liked') + $(this).children('.count').text(res.count) + Cookies.set('like', res.id) diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index ebb6e06..059896d 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,5 +1,5 @@ class ArchivesController < ApplicationController def index - @posts = Post.order(created_at: :desc).page(params[:page]).per(3) + @posts = Post.order(created_at: :desc).page(params[:page]) end end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 4e6e9e3..63e38ca 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -6,15 +6,6 @@ class LikesController < ApplicationController render :json=> { success: true, count: post.liked_count } end - def is_liked - post = Post.find( params[:blog_id] ) - if post.likes.where(id: params[:id]).first - render text: true - else - render text: false - end - end - def create post = Post.find( params[:blog_id] ) like = post.likes.build diff --git a/app/models/post.rb b/app/models/post.rb index 3ecb973..d5fe8c4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -52,4 +52,8 @@ class Post < ActiveRecord::Base def liked_count self.likes.size end + + def liked_by?(like_id) + !! self.likes.where(id: like_id).first + end end diff --git a/app/views/blogs/_post.html.slim b/app/views/blogs/_post.html.slim index ab1635d..557c9ce 100644 --- a/app/views/blogs/_post.html.slim +++ b/app/views/blogs/_post.html.slim @@ -10,8 +10,9 @@ p.ptag.published-at = render 'common/copyright' hr.blog-over p - button.button.like-button type='button' - | #{@likes_count} Like + button.button.like-button class="#{'liked' if post.liked_by?(cookies[:like])}" type='button' data-url=blog_likes_path(post) + span.count #{@likes_count} + span Like .qrcode a#qrcode-link href="#" i.fi-link