Commit 25004cbc authored by Z.J. van de Weg's avatar Z.J. van de Weg Committed by Fatih Acet

Snippets get award emoji! 👍

parent b94de5fd
......@@ -508,6 +508,7 @@ v 8.10.0
- Updated project header design
- Issuable collapsed assignee tooltip is now the users name
- Fix compare view not changing code view rendering style
- Emoji can be awarded on Snippets !4456
- Exclude email check from the standard health check
- Updated layout for Projects, Groups, Users on Admin area. !4424
- Fix changing issue state columns in milestone view
......
......@@ -10,7 +10,9 @@ module ToggleAwardEmoji
if awardable.user_can_award?(current_user, name)
awardable.toggle_award_emoji(name, current_user)
TodoService.new.new_award_emoji(to_todoable(awardable), current_user)
todoable = to_todoable(awardable)
TodoService.new.new_award_emoji(todoable, current_user) if todoable
render json: { ok: true }
else
......@@ -24,8 +26,10 @@ module ToggleAwardEmoji
case awardable
when Note
awardable.noteable
else
when MergeRequest, Issue
awardable
when Snippet
nil
end
end
......
class SnippetsController < ApplicationController
include ToggleAwardEmoji
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
# Allow read snippet
......@@ -85,6 +87,7 @@ class SnippetsController < ApplicationController
PersonalSnippet.find(params[:id])
end
end
alias_method :awardable, :snippet
def authorize_read_snippet!
authenticate_user! unless can?(current_user, :read_personal_snippet, @snippet)
......
......@@ -4,6 +4,7 @@ class Snippet < ActiveRecord::Base
include Participable
include Referable
include Sortable
include Awardable
default_value_for :visibility_level, Snippet::PRIVATE
......
......@@ -101,6 +101,7 @@ Rails.application.routes.draw do
resources :snippets do
member do
get 'raw'
post :toggle_award_emoji
end
end
......@@ -110,7 +111,6 @@ Rails.application.routes.draw do
#
# Invites
#
resources :invites, only: [:show], constraints: { id: /[A-Za-z0-9_-]+/ } do
member do
post :accept
......@@ -665,6 +665,7 @@ Rails.application.routes.draw do
resources :snippets, constraints: { id: /\d+/ } do
member do
get 'raw'
post :toggle_award_emoji
end
end
......
module API
class AwardEmoji < Grape::API
before { authenticate! }
AWARDABLES = [Issue, MergeRequest]
AWARDABLES = [Issue, MergeRequest, Snippet]
resource :projects do
AWARDABLES.each do |awardable_type|
......
require 'spec_helper'
describe SnippetsController do
describe 'GET #show' do
let(:user) { create(:user) }
describe 'GET #show' do
context 'when the personal snippet is private' do
let(:personal_snippet) { create(:personal_snippet, :private, author: user) }
......@@ -230,4 +230,31 @@ describe SnippetsController do
end
end
end
context 'award emoji on snippets' do
let(:personal_snippet) { create(:personal_snippet, :private, author: user) }
before do
sign_in(user)
end
describe 'POST #toggle_award_emoji' do
it "toggles the award emoji" do
expect do
post(:toggle_award_emoji, id: personal_snippet.to_param, name: "thumbsup")
end.to change { personal_snippet.award_emoji.count }.by(1)
expect(response.status).to eq(200)
end
it "removes the already awarded emoji" do
post(:toggle_award_emoji, id: personal_snippet.to_param, name: "thumbsup")
expect do
post(:toggle_award_emoji, id: personal_snippet.to_param, name: "thumbsup")
end.to change { personal_snippet.award_emoji.count }.by(-1)
expect(response.status).to eq(200)
end
end
end
end
......@@ -9,12 +9,14 @@ describe Snippet, models: true do
it { is_expected.to include_module(Participable) }
it { is_expected.to include_module(Referable) }
it { is_expected.to include_module(Sortable) }
it { is_expected.to include_module(Awardable) }
end
describe 'associations' do
it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:notes).dependent(:destroy) }
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
end
describe 'validation' do
......
......@@ -14,6 +14,9 @@ describe API::API, api: true do
describe "GET /projects/:id/awardable/:awardable_id/award_emoji" do
context 'on an issue' do
let(:issue) { create(:issue, project: project, author: user) }
let!(:award_emoji) { create(:award_emoji, awardable: issue, user: user) }
it "returns an array of award_emoji" do
get api("/projects/#{project.id}/issues/#{issue.id}/award_emoji", user)
......@@ -39,6 +42,10 @@ describe API::API, api: true do
end
end
context 'on a snippet' do
it 'returns the awarded '
end
context 'when the user has no access' do
it 'returns a status code 404' do
user1 = create(:user)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment