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

Limit submissions redux #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/admin/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ def show
end

def destroy
Project.find(params[:id]).delete
DemoNight.current.accepting_submissions! if DemoNight.current.projects.count < 16
redirect_to request.referrer
Project.find(params[:id]).destroy
redirect_back fallback_location: admin_projects_path
end

private
Expand Down
1 change: 0 additions & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def new
def create
@project = DemoNight.current.projects.new(project_params)
if @project.save
DemoNight.current.voting! if DemoNight.current.projects.count >= 16
flash[:success] = "Project successfully submitted!"
redirect_to projects_path
else
Expand Down
15 changes: 15 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Project < ApplicationRecord

validates_presence_of :name

after_create_commit :check_and_toggle_demo_night_to_voting
after_destroy_commit :check_and_toggle_demo_night_to_accepting

def number_of_votes
votes.count
end
Expand Down Expand Up @@ -54,4 +57,16 @@ def self.current_projects
[]
end
end

private

def check_and_toggle_demo_night_to_accepting
return unless demo_night.number_of_projects < 16
demo_night.accepting_submissions!
end

def check_and_toggle_demo_night_to_voting
return unless demo_night.number_of_projects > 16
demo_night.voting!
end
end
4 changes: 2 additions & 2 deletions spec/models/demo_night_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
project3.votes.create(user: user1, representation: 5, challenge: 5, wow: 5)
project3.votes.create(user: user2, representation: 5, challenge: 5, wow: 5)
projects = [project1, project2, project3]
demonight = create(:demo_night)
demonight.projects << projects
demonight = create(:demo_night)
demonight.projects << projects
demonight_projects = demonight.sorted_projects

expect(demonight_projects.first.name).to eq(project3.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

describe 'Demo Night Toggles to accepting submissions after 16th project is created', type: :request do
let!(:demo_night) { create(:demo_night, status: 'voting') }
let(:admin) { create(:admin) }

before { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin) }

context 'on project destroy' do
subject { delete "/admin/projects/#{Project.last.id}" }

it 'changes the current demo night status' do
create_list(:project, 16, demo_night: demo_night)

expect { subject }.to change { DemoNight.current.status }.to('accepting_submissions')
end

it 'does not current demo night status' do
create_list(:project, 17, demo_night: demo_night)

expect { subject }.to_not change { DemoNight.current.status }
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

describe 'Demo Night Toggles to voting after the 15th project is created', type: :request do
let!(:demo_night) { create(:demo_night) }
let(:user) { create(:user) }

before { allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) }

context 'on project create' do
let(:attrs) { attributes_for(:project) }
subject { post '/projects', params: { project: attrs } }

it 'changes the current demo night status' do
create_list(:project, 16, demo_night: demo_night)

expect { subject }.to change { DemoNight.current.status }.to('voting')
end

it 'does not change the current demo night status' do
create_list(:project, 15, demo_night: demo_night)

expect { subject }.to_not change { DemoNight.current.status }
end
end
end