Skip to content

Commit

Permalink
Merge pull request #148 from OneBusAway/location
Browse files Browse the repository at this point in the history
Location
  • Loading branch information
aaronbrethorst committed Aug 9, 2024
2 parents 188b453 + 4f664ef commit 3ec2711
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 65 deletions.
8 changes: 8 additions & 0 deletions app/components/data/data_list_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<% @items.each do |i| %>
<div class='border-b pb-1 mb-2'>
<strong><%= i[:name] %></strong>
<%= i[:value] %>
</div>
<% end %>
</div>
8 changes: 8 additions & 0 deletions app/components/data/data_list_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Data::DataListComponent < ViewComponent::Base
def initialize(items)
super()
@items = items
end
end
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
11 changes: 3 additions & 8 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ApplicationHelper

def title(t = nil)
if t.nil?
@title
Expand All @@ -8,11 +8,6 @@ def title(t = nil)
end
end

def back_link(title, path)
@back_link_title = title
@back_link_path = path
end

def severity_to_string(sev)
case sev
when 2
Expand All @@ -23,9 +18,9 @@ def severity_to_string(sev)
return "Severe"
else
return "Unknown"
end
end
end

def value_or_blank_rep(val)
if val.blank?
"—"
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/layouts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ def parent_layout(layout)
output = render(template: File.join("layouts", layout))
self.output_buffer = ActionView::OutputBuffer.new(output)
end

def back_link(title, path)
@back_link_title = title
@back_link_path = path
end

def sub_tab(name:, path:)
@sub_tabs ||= []
@sub_tabs << { name:, path: }
end
end
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
6 changes: 2 additions & 4 deletions app/views/admins/_survey_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<%= render Navigation::TabsComponent.new([
{ name: "Designer", path: admin_study_survey_path(@survey.study, @survey) },
{ name: "Responses", path: admin_study_survey_survey_responses_path(@survey.study, @survey) }
]) %>
<% sub_tab(name: "Designer", path: admin_study_survey_path(@survey.study, @survey)) %>
<% sub_tab(name: "Responses", path: admin_study_survey_survey_responses_path(@survey.study, @survey)) %>
10 changes: 8 additions & 2 deletions app/views/admins/questions/fields/_external_survey.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
option_name: "embedded_data_fields",
options: form.object.embedded_data_fields
) %>
<p class="text-sm text-gray-500">Valid values for embedded data are: <code>region_id</code>, <code>route_id</code>, <code>stop_id</code>, <code>user_id</code>.</p>
<p class="text-sm text-gray-500">
Valid values for embedded data are:
<code>current_location</code>,
<code>region_id</code>,
<code>route_id</code>,
<code>stop_id</code>,
<code>user_id</code>
</p>
</div>

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
16 changes: 9 additions & 7 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
<% end %>

<div class="py-5 space-y-4">
<header>
<div class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<% if @back_link_title.present? && @back_link_path.present? %>
<%= render Navigation::BackLinkComponent.new(title: @back_link_title, link: @back_link_path) %>
<% end %>
<h1 class="text-3xl font-bold leading-tight tracking-tight text-gray-900"><%= @title %></h1>
</div>
<header class="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<% if @back_link_title.present? && @back_link_path.present? %>
<%= render Navigation::BackLinkComponent.new(title: @back_link_title, link: @back_link_path) %>
<% end %>
<h1 class="text-3xl font-bold leading-tight tracking-tight text-gray-900"><%= @title %></h1>

<% if defined?(@sub_tabs) && @sub_tabs.count > 0 %>
<%= render Navigation::TabsComponent.new(@sub_tabs) %>
<% end %>
</header>
<main>
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
Expand Down
13 changes: 13 additions & 0 deletions spec/components/data/data_list_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Data::DataListComponent, type: :component do
it "renders something useful" do
expect(
render_inline(described_class.new([{ name: "Hello world", value: "Foo bar" }])).to_html
).to include(
"Hello world"
)
end
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'rspec/rails'

# Add additional requires below this line. Rails is not loaded until this point!
require "view_component/test_helpers"

Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }

Expand All @@ -30,6 +31,7 @@
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!

config.include ViewComponent::TestHelpers, type: :component
config.include ApiHelper, type: :request
config.include RequestHelper, type: :request
config.include ModelsHelper
Expand Down

0 comments on commit 3ec2711

Please sign in to comment.