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

Users can add people with relevant convictions #104

Merged
merged 8 commits into from
Apr 3, 2018
30 changes: 29 additions & 1 deletion app/controllers/conviction_details_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ def new
end

def create
super(ConvictionDetailsForm, "conviction_details_form")
if params[:commit] == I18n.t("conviction_details_forms.new.add_person_link")
submit_and_add_another
else
super(ConvictionDetailsForm, "conviction_details_form")
end
end

def submit_and_add_another
return unless set_up_form(ConvictionDetailsForm, "conviction_details_form", params["conviction_details_form"][:reg_identifier])

respond_to do |format|
if @conviction_details_form.submit(params["conviction_details_form"])
format.html { redirect_to_correct_form }
else
format.html { render :new }
end
end
end

def delete_person
return unless set_up_form(ConvictionDetailsForm, "conviction_details_form", params[:reg_identifier])

respond_to do |format|
# Check if there are any matches first, to avoid a Mongoid error
people_with_id = @transient_registration.keyPeople.where(id: params[:id])
people_with_id.first.delete if people_with_id.any?

format.html { redirect_to_correct_form }
end
end
end
102 changes: 97 additions & 5 deletions app/forms/conviction_details_form.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,109 @@
class ConvictionDetailsForm < BaseForm
# TODO: Define accessible attributes, eg attr_accessor :field
attr_accessor :first_name, :last_name, :position, :dob_day, :dob_month, :dob_year, :relevant_person, :date_of_birth

def initialize(transient_registration)
super
# TODO: Define params to get from transient_registration, eg self.field = @transient_registration.field
end

def submit(params)
# Assign the params for validation and pass them to the BaseForm method for updating
# TODO: Define allowed params, eg self.field = params[:field]
# TODO: Include attributes to update in the attributes hash, eg { field: field }
attributes = {}
self.first_name = params[:first_name]
self.last_name = params[:last_name]
self.position = params[:positon]
process_date_fields(params)

self.relevant_person = add_relevant_person
self.date_of_birth = relevant_person.date_of_birth

attributes = if fields_have_content?
{ keyPeople: all_people }
else
{}
end

super(attributes, params[:reg_identifier])
end

validates_with KeyPeopleValidator
validate :old_enough?

def maximum_key_people
nil
end

def minimum_key_people
1
end

def number_of_existing_key_people
@transient_registration.relevant_conviction_people.count
end

def enough_key_people?
return false if number_of_existing_key_people < minimum_key_people
true
end

def fields_have_content?
fields = [first_name, last_name, dob_day, dob_month, dob_year]
fields.each do |field|
return true if field.present? && field.to_s.length.positive?
end
false
end

private

def process_date_fields(params)
self.dob_day = format_date_field_value(params[:dob_day])
self.dob_month = format_date_field_value(params[:dob_month])
self.dob_year = format_date_field_value(params[:dob_year])
end

# If we can make the date fields positive integers, use those integers
# Otherwise, return nil
def format_date_field_value(value)
# If this isn't a valid integer, .to_i returns 0
integer_value = value.to_i
return integer_value if integer_value.positive?
end

def add_relevant_person
KeyPerson.new(first_name: first_name,
last_name: last_name,
position: position,
dob_day: dob_day,
dob_month: dob_month,
dob_year: dob_year,
person_type: "relevant")
end

def all_people
list_of_people_to_keep << relevant_person
end

# Adding the new person directly to @transient_registration.keyPeople immediately updates the object,
# regardless of validation. So instead we copy all existing people into a new array and modify that.
def list_of_people_to_keep
people = []

@transient_registration.keyPeople.each do |person|
# We need to copy the person before adding to the array to avoid a 'conflicting modifications' Mongo error (10151)
people << person.clone
end

people
end

def old_enough?
return false unless date_of_birth.present?

age_cutoff_date = (Date.today - 17.years) + 1.day

return true if date_of_birth < age_cutoff_date

error_message = "age_limit".to_sym
errors.add(:date_of_birth, error_message)
false
end
end
46 changes: 29 additions & 17 deletions app/forms/key_people_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(transient_registration)
self.business_type = @transient_registration.business_type

# If there's only one key person, we can pre-fill the fields so users can easily edit them
prefill_form if can_only_have_one_key_person? && @transient_registration.keyPeople.present?
prefill_form if can_only_have_one_key_person? && @transient_registration.key_people.present?
end

def submit(params)
Expand All @@ -21,7 +21,7 @@ def submit(params)
self.date_of_birth = key_person.date_of_birth

attributes = if fields_have_content?
{ keyPeople: all_key_people }
{ keyPeople: all_people }
else
{}
end
Expand All @@ -44,7 +44,7 @@ def minimum_key_people
end

def number_of_existing_key_people
@transient_registration.keyPeople.count
@transient_registration.key_people.count
end

def can_only_have_one_key_person?
Expand All @@ -68,11 +68,11 @@ def fields_have_content?
private

def prefill_form
self.first_name = @transient_registration.keyPeople.first.first_name
self.last_name = @transient_registration.keyPeople.first.last_name
self.dob_day = @transient_registration.keyPeople.first.dob_day
self.dob_month = @transient_registration.keyPeople.first.dob_month
self.dob_year = @transient_registration.keyPeople.first.dob_year
self.first_name = @transient_registration.key_people.first.first_name
self.last_name = @transient_registration.key_people.first.last_name
self.dob_day = @transient_registration.key_people.first.dob_day
self.dob_month = @transient_registration.key_people.first.dob_month
self.dob_year = @transient_registration.key_people.first.dob_year
end

def process_date_fields(params)
Expand All @@ -98,17 +98,29 @@ def add_key_person
person_type: "key")
end

def all_key_people
# If there's only one key person allowed, just replace existing data
return [key_person] if can_only_have_one_key_person?
def all_people
list_of_people_to_keep << key_person
end

existing_key_people = []
# Adding the new key person directly to @transient_registration.keyPeople immediately updates the object,
# regardless of validation. So instead we copy the existing key people into a new array and modify that.
@transient_registration.keyPeople.each do |person|
existing_key_people << person
# Adding the new key person directly to @transient_registration.keyPeople immediately updates the object,
# regardless of validation. So instead we copy all existing people into a new array and modify that.
def list_of_people_to_keep
people = []

# If there's only one key person allowed, we want to discard any existing key people, but keep people with
# relevant convictions. Otherwise, we copy all the keyPeople, regardless of type.
existing_people = if can_only_have_one_key_person?
@transient_registration.relevant_conviction_people
else
@transient_registration.keyPeople
end

existing_people.each do |person|
# We need to copy the person before adding to the array to avoid a 'conflicting modifications' Mongo error (10151)
people << person.clone
end
existing_key_people << key_person

people
end

def key_people_limits
Expand Down
10 changes: 10 additions & 0 deletions app/models/concerns/can_have_registration_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ def registered_address
def overseas?
location == "overseas"
end

def key_people
return [] unless keyPeople.present?
keyPeople.where(person_type: "key")
end

def relevant_conviction_people
return [] unless keyPeople.present?
keyPeople.where(person_type: "relevant")
end
end
end
112 changes: 102 additions & 10 deletions app/views/conviction_details_forms/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,116 @@
<%= render("shared/back", back_path: back_conviction_details_forms_path(@conviction_details_form.reg_identifier)) %>

<% if @conviction_details_form.errors.any? %>
<div class="grid-row">
<div class="column-two-thirds">
<%= form_for(@conviction_details_form) do |f| %>
<%= render("shared/errors", object: @conviction_details_form) %>

<h1 class="heading-large"><%= t(".error_heading") %></h1>
<h1 class="heading-large"><%= t(".heading") %></h1>

<% @conviction_details_form.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<% if @conviction_details_form.errors[:first_name].any? %>
<div class="form-group form-group-error">
<% else %>
<div class="form-group">
<% end %>
<fieldset id="first_name">
<% if @conviction_details_form.errors[:first_name].any? %>
<span class="error-message"><%= @conviction_details_form.errors[:first_name].join(", ") %></span>
<% end %>

<% else %>
<%= f.label :first_name, t(".first_name"), class: "form-label" %>
<%= f.text_field :first_name, value: @conviction_details_form.first_name, class: "form-control" %>
</fieldset>
</div>

<%= form_for(@conviction_details_form) do |f| %>
<%= render("shared/errors", object: @conviction_details_form) %>
<% if @conviction_details_form.errors[:last_name].any? %>
<div class="form-group form-group-error">
<% else %>
<div class="form-group">
<% end %>
<fieldset id="last_name">
<% if @conviction_details_form.errors[:last_name].any? %>
<span class="error-message"><%= @conviction_details_form.errors[:last_name].join(", ") %></span>
<% end %>

<h1 class="heading-large"><%= t(".heading") %></h1>
<%= f.label :last_name, t(".last_name"), class: "form-label" %>
<%= f.text_field :last_name, value: @conviction_details_form.last_name, class: "form-control" %>
</fieldset>
</div>

<% if @conviction_details_form.errors[:position].any? %>
<div class="form-group form-group-error">
<% else %>
<div class="form-group">
<% end %>
<fieldset id="position">
<% if @conviction_details_form.errors[:position].any? %>
<span class="error-message"><%= @conviction_details_form.errors[:position].join(", ") %></span>
<% end %>

<%= f.label :position, t(".position"), class: "form-label" %>
<%= f.text_field :position, value: @conviction_details_form.position, class: "form-control" %>
</fieldset>
</div>

<% if @conviction_details_form.errors[:date_of_birth].any? %>
<div class="form-group form-group-error">
<% else %>
<div class="form-group">
<% end %>
<fieldset id="date_of_birth">
<% if @conviction_details_form.errors[:date_of_birth].any? %>
<span class="error-message"><%= @conviction_details_form.errors[:date_of_birth].join(". ") %></span>
<% end %>

<legend>
<%= t(".date_of_birth") %>
<span class="form-hint"><%= t(".date_of_birth_hint") %></span>
</legend>

<fieldset id="dob_day" class="inline-date">
<%= f.label :dob_day, t(".dob_day"), class: "form-label" %>
<%= f.text_field :dob_day, value: @conviction_details_form.dob_day, class: "form-control" %>
</fieldset>

<fieldset id="dob_month" class="inline-date">
<%= f.label :dob_month, t(".dob_month"), class: "form-label" %>
<%= f.text_field :dob_month, value: @conviction_details_form.dob_month, class: "form-control" %>
</fieldset>

<fieldset id="dob_year" class="inline-date">
<%= f.label :dob_year, t(".dob_year"), class: "form-label" %>
<%= f.text_field :dob_year, value: @conviction_details_form.dob_year, class: "form-control" %>
</fieldset>

</fieldset>
</div>

<%= f.hidden_field :reg_identifier, value: @conviction_details_form.reg_identifier %>

<div class="form-group">
<%= f.submit t(".add_person_link"), class: "button-link" %>
</div>
<div class="form-group">
<%= f.submit t(".next_button"), class: "button" %>
</div>
<% end %>

<% end %>
<% if @conviction_details_form.number_of_existing_key_people > 0 %>
</div>
<div class="column-one-third">
<h2 class="heading-small"><%= t(".list_of_people") %></h2>
<% @transient_registration.relevant_conviction_people.each do |person| %>
<ul class="key-person-list">
<li>
<%= person.first_name %> <%= person.last_name %>
<%= button_to(t(".delete_person_link"),
delete_person_conviction_details_forms_path(person.id),
class: "button-link",
method: :delete,
params: { reg_identifier: @conviction_details_form.reg_identifier }) %>
</li>
</ul>
<% end %>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/key_people_forms/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
</div>
<div class="column-one-third">
<h2 class="heading-small"><%= t(".list_of_people") %></h2>
<% @transient_registration.keyPeople.each do |person| %>
<% @transient_registration.key_people.each do |person| %>
<ul class="key-person-list">
<li>
<%= person.first_name %> <%= person.last_name %>
Expand Down
Loading