Skip to content
Draft
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
4 changes: 2 additions & 2 deletions app/controllers/community_news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create
end
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Community news create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand All @@ -95,7 +95,7 @@ def update
assign_associations(@community_news)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Community news update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
49 changes: 21 additions & 28 deletions app/controllers/event_registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,15 @@ def create
if @event_registration.save
respond_to do |format|
format.html {
if current_user.super_user?
return_to = params.dig(:event_registration, :event_id).present? ? "manage" : "ticket"
redirect_to confirm_event_registration_path(@event_registration, return_to: return_to)
elsif params.dig(:event_registration, :event_id).present?
redirect_to manage_event_path(@event_registration.event),
notice: "Registration created."
else
redirect_to registration_ticket_path(@event_registration.slug),
notice: "Registration created."
end
redirect_to confirm_event_registration_path(@event_registration, return_to: params[:return_to])
}
end
else
respond_to do |format|
format.html {
if @event_registration.event_id.present?
redirect_to manage_event_path(@event_registration.event),
alert: @event_registration.errors.full_messages.to_sentence
else
redirect_to event_registrations_path,
alert: @event_registration.errors.full_messages.to_sentence
case params[:return_to]
when "manage" then redirect_to manage_event_path(@event_registration.event), alert: @event_registration.errors.full_messages.to_sentence
else redirect_to event_registrations_path, alert: @event_registration.errors.full_messages.to_sentence
end
}
end
Expand All @@ -93,10 +81,10 @@ def update
respond_to do |format|
format.turbo_stream
format.html {
if params[:return_to] == "manage"
redirect_to manage_event_path(@event_registration.event), notice: "Registration was successfully updated.", status: :see_other
else
redirect_to registration_ticket_path(@event_registration.slug), notice: "Registration was successfully updated.", status: :see_other
case params[:return_to]
when "manage" then redirect_to manage_event_path(@event_registration.event), notice: "Registration was successfully updated.", status: :see_other
when "index" then redirect_to event_registrations_path, notice: "Registration was successfully updated.", status: :see_other
else redirect_to registration_ticket_path(@event_registration.slug), notice: "Registration was successfully updated.", status: :see_other
end
}
end
Expand Down Expand Up @@ -133,22 +121,26 @@ def process_confirm
current_user: current_user
)

if params[:return_to] == "manage"
redirect_to manage_event_path(@event_registration.event), notice: result.summary
else
redirect_to registration_ticket_path(@event_registration.slug), notice: result.summary
case params[:return_to]
when "manage" then redirect_to manage_event_path(@event_registration.event), notice: result.summary
when "index" then redirect_to event_registrations_path, notice: result.summary
else redirect_to registration_ticket_path(@event_registration.slug), notice: result.summary
end
end

def destroy
authorize! @event_registration
event = @event_registration.event
if @event_registration.destroy
flash[:notice] = "Registration deleted."

else
flash[:alert] = @event_registration.errors.full_messages.to_sentence
end
redirect_to event_registrations_path

case params[:return_to]
when "manage" then redirect_to manage_event_path(event)
else redirect_to event_registrations_path
end
end

# Optional hooks for setting variables for forms or index
Expand All @@ -167,14 +159,15 @@ def set_form_variables
private

def set_event_registration
@event_registration = EventRegistration.includes(:registrant, { event: [ :location, :event_forms ] }, comments: [ :created_by, :updated_by ]).find(params[:id])
@event_registration = EventRegistration.includes({ registrant: { affiliations: :organization } }, { event: [ :location, :event_forms ] }, :organizations, comments: [ :created_by, :updated_by ]).find(params[:id])
end

# Strong parameters
def event_registration_params
params.require(:event_registration).permit(
:event_id, :registrant_id, :status,
:scholarship_recipient, :scholarship_tasks_completed,
:scholarship_requested, :scholarship_recipient, :scholarship_tasks_completed,
organization_ids: [],
comments_attributes: [ :id, :body, :_destroy ]
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events/public_registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def validate_required_fields(form_params)

if field.number_integer? && value.to_s !~ /\A\d+\z/
errors[field.id] = "must be a whole number"
elsif field.field_key&.match?(/email(?!_type)/) && value.to_s !~ /\A[^@\s]+@[^@\s]+\z/
elsif field.field_key&.match?(/email(?!_type|_confirmation)/) && value.to_s !~ /\A[^@\s]+@[^@\s]+\z/
errors[field.id] = "must be a valid email address"
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def manage
authorize! @event, to: :manage?
@event = @event.decorate
scope = @event.event_registrations
.includes(:payments, :comments, registrant: [ :user, :contact_methods, { affiliations: :organization }, { avatar_attachment: :blob } ])
.includes(:payments, :comments, :organizations, registrant: [ :user, :contact_methods, { avatar_attachment: :blob } ])
.joins(:registrant)
scope = scope.keyword(params[:keyword]) if params[:keyword].present?
scope = scope.attendance_status(params[:attendance_status]) if params[:attendance_status].present?
Expand Down Expand Up @@ -75,7 +75,7 @@ def create
end
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Event create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down Expand Up @@ -105,7 +105,7 @@ def update
else
raise ActiveRecord::Rollback
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Event update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
141 changes: 141 additions & 0 deletions app/controllers/forms_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
class FormsController < ApplicationController
before_action :set_form, only: %i[show edit update destroy question_library add_questions]

def index
authorize!
@forms = authorized_scope(Form.all)
.includes(:form_fields, :events)
.order(:name)
.paginate(page: params[:page], per_page: 25)
end

def show
authorize! @form
@form_fields_by_group = grouped_fields
@linked_events = @form.events.order(start_date: :desc)
end

def new
authorize! Form.new
@builders = builder_options
end

def create
authorize! Form.new

@form = case params[:builder_type]
when "short_registration"
ShortEventRegistrationFormBuilder.build_standalone!
when "extended_registration"
ExtendedEventRegistrationFormBuilder.build_standalone!
when "scholarship_application"
ScholarshipApplicationFormBuilder.build_standalone!
when "generic"
Form.create!(name: params[:form_name].presence || "New Form")
else
Form.create!(name: "New Form")
end

redirect_to edit_form_path(@form), notice: "Form was successfully created. Customize it below."
end

def edit
authorize! @form
set_form_variables
end

def update
authorize! @form
if @form.update(form_params)
redirect_to @form, notice: "Form was successfully updated.", status: :see_other
else
set_form_variables
render :edit, status: :unprocessable_content
end
end

def destroy
authorize! @form
@form.destroy!
redirect_to forms_path, notice: "Form was successfully destroyed."
end

def question_library
authorize! @form
existing_keys = @form.form_fields.reorder(position: :asc).pluck(:field_key).compact
scope = FormField.unscoped
.where.not(form_id: @form.id)
.where.not(field_key: [ nil, "" ])
scope = scope.where.not(field_key: existing_keys) if existing_keys.any?
@available_fields = scope.order(:field_group, :position).group_by(&:field_group)
render layout: false
end

def add_questions
authorize! @form
source_field_ids = Array(params[:source_field_ids]).map(&:to_i)
max_position = @form.form_fields.reorder(position: :asc).maximum(:position) || 0

FormField.unscoped.where(id: source_field_ids).find_each do |source|
max_position += 1
new_field = @form.form_fields.create!(
question: source.question,
answer_type: source.answer_type,
answer_datatype: source.answer_datatype,
status: source.status,
position: max_position,
is_required: source.is_required,
instructional_hint: source.instructional_hint,
field_key: source.field_key,
field_group: source.field_group
)

source.form_field_answer_options.each do |ffao|
new_field.form_field_answer_options.create!(answer_option: ffao.answer_option)
end
end

redirect_to edit_form_path(@form), notice: "Questions added successfully."
end

private

def set_form
@form = Form.find(params[:id])
end

def set_form_variables
@form_fields_by_group = grouped_fields
@field_groups = @form.form_fields.reorder(position: :asc).pluck(:field_group).compact.uniq
@answer_type_options = FormField.answer_types.keys.map { |k| [ k.humanize, k ] }
end

def grouped_fields
@form.form_fields.reorder(position: :asc).group_by(&:field_group)
end

def builder_options
[
{ key: "short_registration", name: ShortEventRegistrationFormBuilder::FORM_NAME,
description: "Contact, consent, qualitative, and scholarship fields" },
{ key: "extended_registration", name: ExtendedEventRegistrationFormBuilder::FORM_NAME,
description: "Contact, background, professional, qualitative, scholarship, and payment fields" },
{ key: "scholarship_application", name: ScholarshipApplicationFormBuilder::FORM_NAME,
description: "Scholarship-specific fields" },
{ key: "generic", name: "Generic Form",
description: "Start with a blank form and add fields manually" }
]
end

def form_params
params.require(:form).permit(
:name,
:scholarship_application,
form_fields_attributes: [
:id, :question, :answer_type, :answer_datatype,
:status, :position, :is_required, :instructional_hint,
:field_key, :field_group, :_destroy
]
)
end
end
4 changes: 2 additions & 2 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def create
assign_associations(@resource)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Resource create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand All @@ -99,7 +99,7 @@ def update
assign_associations(@resource)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Resource update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def create
end
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Story create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand All @@ -101,7 +101,7 @@ def update
end
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Story update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/story_ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create
notification_type: 0)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "StoryIdea create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down Expand Up @@ -88,7 +88,7 @@ def update
assign_associations(@story_idea)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "StoryIdea update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/tutorials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create
assign_associations(@tutorial)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Tutorial create failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand All @@ -75,7 +75,7 @@ def update
assign_associations(@tutorial)
success = true
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
Rails.logger.error "Tutorial update failed: #{e.class} - #{e.message}"
raise ActiveRecord::Rollback
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def destroy
authorize! @user
@user.destroy!
redirect_to users_path, notice: "User was successfully destroyed."
rescue ActiveRecord::InvalidForeignKey
redirect_to @user, alert: "Unable to delete this user because they have associated records that cannot be removed."
end

# ---------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/workshop_ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def create
set_form_variables
render :new, status: :unprocessable_content
end
rescue ActiveRecord::RecordNotUnique => e
Rails.logger.error "WorkshopIdea create failed: #{e.class} - #{e.message}"
@workshop_idea.errors.add(:base, "could not be saved due to a conflict. Please try again.")
set_form_variables
render :new, status: :unprocessable_content
end

def edit
Expand All @@ -59,6 +64,11 @@ def update
set_form_variables
render :edit, status: :unprocessable_content
end
rescue ActiveRecord::RecordNotUnique => e
Rails.logger.error "WorkshopIdea update failed: #{e.class} - #{e.message}"
@workshop_idea.errors.add(:base, "could not be saved due to a conflict. Please try again.")
set_form_variables
render :edit, status: :unprocessable_content
end

def destroy
Expand Down
Loading
Loading