smiles!!!

This commit is contained in:
nilsding 2014-11-30 20:31:22 +01:00
parent c7221ef377
commit 97c197f112
7 changed files with 104 additions and 5 deletions

View File

@ -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 = "<i class=\"fa fa-meh-o fa-spin\"></i>"
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 "<i class=\"fa fa-frown-o\"></i>"
when 'unsmile'
btn[0].dataset.action = 'smile'
btn.html "<i class=\"fa fa-smile-o\"></i>"
$(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'

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
json.partial! 'ajax/shared/status'

View File

@ -0,0 +1 @@
json.partial! 'ajax/shared/status'

View File

@ -17,8 +17,15 @@
%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
%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

View File

@ -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'