From 28eaa239512091219f5dc2a17c1da827865f8866 Mon Sep 17 00:00:00 2001 From: n1vedhar1 Date: Wed, 4 Dec 2024 19:36:39 +0530 Subject: [PATCH] Added results per attempt --- .../rapidfire/surveys_controller.rb | 7 ++++ app/views/rapidfire/surveys/results.html.erb | 34 +++---------------- .../rapidfire/surveys/show_result.html.erb | 17 ++++++++++ config/routes.rb | 6 +++- .../rapidfire/managing_surveys_spec.rb | 24 ++++++++++--- 5 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 app/views/rapidfire/surveys/show_result.html.erb diff --git a/app/controllers/rapidfire/surveys_controller.rb b/app/controllers/rapidfire/surveys_controller.rb index e615fe01..9bf38e44 100644 --- a/app/controllers/rapidfire/surveys_controller.rb +++ b/app/controllers/rapidfire/surveys_controller.rb @@ -72,6 +72,7 @@ def destroy def results params[:filter] ||= {} @survey = Rapidfire::Survey.find(params[:id]) + @survey_attempts = @survey.attempts @survey_results = SurveyResults.new(survey: @survey).extract(filter_params) @@ -83,6 +84,12 @@ def results end end + def show_result + @survey = Rapidfire::Survey.find(params[:survey_id]) + @attempt = Rapidfire::Attempt.find(params[:id]) + @answers = @attempt.answers.includes(:question) + end + private def survey_params diff --git a/app/views/rapidfire/surveys/results.html.erb b/app/views/rapidfire/surveys/results.html.erb index 2920fb93..1c31c0ea 100644 --- a/app/views/rapidfire/surveys/results.html.erb +++ b/app/views/rapidfire/surveys/results.html.erb @@ -1,34 +1,8 @@

Results

<%= link_to "Download CSV", results_survey_path(id: params[:id], format: :csv) %> -<%- @survey_results.each do |result| %> -
-

<%= result.question.question_text %>

-

- <%- if result.results.is_a?(Array) %> -

    - <%- result.results.each do |answer| %> -
  1. <%= answer %>
  2. - <% end %> -
- <% elsif result.results.is_a?(Hash) %> - - - - - - - - - - <%- result.results.each do |option, count| %> - - - - - <% end %> - -
OptionCountFilter
<%= option %><%= count %><%= filter_link(result.question.id, option) %> -
- <% end %> +<%- @survey_attempts.each do |attempt| %> +
+

User : <%= attempt.user_id %>

+ <%= link_to "View Answers", survey_result_path(@survey, attempt.id) %>
<% end %> diff --git a/app/views/rapidfire/surveys/show_result.html.erb b/app/views/rapidfire/surveys/show_result.html.erb new file mode 100644 index 00000000..e9666a18 --- /dev/null +++ b/app/views/rapidfire/surveys/show_result.html.erb @@ -0,0 +1,17 @@ +

Survey Result

+ +

Survey Name: <%= @survey.name %>

+

User ID: <%= @attempt.user_id %>

+ +
+ <% @answers.each do |answer| %> +
+

Question: <%= answer.question.question_text %>

+

Answer: <%= answer.answer_text %>

+
+
+ <% end %> +
+ + +<%= link_to "Back to All Results", results_survey_path(@survey) %> diff --git a/config/routes.rb b/config/routes.rb index 7115f3bc..ebe99332 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,10 @@ Rapidfire::Engine.routes.draw do resources :surveys do - get 'results', on: :member + member do + get 'results' + end + + get 'results/:id', to: 'surveys#show_result', as: 'result' resources :questions resources :attempts, only: [:new, :create, :edit, :update, :show] diff --git a/spec/features/rapidfire/managing_surveys_spec.rb b/spec/features/rapidfire/managing_surveys_spec.rb index f6cb271c..81f47903 100644 --- a/spec/features/rapidfire/managing_surveys_spec.rb +++ b/spec/features/rapidfire/managing_surveys_spec.rb @@ -25,7 +25,6 @@ context "when user can administer" do before do allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true) - visit rapidfire.root_path click_button "Delete" end @@ -148,9 +147,26 @@ end end - it "shows results for particular question group" do - expect(page).to have_content "Results" - expect(page).to have_content "hindi 3" + it "shows results for all attempts" do + survey.attempts.each do |attempt| + expect(page).to have_content "User: #{attempt.user_id}" + expect(page).to have_link "View Answers", href: result_survey_path(survey, attempt.id) + end + end + + context "when viewing an individual attempt" do + let(:attempt) { survey.attempts.first || survey.attempts.create(user_id: 1) } + + before do + visit rapidfire.survey_result_path(survey, attempt.id) + end + + it "displays the questions and answers for the attempt" do + attempt.answers.each do |answer| + expect(page).to have_content answer.question.question_text + expect(page).to have_content answer.answer_text + end + end end end end