Skip to content

Commit

Permalink
Updated Form object for due validation
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin-jebaraj committed Dec 6, 2024
1 parent 2c4ed0c commit 7188cdd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
30 changes: 17 additions & 13 deletions app/controllers/support/cases/evaluation_due_dates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module Support
class Cases::EvaluationDueDatesController < Cases::ApplicationController
before_action :set_current_case
before_action { @back_url = support_case_path(@current_case, anchor: "tasklist") }

include HasDateParams
def edit; end

def update
@current_case.assign_attributes(evaluation_due_date_params)
@case_evaluation_due_date_form = CaseEvaluationDueDateForm.from_validation(validation)

if @current_case.valid?(:update_due_date_form)
if @current_case.save(context: :update_due_date_form)
redirect_to @back_url
else
render :edit
end
if validation.success?
current_case.update!(@case_evaluation_due_date_form.to_h)
redirect_to @back_url
else
add_errors_to_current_case(validation.errors.to_h)
render :edit
end
end
Expand All @@ -24,18 +24,22 @@ def set_current_case
@current_case = Support::Case.find(params[:case_id])
end

def validation
@validation ||= CaseEvaluationDueDateFormSchema.new.call(**evaluation_due_date_params)
end

def evaluation_due_date_params
form_params = params.require(:support_case).permit
form_params[:evaluation_due_date] = date_param(:support_case, :evaluation_due_date)
form_params
end

def date_param(form_param, date_field)
date = params.fetch(form_param, {}).permit("#{date_field}(1i)", "#{date_field}(2i)", "#{date_field}(3i)")
begin
Date.new(date["#{date_field}(1i)"].to_i, date["#{date_field}(2i)"].to_i, date["#{date_field}(3i)"].to_i)
rescue StandardError
nil
def add_errors_to_current_case(errors)
errors.each do |attribute, messages|
messages.each do |message|
@current_case.errors.add(attribute, message)
@current_case[attribute] = nil
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/forms/support/case_evaluation_due_date_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Support
class CaseEvaluationDueDateForm < Form
option :evaluation_due_date, Types::DateField
end
end
30 changes: 30 additions & 0 deletions app/forms/support/case_evaluation_due_date_form_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Support
class CaseEvaluationDueDateFormSchema < ::Support::Schema
config.messages.top_namespace = :case_evaluation_due_date_form

params do
required(:evaluation_due_date).value(:hash)
end

rule :evaluation_due_date do
if value.blank?
key.failure(:missing)
end
end

rule :evaluation_due_date do
if value.present? && !hash_to_date.call(value)
key.failure(:invalid)
end
end

rule :evaluation_due_date do
if value.present?
date = hash_to_date.call(value)
if date && date <= Time.zone.today
key.failure(:must_be_in_the_future)
end
end
end
end
end
5 changes: 0 additions & 5 deletions config/locales/validation/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ en:
blank: Enter provider end date
unique_name_and_provider: The combination of name and provider must be unique

support/case:
attributes:
evaluation_due_date:
blank: Enter valid evaluation due date
evaluation_due_date_must_be_in_the_future: Evaluation due date must be in the future

request:
rules:
Expand Down
9 changes: 9 additions & 0 deletions config/locales/validation/support/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,13 @@ en:
file_attachments:
infected: One or more of the files you uploaded contained a virus and have been rejected
incorrect_file_type: One or more of the files you uploaded was an incorrect file type
case_evaluation_due_date_form:
rules:
evaluation_due_date: Evaluation due date
errors:
rules:
evaluation_due_date:
missing: Enter valid evaluation due date
invalid: Enter valid evaluation due date
must_be_in_the_future: Evaluation due date must be in the future

0 comments on commit 7188cdd

Please sign in to comment.