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 new require_stop_id_in_response property #159

Merged
merged 2 commits into from
Sep 4, 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
1 change: 1 addition & 0 deletions app/controllers/admins/surveys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def survey_params
:name,
:enabled,
:extra_data,
:require_stop_id_in_response,
:show_on_map,
:show_on_stops,
:visible_stop_list,
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/v1/survey_responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def create
@survey = Survey.find(params[:survey_id])
@survey_response = @survey.survey_responses.build
@survey_response.user_identifier = params[:user_identifier]
@survey_response.stop_identifier = params[:stop_identifier]

responses = JSON.parse(params[:responses]).map { |r| SurveyResponseContent.new(r) }
@survey_response.upsert_responses(responses)
Expand Down
1 change: 0 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module ApplicationHelper

def title(t = nil)
if t.nil?
@title
Expand Down
20 changes: 18 additions & 2 deletions app/models/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ class Survey < ApplicationRecord
jsonb_accessor(:extra_data, {
name: :string,

show_on_map: :boolean,
show_on_stops: :boolean,
show_on_map: [:boolean, default: false],
show_on_stops: [:boolean, default: false],
require_stop_id_in_response: [:boolean, default: false],
visible_stop_list: :string,
visible_route_list: :string
})

validates :name, presence: true

validate :require_stop_id_sensibility

# Survey Questions

has_many :questions, -> { order(position: :asc) }, dependent: :destroy

# Survey Responses

has_many :survey_responses, dependent: :destroy

private

def require_stop_id_sensibility
if require_stop_id_in_response && !show_on_stops && !errors.add(:require_stop_id_in_response, 'is only valid if Show on Stops is true')
return
end

return unless require_stop_id_in_response && show_on_map

errors.add(:require_stop_id_in_response, 'is only valid if Show on Map is false')

end
end
2 changes: 2 additions & 0 deletions app/models/survey_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class SurveyResponse < ApplicationRecord
# and leaking information about the number of responses.
validates :public_identifier, presence: true

validates :stop_identifier, presence: true, if: -> { survey.require_stop_id_in_response? }

after_initialize do
self.public_identifier ||= SecureRandom.hex(10)
end
Expand Down
1 change: 1 addition & 0 deletions app/views/admins/surveys/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<%= render Forms::CheckboxComponent.new(form: f, method: :show_on_map) %>
<%= render Forms::CheckboxComponent.new(form: f, method: :show_on_stops) %>
<%= render Forms::CheckboxComponent.new(form: f, method: :require_stop_id_in_response) %>

<div>
<%= render Forms::TextFieldComponent.new(form: f, method: :visible_stop_list) %>
Expand Down
1 change: 1 addition & 0 deletions app/views/admins/surveys/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{ 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: "Require stop ID in responses", value: @survey.require_stop_id_in_response? ? "Yes" : "No" },
{ name: "Visible stop list", value: @survey.visible_stop_list },
{ name: "Visible route list", value: @survey.visible_route_list },
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddStopIdentifierToSurveyResponses < ActiveRecord::Migration[7.1]
def change
add_column :survey_responses, :stop_identifier, :string
add_index :survey_responses, :stop_identifier
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_07_06_205804) do
ActiveRecord::Schema[7.1].define(version: 2024_09_04_160018) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -144,7 +144,9 @@
t.jsonb "responses", default: {}, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "stop_identifier"
t.index ["public_identifier"], name: "index_survey_responses_on_public_identifier", unique: true
t.index ["stop_identifier"], name: "index_survey_responses_on_stop_identifier"
t.index ["survey_id"], name: "index_survey_responses_on_survey_id"
t.index ["user_identifier"], name: "index_survey_responses_on_user_identifier"
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/survey_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
survey_response = build(:survey_response, public_identifier: nil)
expect(survey_response).not_to be_valid
end

describe 'survey.require_stop_id_in_response == true' do
let(:survey) { create(:survey, require_stop_id_in_response: true, show_on_stops: true) }
it 'is invalid without a stop_identifier' do
survey_response = build(:survey_response, survey:, stop_identifier: nil)
expect(survey_response).not_to be_valid
end

it 'is valid with a stop_identifier' do
survey_response = build(:survey_response, survey:, stop_identifier: '123')
expect(survey_response).to be_valid
end
end
end

describe '#upsert_responses' do
Expand Down
Loading