diff --git a/app/controllers/new_admin/questionnaires_controller.rb b/app/controllers/new_admin/questionnaires_controller.rb
index 58cc417de..8b417c96c 100644
--- a/app/controllers/new_admin/questionnaires_controller.rb
+++ b/app/controllers/new_admin/questionnaires_controller.rb
@@ -30,6 +30,20 @@ def create
end
end
+ def edit
+ @questionnaire = Questionnaire.find(params[:id])
+ end
+
+ def update
+ @questionnaire = Questionnaire.find(params[:id])
+
+ if @questionnaire.update(questionnaire_params)
+ redirect_on_success new_admin_show_questionnaire_path(id: @questionnaire.id), message_scope: 'update'
+ else
+ render_on_failure :edit
+ end
+ end
+
private
def filters
@@ -48,7 +62,7 @@ def questionnaires
def questionnaire_params
params.require(:questionnaire).permit(:title, :kind, :active, :description,
- questions_attributes: %i[title kind raw_answer_options _destroy])
+ questions_attributes: %i[title kind raw_answer_options _destroy id])
end
def redirect_on_success(url, message_scope:)
diff --git a/app/views/new_admin/questionnaires/_question_form.html.erb b/app/views/new_admin/questionnaires/_question_form.html.erb
index 6d082e94c..149ec8a90 100644
--- a/app/views/new_admin/questionnaires/_question_form.html.erb
+++ b/app/views/new_admin/questionnaires/_question_form.html.erb
@@ -46,7 +46,7 @@
- <%= form.text_field :raw_answer_options, class: "bg-gray-100 font-semibold rounded-lg text-gray-600 dark:text-gray-400 h-8 dark:bg-slate-700" %>
+ <%= form.text_field :raw_answer_options, value: form.object.answer_options_to_string, class: "bg-gray-100 font-semibold rounded-lg text-gray-600 dark:text-gray-400 h-8 dark:bg-slate-700" %>
|
diff --git a/app/views/new_admin/questionnaires/_questionnaires_table.html.erb b/app/views/new_admin/questionnaires/_questionnaires_table.html.erb
index 9222f8e9e..c97eb73b0 100644
--- a/app/views/new_admin/questionnaires/_questionnaires_table.html.erb
+++ b/app/views/new_admin/questionnaires/_questionnaires_table.html.erb
@@ -7,6 +7,7 @@
<%= t 'created_at' %> |
<%= t 'updated_at' %> |
|
+ |
@@ -49,6 +50,14 @@
%>
+
+
+ <%= link_to t('edit'),
+ edit_new_admin_questionnaire_path(id: questionnaire.id),
+ class: 'font-semibold transition-colors duration-150 underline dark:hover:text-white hover:text-gray-900'
+ %>
+
+ |
<% end %>
diff --git a/app/views/new_admin/questionnaires/edit.html.erb b/app/views/new_admin/questionnaires/edit.html.erb
new file mode 100644
index 000000000..2b6c97d53
--- /dev/null
+++ b/app/views/new_admin/questionnaires/edit.html.erb
@@ -0,0 +1,7 @@
+<% content_for :page_title do %>
+ <%= t '.edit' %>
+<% end %>
+
+
+ <%= render 'form', questionnaire: @questionnaire, url: new_admin_update_questionnaire_path %>
+
diff --git a/config/routes.rb b/config/routes.rb
index 50e5718d9..0a83deaad 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -45,8 +45,9 @@
resources :projects, only: :update, as: :update_project
resources :projects, only: :destroy, as: :destroy_project
resources :revenue_forecast, only: :index
- resources :questionnaires, except: %i[show]
+ resources :questionnaires, except: %i[show update]
resources :questionnaires, only: :show, as: :show_questionnaire
+ resources :questionnaires, only: :update, as: :update_questionnaire
namespace :projects do
resources :allocate_users, only: %i[new create]
diff --git a/spec/requests/new_admin/questionnaires_controller_spec.rb b/spec/requests/new_admin/questionnaires_controller_spec.rb
index 91e962a8e..7aca4974e 100644
--- a/spec/requests/new_admin/questionnaires_controller_spec.rb
+++ b/spec/requests/new_admin/questionnaires_controller_spec.rb
@@ -191,4 +191,111 @@
end
end
end
+
+ describe 'GET #edit' do
+ context 'when user is admin' do
+ let(:user) { create(:user, :admin) }
+ let(:questionnaire) { create(:questionnaire) }
+ before { sign_in user }
+
+ it 'renders edit template' do
+ get edit_new_admin_questionnaire_path(questionnaire.id)
+
+ expect(response).to render_template(:edit)
+ end
+
+ it 'returns http status 200 ok' do
+ get edit_new_admin_questionnaire_path(questionnaire.id)
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:questionnaire) { create(:questionnaire) }
+
+ before { sign_in user }
+
+ it 'redirects to root page' do
+ get edit_new_admin_questionnaire_path(questionnaire.id)
+
+ expect(response).to redirect_to(root_path)
+ end
+ end
+
+ context 'when user is not logged in' do
+ let(:questionnaire) { create(:questionnaire) }
+
+ it 'redirects to root path' do
+ get edit_new_admin_questionnaire_path(questionnaire.id)
+
+ expect(response).to redirect_to(root_path)
+ end
+ end
+ end
+
+ describe 'PUT/PATCH #update' do
+ context 'when user is admin' do
+ let(:user) { create(:user, :admin) }
+ let(:questionnaire) { create(:questionnaire) }
+
+ before { sign_in user }
+
+ context 'whith valid params' do
+ let(:valid_params) { { questionnaire: { title: 'New title' } } }
+ it 'updates the questionnaire' do
+ patch new_admin_update_questionnaire_path(questionnaire.id, params: valid_params)
+
+ expect(questionnaire.reload.title).to eq('New title')
+ end
+
+ it 'redirects to show page' do
+ patch new_admin_update_questionnaire_path(questionnaire.id, params: valid_params)
+
+ expect(response).to redirect_to(new_admin_show_questionnaire_path)
+ end
+ end
+
+ context 'with invalid params' do
+ let(:invalid_params) { { questionnaire: { title: '' } } }
+
+ it 'does not update the questionnaire' do
+ expect { patch new_admin_update_questionnaire_path(questionnaire.id, params: invalid_params) }.not_to change(Questionnaire.find(questionnaire.id), :title)
+ end
+
+ it 'renders the edit template with errors', :aggregate_failures do
+ patch new_admin_update_questionnaire_path(questionnaire.id, params: invalid_params)
+
+ expect(response.body).to include('Título não pode ficar em branco')
+ expect(response).to render_template(:edit)
+ end
+ end
+ end
+
+ context 'when user is not admin' do
+ let(:user) { create(:user) }
+ let(:questionnaire) { create(:questionnaire) }
+ let(:valid_params) { { questionnaire: { title: 'New title' } } }
+
+ before { sign_in user }
+
+ it 'redirects to root path' do
+ patch new_admin_update_questionnaire_path(questionnaire.id, params: valid_params)
+
+ expect(response).to redirect_to(root_path)
+ end
+ end
+
+ context 'when user is not logged in' do
+ let(:questionnaire) { create(:questionnaire) }
+ let(:valid_params) { { questionnaire: { title: 'New title' } } }
+
+ it 'redirects to root path' do
+ patch new_admin_update_questionnaire_path(questionnaire.id, params: valid_params)
+
+ expect(response).to redirect_to(root_path)
+ end
+ end
+ end
end