Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single and multiple file attachments to Questions #154

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/models/rapidfire/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
17 changes: 14 additions & 3 deletions app/services/rapidfire/question_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/rapidfire/answers/_file.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= render partial: "rapidfire/answers/errors", locals: {answer: answer} %>
<div class="form-group">
<%= render partial: 'rapidfire/answers/file_attachment', locals: { answer: answer } %>
<%= f.label :answer_text, answer.question.question_text.html_safe %>
<%= f.file_field :file %>
</div>
8 changes: 8 additions & 0 deletions app/views/rapidfire/answers/_file_attachment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if answer.question.files.attached? %>
<p><strong>Attached Files:</strong></p>
<% answer.question.files.each do |file| %>
<p>
<%= link_to file.filename.to_s, main_app.url_for(file), target: "_blank" %>
</p>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions app/views/rapidfire/answers/_multifile.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= render partial: "rapidfire/answers/errors", locals: {answer: answer} %>
<div class="form-group">
<%= 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 %>
</div>
5 changes: 5 additions & 0 deletions app/views/rapidfire/questions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<%= f.text_area :answer_options, rows: 5 %>
</div>

<div class="form-group">
<%= f.label :files, "Attach Multiple Files" %>
<%= f.file_field :files, multiple: true %>
</div>

<h4>Other options</h4>
<hr/>

Expand Down
2 changes: 1 addition & 1 deletion spec/features/rapidfire/managing_questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
visit rapidfire.survey_questions_path(survey)

page.within("#question_#{question1.id}") do
click_link "Delete"
click_button "Delete"
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/rapidfire/managing_surveys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading