diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index 123af69c..d2e158df 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -112,12 +112,54 @@ $(document).on "click", "button[name=ab-destroy]", -> complete: (jqxhr, status) -> btn.button "reset" +$(document).on "click", "button[name=ab-smile]", -> + btn = $(this) + aid = btn[0].dataset.aId + action = btn[0].dataset.action + count = Number $("span#ab-smile-count-#{aid}").html() + btn[0].dataset.loadingText = "" + btn.button "loading" + + target_url = switch action + when 'smile' + count++ + '/ajax/create_smile' + when 'unsmile' + count-- + '/ajax/destroy_smile' + + success = false + + $.ajax + url: target_url + type: 'POST' + data: + id: aid + success: (data, status, jqxhr) -> + success = data.success + if success + $("span#ab-smile-count-#{aid}").html(count) + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + btn.button "reset" + if success + switch action + when 'smile' + btn[0].dataset.action = 'unsmile' + btn.html "" + when 'unsmile' + btn[0].dataset.action = 'smile' + btn.html "" + $(document).on "click", "button[name=user-action]", -> btn = $(this) btn.button "loading" target = btn[0].dataset.target action = btn[0].dataset.action - count = Number($("h4.entry-text#follower-count").html()) + count = Number $("h4.entry-text#follower-count").html() target_url = switch action when 'follow' diff --git a/app/controllers/ajax/smile_controller.rb b/app/controllers/ajax/smile_controller.rb new file mode 100644 index 00000000..f6f8fa82 --- /dev/null +++ b/app/controllers/ajax/smile_controller.rb @@ -0,0 +1,39 @@ +class Ajax::SmileController < ApplicationController + def create + params.require :id + + answer = Answer.find(params[:id]) + + begin + current_user.smile answer + rescue + @status = :fail + @message = "You have already smiled that answer." + @success = false + return + end + + @status = :okay + @message = "Successfully smiled answer." + @success = true + end + + def destroy + params.require :id + + answer = Answer.find(params[:id]) + + begin + current_user.unsmile answer + rescue + @status = :fail + @message = "You have not smiled that answer." + @success = false + return + end + + @status = :okay + @message = "Successfully unsmiled answer." + @success = true + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 0f1e4f17..fea7e6a6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,6 +19,7 @@ class User < ActiveRecord::Base dependent: :destroy has_many :friends, through: :active_relationships, source: :target has_many :followers, through: :passive_relationships, source: :source + has_many :smiles SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ @@ -66,7 +67,7 @@ class User < ActiveRecord::Base target_user.decrement! :follower_count end - # @return [Boolean] true if +current_user+ is following +target_user+ + # @return [Boolean] true if +self+ is following +target_user+ def following?(target_user) friends.include? target_user end @@ -86,4 +87,10 @@ class User < ActiveRecord::Base decrement! :smiled_count answer.decrement! :smile_count end + + def smiled?(answer) + # TODO: you know what to do here, nilsding + answer.smiles.each { |s| return true if s.user_id == self.id } + false + end end diff --git a/app/views/ajax/smile/create.json.jbuilder b/app/views/ajax/smile/create.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/smile/create.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/app/views/ajax/smile/destroy.json.jbuilder b/app/views/ajax/smile/destroy.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/smile/destroy.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/app/views/shared/_answerbox.html.haml b/app/views/shared/_answerbox.html.haml index 4f896f5b..4a96497e 100644 --- a/app/views/shared/_answerbox.html.haml +++ b/app/views/shared/_answerbox.html.haml @@ -17,9 +17,16 @@ %img.img-rounded.img-answerbox-small{src: gravatar_url(a.user)} %span= a.user.screen_name .col-md-6.col-sm-8.col-xs-5.text-right - %span.hidden-xs.text-muted x users smiled at this - %a.btn.btn-info.btn-sm - %i.fa.fa-smile-o + %span.hidden-xs.text-muted + %span{id: "ab-smile-count-#{a.id}"}= a.smile_count + users smiled this + - if user_signed_in? + - if current_user.smiled? a + %button.btn.btn-info.btn-sm{type: :button, name: 'ab-smile', 'data-a-id' => a.id, 'data-action' => 'unsmile'} + %i.fa.fa-frown-o + - else + %button.btn.btn-info.btn-sm{type: :button, name: 'ab-smile', 'data-a-id' => a.id, 'data-action' => 'smile'} + %i.fa.fa-smile-o %a.btn.btn-primary.btn-sm %i.fa.fa-comments - if privileged? a.user diff --git a/config/routes.rb b/config/routes.rb index 49ed4547..286bc9d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,8 @@ Rails.application.routes.draw do match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer match '/create_friend', to: 'friend#create', via: :post, as: :create_friend match '/destroy_friend', to: 'friend#destroy', via: :post, as: :destroy_friend + match '/create_smile', to: 'smile#create', via: :post, as: :create_smile + match '/destroy_smile', to: 'smile#destroy', via: :post, as: :destroy_smile end match '/inbox', to: 'inbox#show', via: 'get'