Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Adds project submission policy. #4349

Merged
merged 7 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/controllers/project_submissions/likes_controller.rb
Expand Up @@ -2,16 +2,36 @@ class ProjectSubmissions::LikesController < ApplicationController
before_action :authenticate_user!

def update
@project_submission = ProjectSubmission.find(params[:project_submission_id])
if policy.allowed?
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
@project_submission = ProjectSubmission.find(params[:project_submission_id])
toggle

respond_to do |format|
format.turbo_stream
end
else
send_error_flash
end
end

private

def toggle
if @project_submission.liked_by?(current_user)
@project_submission.unlike(current_user)
else
@project_submission.like(current_user)
end
end

def send_error_flash
respond_to do |format|
format.turbo_stream
flash.now[:alert] = 'Your account is either too young or has not submitted a project.'
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
format.turbo_stream { render turbo_stream: turbo_stream.update('flash-messages', partial: 'shared/flash') }
end
end

def policy
SubmissionVotePolicy.new(current_user)
end
end
21 changes: 21 additions & 0 deletions app/policies/submission_vote_policy.rb
@@ -0,0 +1,21 @@
class SubmissionVotePolicy
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
def initialize(user)
@user = user
end

def allowed?
return true if user_has_submissions

!another_acct_created_recently
end

private

def another_acct_created_recently
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
User.where(last_sign_in_ip: @user.current_sign_in_ip).where.not(id: @user.id).any?
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
end

def user_has_submissions
@user.project_submissions.any?
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
end
end
56 changes: 55 additions & 1 deletion spec/system/lesson_project_submissions/like_spec.rb
@@ -1,14 +1,20 @@
require 'rails_helper'

RSpec.describe 'Liking project submissions' do
let(:user) { create(:user) }
let(:lesson) { create(:lesson, :project) }
let(:user_lesson) { create(:lesson, :project) }
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved

context 'when the submission has no likes' do
let(:user) { create(:user, created_at: 15.days.ago) }
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved

before do
create(:project_submission, lesson:)

sign_in(user)

visit lesson_path(user_lesson)
Pages::ProjectSubmissions::Form.new.open.fill_in.submit
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved

visit lesson_project_submissions_path(lesson)
end

Expand All @@ -29,10 +35,16 @@
end

context 'when the submission has existing likes' do
let(:user) { create(:user, created_at: 15.days.ago) }

before do
create(:project_submission, lesson:, likes_count: 10)

sign_in(user)

visit lesson_path(user_lesson)
Pages::ProjectSubmissions::Form.new.open.fill_in.submit

visit lesson_project_submissions_path(lesson)
end

Expand All @@ -51,4 +63,46 @@
end
end
end

context 'when submission has no likes and user is duplicate' do
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
let(:user) { create(:user) }
let(:another_user) { create(:user) }

before do
create(:project_submission, lesson:)

sign_in(another_user) # Sets last sign in ip to localhost
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
sign_in(user) # Sets current ip to localhost
visit lesson_project_submissions_path(lesson)
end

it 'cannot like the submission' do
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
within(:test_project_submission, 1) do
expect(find(:test_id, 'like-count')).to have_content('0')

find(:test_id, 'like-submission').click
expect(find(:test_id, 'like-count')).to have_content('0')
end
end
end

context 'when submission has no likes and user has no submissions' do
ZachBaird marked this conversation as resolved.
Show resolved Hide resolved
let(:user) { create(:user) }

before do
create(:project_submission, lesson:)

sign_in(user)
visit lesson_project_submissions_path(lesson)
end

it 'can like the submission' do
within(:test_project_submission, 1) do
expect(find(:test_id, 'like-count')).to have_content('0')

find(:test_id, 'like-submission').click
expect(find(:test_id, 'like-count')).to have_content('1')
end
end
end
end