Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
implementing update method
Browse files Browse the repository at this point in the history
  • Loading branch information
nehamand committed Nov 9, 2023
1 parent 791a4b3 commit d835086
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 3 deletions.
16 changes: 15 additions & 1 deletion app/controllers/new_admin/questionnaires_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:)
Expand Down
2 changes: 1 addition & 1 deletion app/views/new_admin/questionnaires/_question_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</th>
<td class="px-4 py-3">
<div class="flex items-center">
<%= 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" %>
</div>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<th class="px-4 py-3"><%= t 'created_at' %></th>
<th class="px-4 py-3"><%= t 'updated_at' %></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800 text-sm">
Expand Down Expand Up @@ -49,6 +50,14 @@
%>
</div>
</td>
<td class="px-4 py-3">
<div class="flex items-center">
<%= 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'
%>
</div>
</td>
</tr>
<% end %>
</tbody>
Expand Down
7 changes: 7 additions & 0 deletions app/views/new_admin/questionnaires/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% content_for :page_title do %>
<%= t '.edit' %>
<% end %>

<div data-tab-id="edit_questionnaire">
<%= render 'form', questionnaire: @questionnaire, url: new_admin_update_questionnaire_path %>
</div>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
107 changes: 107 additions & 0 deletions spec/requests/new_admin/questionnaires_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d835086

Please sign in to comment.