Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
24 changes: 24 additions & 0 deletions app/controllers/users/degrees_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Users
class DegreesController < ApplicationController
before_action :authenticate_user!
before_action :ensure_feature_enabled!

def edit; end

def update
current_user.degree = Degree.find(params[:degree_id])

if current_user.save
redirect_to root_path, notice: "Tu carrera ha sido actualizada correctamente."
else
redirect_to edit_user_degree_path, alert: "Hubo un error actualizando tu carrera."
end
end

private

def ensure_feature_enabled!
redirect_to root_path unless Features::ChangingDegrees.enabled?
end
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def planned?(subject)
private

def set_default_degree
self.degree = Degree.default
self.degree ||= Degree.default
end
end

Expand Down
9 changes: 9 additions & 0 deletions app/services/features/changing_degrees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Features
module ChangingDegrees
module_function

def enabled?
ENV['ENABLE_CHANGING_DEGREES'].present?
end
end
end
3 changes: 3 additions & 0 deletions app/views/shared/_user_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<li class="border-t border-gray-100 my-1"></li>
<%= link_to "Editar Perfil", edit_user_registration_path, class: "block px-4 py-2 hover:bg-gray-100", tabindex: 0 %>
<%= link_to "Subir Escolaridad", new_transcript_path, class: "block px-4 py-2 hover:bg-gray-100", tabindex: 0 %>
<% if Features::ChangingDegrees.enabled? %>
<%= link_to "Cambiar Carrera", edit_user_degree_path(current_user), class: "block px-4 py-2 hover:bg-gray-100", tabindex: 0 %>
<% end %>
<%= link_to "Salir", destroy_user_session_path, data: { turbo: true, turbo_method: :delete }, class: "block px-4 py-2 hover:bg-gray-100", tabindex: 0 %>
<% end %>
</ul>
Expand Down
36 changes: 36 additions & 0 deletions app/views/users/degrees/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<div class="flex flex-col justify-center items-center p-6 space-y-6">
<h2 class="text-3xl/9 text-neutral-700">Cambiar Carrera</h2>

<%= form_with(url: user_degree_path(current_user), method: :put, class: "flex flex-col w-full max-w-md space-y-6") do |f| %>
<div>
<%= f.label :degree_id, "Carrera de Grado", class: "text-sm/6 font-medium text-gray-700 with-error:text-red-600" %>
<%= f.select(
:degree_id,
options_for_select(
Degree.ids.map { |id| [id.humanize, id] },
selected: current_user.degree_id
),
{
include_blank: "Selecciona tu carrera de grado",
},
{
class: %w[
bg-white
w-full
rounded-md
px-3
py-1.5
outline-1
outline-gray-300
focus:outline-2
focus:outline-indigo-600
with-error:outline-red-500
with-error:focus:outline-red-600
].join(" "),
required: true
},
) %>
</div>
<%= render ButtonComponent.new(form: f, label: "Guardar") %>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

scope path: "usuarios", module: "users", as: "user" do
resources :passkeys, only: [:index, :create, :destroy]
resources :degrees, only: [:update, :edit]
end

root to: "subjects#index"
Expand Down
21 changes: 21 additions & 0 deletions spec/services/features/changing_degrees_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

RSpec.describe Features::ChangingDegrees do
describe '.enabled?' do
after do
ENV.delete('ENABLE_CHANGING_DEGREES')
end

it 'returns true when the ENABLE_CHANGING_DEGREES env var is set' do
ENV['ENABLE_CHANGING_DEGREES'] = 'true'

expect(described_class.enabled?).to be true
end

it 'returns false when the ENABLE_CHANGING_DEGREES env var is not set' do
ENV['ENABLE_CHANGING_DEGREES'] = nil

expect(described_class.enabled?).to be false
end
end
end
75 changes: 75 additions & 0 deletions spec/system/changing_degrees_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'rails_helper'

RSpec.describe 'Changing degrees', type: :system do
let!(:computacion_degree) { degrees(:computacion) }
let!(:sistemas_degree) { create(:degree, id: 'sistemas', current_plan: '2025', include_inco_subjects: false) }
let!(:user) { create(:user, degree: computacion_degree) }

before do
sign_in user
end

context 'when feature is enabled' do
before do
allow(Features::ChangingDegrees).to receive(:enabled?).and_return(true)
end

it 'allows user to change their degree successfully' do
visit root_path

click_user_menu
click_on 'Cambiar Carrera'

expect(page).to have_text('Cambiar Carrera')
expect(page).to have_select('degree_id', selected: 'Computacion')

select 'Sistemas', from: 'degree_id'
click_on 'Guardar'

expect(page).to have_text('Tu carrera ha sido actualizada correctamente.')
expect(current_path).to eq(root_path)

user.reload
expect(user.degree_id).to eq('sistemas')
end
end

context 'when feature is disabled' do
before do
allow(Features::ChangingDegrees).to receive(:enabled?).and_return(false)
end

it 'does not show the change degree link in user menu' do
visit root_path

click_user_menu

expect(page).not_to have_link('Cambiar Carrera')
end

it 'redirects to root path when trying to access edit page directly' do
visit edit_user_degree_path(user)

expect(current_path).to eq(root_path)
end
end

context 'when user is not authenticated' do
before do
allow(Features::ChangingDegrees).to receive(:enabled?).and_return(true)
sign_out :user
end

it 'redirects to login page when accessing edit page' do
visit edit_user_degree_path(user)

expect(current_path).to eq(new_user_session_path)
end
end

private

def click_user_menu
find("#user-menu[data-controller-connected='true']").click
end
end