Last active
August 9, 2023 20:25
-
-
Save alexshapalov/34ce755de39609922a2707431885f0e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def accept | |
authorize!(:admin_affiliates, org) | |
service = AffiliateApplicationService.new(@campaign) | |
if service.accept_affiliate_application(affiliate_application_id: params[:id]) | |
flash[:success] = "Accepted #{service.application.user.name}" | |
else | |
flash[:error] = "Application was not accepted. Please try again" | |
end | |
respond_to do |format| | |
format.turbo_stream do | |
render turbo_stream: turbo_stream.replace(dom_id(service.application), partial: 'application_accept_reject_column', locals: { affiliate_application: service.application }); | |
end | |
format.html do | |
redirect_to admin_campaign_affiliate_applications_path(@campaign) | |
end | |
end | |
end | |
# Rspec Test for accept | |
# I use factories (Factory Bot gem) | |
# for prepare: org, campaign, affiliate_application, user, trait for admin | |
# Devise for authentication | |
require 'rails_helper' | |
RSpec.describe YourControllerName, type: :controller do | |
describe 'POST #accept' do | |
let(:org) { create(:org) } | |
let(:campaign) { create(:campaign) } | |
let(:affiliate_application) { create(:affiliate_application, campaign: campaign) } | |
let(:user) { create(:user, :admin) } | |
before do | |
sign_in user | |
end | |
context 'when application is accepted successfully' do | |
it 'sets a success flash message' do | |
post :accept, params: { id: affiliate_application.id } | |
expect(flash[:success]).to eq "Accepted #{affiliate_application.user.name}" | |
end | |
end | |
context 'when application is not accepted' do | |
it 'sets an error flash message' do | |
post :accept, params: { id: affiliate_application.id } | |
expect(flash[:error]).to eq 'Application was not accepted. Please try again' | |
end | |
end | |
context 'with turbo_stream format' do | |
it 'renders the application_accept_reject_column partial' do | |
post :accept, params: { id: affiliate_application.id }, format: :turbo_stream | |
expect(response).to render_template(partial: '_application_accept_reject_column') | |
end | |
end | |
context 'with html format' do | |
it 'redirects to the affiliate applications index page' do | |
post :accept, params: { id: affiliate_application.id }, format: :html | |
expect(response).to redirect_to admin_campaign_affiliate_applications_path(campaign) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment