From 68f74395a35132d5200a3c7ace3e2740b2e47e71 Mon Sep 17 00:00:00 2001 From: n1vedhar1 Date: Tue, 3 Dec 2024 19:21:20 +0530 Subject: [PATCH] Added attachment for questions --- Gemfile | 2 -- app/models/rapidfire/question.rb | 3 +++ app/services/rapidfire/question_form.rb | 17 ++++++++++++++--- app/views/rapidfire/answers/_file.html.erb | 1 + .../rapidfire/answers/_file_attachment.html.erb | 8 ++++++++ app/views/rapidfire/answers/_multifile.html.erb | 1 + app/views/rapidfire/questions/_form.html.erb | 5 +++++ .../rapidfire/managing_questions_spec.rb | 2 +- .../features/rapidfire/managing_surveys_spec.rb | 2 +- 9 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 app/views/rapidfire/answers/_file_attachment.html.erb diff --git a/Gemfile b/Gemfile index bf17eb4c..4f30582a 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,6 @@ gem 'ostruct' group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] - # Adds support for Capybara system testing and selenium driver - gem 'capybara', '~> 2.13.0' gem 'selenium-webdriver' gem 'pry-rails' end diff --git a/app/models/rapidfire/question.rb b/app/models/rapidfire/question.rb index cddeb6af..ba923fe5 100644 --- a/app/models/rapidfire/question.rb +++ b/app/models/rapidfire/question.rb @@ -2,6 +2,9 @@ module Rapidfire class Question < ApplicationRecord belongs_to :survey, :inverse_of => :questions has_many :answers + + has_many_attached :files + default_scope { order(:position) } diff --git a/app/services/rapidfire/question_form.rb b/app/services/rapidfire/question_form.rb index 99c6e0dd..4e939f35 100644 --- a/app/services/rapidfire/question_form.rb +++ b/app/services/rapidfire/question_form.rb @@ -22,7 +22,7 @@ class QuestionForm < Rapidfire::BaseService attr_accessor :survey, :question, :default_text, :placeholder, :type, :question_text, :position, :answer_options, :answer_presence, - :answer_minimum_length, :answer_maximum_length, + :answer_minimum_length, :answer_maximum_length, :files, :answer_greater_than_or_equal_to, :answer_less_than_or_equal_to delegate :valid?, :errors, :to => :question @@ -47,11 +47,15 @@ def create_question return false end - @question = klass.create(to_question_params) + @question = klass.create(to_question_params).tap do |new_question| + attach_files(new_question) + end end def update_question - @question.update(to_question_params) + result = @question.update(to_question_params) + attach_files(@question) if result + result end def to_question_params @@ -73,11 +77,18 @@ def to_question_params } end + def attach_files(question) + if files.present? + files.each { |f| question.files.attach(f) } + end + end + def from_question_to_attributes(question) self.type = question.type self.survey = question.survey self.question_text = question.question_text self.position = question.position + self.files = question.files if question.files.attached? self.default_text = question.default_text self.placeholder = question.placeholder self.answer_options = question.answer_options diff --git a/app/views/rapidfire/answers/_file.html.erb b/app/views/rapidfire/answers/_file.html.erb index f3495dde..d1ceebef 100644 --- a/app/views/rapidfire/answers/_file.html.erb +++ b/app/views/rapidfire/answers/_file.html.erb @@ -1,5 +1,6 @@ <%= render partial: "rapidfire/answers/errors", locals: {answer: answer} %>
+<%= render partial: 'rapidfire/answers/file_attachment', locals: { answer: answer } %> <%= f.label :answer_text, answer.question.question_text.html_safe %> <%= f.file_field :file %>
diff --git a/app/views/rapidfire/answers/_file_attachment.html.erb b/app/views/rapidfire/answers/_file_attachment.html.erb new file mode 100644 index 00000000..8a20908d --- /dev/null +++ b/app/views/rapidfire/answers/_file_attachment.html.erb @@ -0,0 +1,8 @@ +<% if answer.question.files.attached? %> +

Attached Files:

+ <% answer.question.files.each do |file| %> +

+ <%= link_to file.filename.to_s, main_app.url_for(file), target: "_blank" %> +

+ <% end %> +<% end %> diff --git a/app/views/rapidfire/answers/_multifile.html.erb b/app/views/rapidfire/answers/_multifile.html.erb index 3739d782..e0b6f12a 100644 --- a/app/views/rapidfire/answers/_multifile.html.erb +++ b/app/views/rapidfire/answers/_multifile.html.erb @@ -1,5 +1,6 @@ <%= render partial: "rapidfire/answers/errors", locals: {answer: answer} %>
+<%= render partial: 'rapidfire/answers/file_attachment', locals: { answer: answer } %> <%= f.label :answer_text, answer.question.question_text.html_safe %> <%= f.file_field :files, multiple: true %>
diff --git a/app/views/rapidfire/questions/_form.html.erb b/app/views/rapidfire/questions/_form.html.erb index 5d779a5b..70d4b546 100644 --- a/app/views/rapidfire/questions/_form.html.erb +++ b/app/views/rapidfire/questions/_form.html.erb @@ -39,6 +39,11 @@ <%= f.text_area :answer_options, rows: 5 %> +
+ <%= f.label :files, "Attach Multiple Files" %> + <%= f.file_field :files, multiple: true %> +
+

Other options


diff --git a/spec/features/rapidfire/managing_questions_spec.rb b/spec/features/rapidfire/managing_questions_spec.rb index 904528e8..5a77f731 100644 --- a/spec/features/rapidfire/managing_questions_spec.rb +++ b/spec/features/rapidfire/managing_questions_spec.rb @@ -14,7 +14,7 @@ visit rapidfire.survey_questions_path(survey) page.within("#question_#{question1.id}") do - click_link "Delete" + click_button "Delete" end end diff --git a/spec/features/rapidfire/managing_surveys_spec.rb b/spec/features/rapidfire/managing_surveys_spec.rb index 5d45e4ec..f6cb271c 100644 --- a/spec/features/rapidfire/managing_surveys_spec.rb +++ b/spec/features/rapidfire/managing_surveys_spec.rb @@ -27,7 +27,7 @@ allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true) visit rapidfire.root_path - click_link "Delete" + click_button "Delete" end it "deletes the survey" do