Skip to content

Commit

Permalink
Adds new options to the Survey model to configure visibility of surveys
Browse files Browse the repository at this point in the history
Allow them to be shown on maps or stops—or neither. And restrict the list of stops and routes.
  • Loading branch information
aaronbrethorst committed Aug 9, 2024
1 parent 5acc84e commit 4f664ef
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 44 deletions.
11 changes: 10 additions & 1 deletion app/controllers/admins/surveys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ def destroy
private

def survey_params
params.require(:survey).permit(:name, :enabled, :extra_data, questions: [])
params.require(:survey).permit(
:name,
:enabled,
:extra_data,
:show_on_map,
:show_on_stops,
:visible_stop_list,
:visible_route_list,
questions: []
)
end

def load_study
Expand Down
7 changes: 6 additions & 1 deletion app/models/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ class Survey < ApplicationRecord
# Extra Data

jsonb_accessor(:extra_data, {
name: :string
name: :string,

show_on_map: :boolean,
show_on_stops: :boolean,
visible_stop_list: :string,
visible_route_list: :string
})

validates :name, presence: true
Expand Down
21 changes: 20 additions & 1 deletion app/views/admins/surveys/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<%= form_with model: @survey, url: admin_study_surveys_path(@study) do |f| %>
<% url = @survey.persisted? ? admin_study_survey_path(@study, @survey) : admin_study_surveys_path(@study) %>
<%= form_with model: @survey, url: url do |f| %>
<div class="space-y-4">
<%= render Forms::ErrorsComponent.new(errors: @survey.errors.full_messages) %>
<%= render Forms::TextFieldComponent.new(form: f, method: :name, autofocus: true) %>
<%= render Forms::CheckboxComponent.new(form: f, method: :available) %>
<%= render Forms::CheckboxComponent.new(form: f, method: :show_on_map) %>
<%= render Forms::CheckboxComponent.new(form: f, method: :show_on_stops) %>

<div>
<%= render Forms::TextFieldComponent.new(form: f, method: :visible_stop_list) %>
<div class='text-gray-500 text-sm'>
Optional, comma separated list of stop IDs that this survey should be visible on. When blank, it implies visibility on all stops.
</div>
</div>

<div>
<%= render Forms::TextFieldComponent.new(form: f, method: :visible_route_list) %>
<div class='text-gray-500 text-sm'>
Optional, comma separated list of route IDs that this survey should be visible on. When blank, it implies visibility on all routes.
</div>
</div>

<%= render Forms::ButtonBarComponent.new(f) %>
</div>
<% end %>
102 changes: 62 additions & 40 deletions app/views/admins/surveys/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@
<% title @survey.name %>
<% back_link "Back to Study", admin_study_path(@survey.study) %>
<%= render 'admins/survey_tabs' %>

<div class="space-y-4">
<div>
<%= link_to "New Text Field", new_admin_study_survey_question_path(@survey.study, @survey, content: 'text'), class: 'oba-btn' %>
<%= link_to "New Label", new_admin_study_survey_question_path(@survey.study, @survey, content: 'label'), class: 'oba-btn' %>
<%= link_to "New Radio List", new_admin_study_survey_question_path(@survey.study, @survey, content: 'radio'), class: 'oba-btn' %>
<%= link_to "New Checkbox List", new_admin_study_survey_question_path(@survey.study, @survey, content: 'checkbox'), class: 'oba-btn' %>
<%= link_to "New External Survey", new_admin_study_survey_question_path(@survey.study, @survey, content: 'external_survey'), class: 'oba-btn' %>
<div class='flex flex-col md:flex-row-reverse space-y-4 md:space-y-0 md:gap-x-2'>
<div class='md:w-72 bg-neutral-50 rounded-md p-2'>
<div class='flex mb-2'>
<h2 class="h2">Settings</h2>
<div class='flex-1 text-right text-sm self-center'>
<%= link_to "Edit", edit_admin_study_survey_path(@survey.study, @survey), class: "oba-btn oba-btn--sm" %>
</div>
</div>

<div class='text-sm'>
<%= render Data::DataListComponent.new(
[
{ name: "Name", value: @survey.name },
{ name: "Show on Map", value: @survey.show_on_map? ? "Yes" : "No" },
{ name: "Show on Stops", value: @survey.show_on_stops? ? "Yes" : "No" },
{ name: "Visible stop list", value: @survey.visible_stop_list },
{ name: "Visible route list", value: @survey.visible_route_list },
]
) %>
</div>
</div>

<% if @survey.questions.blank? %>
<%= render Containers::EmptyStateComponent.new(
title: "No Survey Questions",
description: "Add survey questions to this survey to collect data from participants."
) %>
<% else %>
<div
class="flex flex-col gap-y-4"
data-controller="sortable"
data-sortable-resource-name-value="question"
data-sortable-response-kind-value="json"
data-sortable-animation-value="150"
data-sortable-handle-value=".handle">
<% @survey.questions.each do |sq| %>
<div
data-sortable-update-url="<%= admin_study_survey_question_path(@survey.study, @survey, sq) %>"
class="border bg-slate-50 p-2">
<div class="flex">
<div class="handle self-center cursor-move">
<%= render("icons/grip_vertical_solid", classes: "w-4 h-4 fill-gray-500") %>
</div>
<div class="flex-1">
<h3 class="h3">Q<%= sq.position %>: <%= sq.content.type.titleize %></h3>
</div>
<div>
<%= link_to "Edit", edit_admin_study_survey_question_path(@survey.study, @survey, sq), class: "link text-sm" %>
<div class="space-y-4 flex-1">
<div class='flex gap-x-2'>
<%= link_to "New Text Field", new_admin_study_survey_question_path(@survey.study, @survey, content: 'text'), class: 'oba-btn' %>
<%= link_to "New Label", new_admin_study_survey_question_path(@survey.study, @survey, content: 'label'), class: 'oba-btn' %>
<%= link_to "New Radio List", new_admin_study_survey_question_path(@survey.study, @survey, content: 'radio'), class: 'oba-btn' %>
<%= link_to "New Checkbox List", new_admin_study_survey_question_path(@survey.study, @survey, content: 'checkbox'), class: 'oba-btn' %>
<%= link_to "New External Survey", new_admin_study_survey_question_path(@survey.study, @survey, content: 'external_survey'), class: 'oba-btn' %>
</div>

<% if @survey.questions.blank? %>
<%= render Containers::EmptyStateComponent.new(
title: "No Survey Questions",
description: "Add survey questions to this survey to collect data from participants."
) %>
<% else %>
<div
class="flex flex-col gap-y-4"
data-controller="sortable"
data-sortable-resource-name-value="question"
data-sortable-response-kind-value="json"
data-sortable-animation-value="150"
data-sortable-handle-value=".handle">
<% @survey.questions.each do |sq| %>
<div
data-sortable-update-url="<%= admin_study_survey_question_path(@survey.study, @survey, sq) %>"
class="border bg-slate-50 p-2">
<div class="flex">
<div class="handle self-center cursor-move">
<%= render("icons/grip_vertical_solid", classes: "w-4 h-4 fill-gray-500") %>
</div>
<div class="flex-1">
<h3 class="h3">Q<%= sq.position %>: <%= sq.content.type.titleize %></h3>
</div>
<div>
<%= link_to "Edit", edit_admin_study_survey_question_path(@survey.study, @survey, sq), class: "link text-sm" %>
</div>
</div>
<%= render partial_for_field_preview(sq), question: sq %>
</div>
<%= render partial_for_field_preview(sq), question: sq %>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
</div>
14 changes: 13 additions & 1 deletion app/views/api/v1/surveys/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
json.surveys do
json.array! @surveys do |survey|
json.extract! survey, :id, :name, :created_at
json.extract! survey, :id, :name, :created_at, :updated_at, :show_on_map, :show_on_stops

if survey.visible_stop_list.blank?
json.visible_stop_list nil
else
json.visible_stop_list survey.visible_stop_list&.split(',')&.map(&:strip)
end

if survey.visible_route_list.blank?
json.visible_route_list nil
else
json.visible_route_list survey.visible_route_list&.split(',')&.map(&:strip)
end

json.study do
json.extract! survey.study, :id, :name, :description
Expand Down

0 comments on commit 4f664ef

Please sign in to comment.