Skip to content
Open
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
30 changes: 17 additions & 13 deletions app/controllers/maneuver_participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def show
end

def new
@maneuver = Maneuver.find_by name: params.require(:maneuver)
@participant = Participant.find_by number: params.require(:participant)
@maneuver = Maneuver.find_by name: params.expect(:maneuver)
@participant = Participant.find_by number: params.expect(:participant)
@page_title = "Judging #{@maneuver.name}"
@page_subtitle = @participant.display_information(:name, :number, :bus)

Expand All @@ -29,8 +29,8 @@ def new
def create
unless @participant.completed? @maneuver
attrs = { maneuver: @maneuver, participant: @participant }
attrs.merge! params.permit(:reversed_direction, :speed_achieved,
:made_additional_stops, :completed_as_designed)
attrs.merge! maneuver_participant_params.permit(:reversed_direction, :speed_achieved,
:made_additional_stops, :completed_as_designed)
attrs[:obstacles_hit] = parse_obstacles
attrs[:distances_achieved] = parse_distance_targets
ManeuverParticipant.create! attrs
Expand All @@ -39,8 +39,8 @@ def create
end

def update
attrs = params.permit(:reversed_direction, :speed_achieved,
:made_additional_stops, :completed_as_designed).to_hash
attrs = maneuver_participant_params.permit(:reversed_direction, :speed_achieved,
:made_additional_stops, :completed_as_designed).to_hash
attrs[:obstacles_hit] = parse_obstacles
attrs[:distances_achieved] = parse_distance_targets
@record.update! attrs
Expand All @@ -50,23 +50,27 @@ def update

private

def maneuver_participant_params
params.expect(maneuver_participant: {})
end

def find_maneuver_and_participant
@maneuver = Maneuver.find_by id: params.require(:maneuver_id)
@participant = Participant.find_by id: params.require(:participant_id)
@maneuver = Maneuver.find params.expect(:maneuver_id)
@participant = Participant.find params.expect(:participant_id)
end

def find_record
@record = ManeuverParticipant.find_by id: params.require(:id)
@record = ManeuverParticipant.find params.expect(:id)
end

def parse_obstacles
obstacles_hit = {}
obstacle_params = params.select do |key, value|
obstacle_params = maneuver_participant_params.select do |key, value|
key.starts_with?('obstacle') && value.to_i != 0
end

obstacle_params.each do |key, value|
obstacle = Obstacle.find_by id: key.split('_').last.to_i
obstacle = Obstacle.find key.split('_').last.to_i
obstacles_hit[obstacle.id] = [obstacle.point_value, value.to_i] if obstacle.present?
end

Expand All @@ -75,12 +79,12 @@ def parse_obstacles

def parse_distance_targets
distances_achieved = {}
target_params = params.select do |key, _value|
target_params = maneuver_participant_params.select do |key, _value|
key.starts_with?('target')
end

target_params.each do |key, value|
target = DistanceTarget.find_by id: key.split('_').last.to_i
target = DistanceTarget.find key.split('_').last.to_i
distances_achieved[[target.minimum, target.multiplier]] = value.to_i if target.present?
end

Expand Down
32 changes: 32 additions & 0 deletions app/views/maneuver_participants/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
= form_with model: maneuver_participant do |f|
= hidden_field_tag :maneuver_id, maneuver.id
= hidden_field_tag :participant_id, participant.id
- maneuver.grouped_obstacles.each do |(point_value, type), obstacles|
.form-group
= f.label :"obstacle_#{obstacles.first.id}", "#{point_value}-point #{type.pluralize} touched"
- value = maneuver_participant.obstacles_hit[obstacles.first.id].try(:[], 1) || 0
= render 'shared/with_increment_buttons' do
= f.number_field :"obstacle_#{obstacles.first.id}", { value:, in: 0..obstacles.count }
- maneuver.distance_targets.each do |target|
.form-group
= f.label "target_#{target.id}", "#{target.interval_type.capitalize} from #{target.name}"
- value = maneuver_participant.distances_achieved[[target.minimum, target.multiplier]] || 0
= render 'shared/with_increment_buttons' do
= f.number_field "target_#{target.id}", value:, min: 0
.form-group
= f.label :reversed_direction, maneuver.name.include?('Reverse') ? 'Extra reverses' : 'Reverses'
- value = maneuver_participant.reversed_direction || 0
= render 'shared/with_increment_buttons' do
= f.number_field :reversed_direction, value:, min: 0
- if maneuver.speed_target.present?
.form-group.form-check
= f.checkbox :speed_achieved, class: 'form-check-input'
= f.label :speed_achieved, "Achieved #{maneuver.speed_target} mph", class: 'form-check-label'
- if maneuver.counts_additional_stops?
.form-group
= f.checkbox :made_additional_stops, class: 'form-check-input'
= f.label :made_additional_stops, 'Extra stops?', class: 'form-check-label'
.form-group.form-check
= f.checkbox :completed_as_designed, class: 'form-check-input'
= f.label :completed_as_designed, 'Completed as designed?', class: 'form-check-label'
= submit_tag submit_text, class: 'btn btn-primary submit'
45 changes: 8 additions & 37 deletions app/views/maneuver_participants/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
.scoring
= form_tag maneuver_participants_path, id: 'score_form' do
= hidden_field_tag :maneuver_id, @maneuver.id
= hidden_field_tag :participant_id, @participant.id
- @maneuver.grouped_obstacles.each do |(point_value, type), obstacles|
.form-group
= label_tag :"obstacle_#{obstacles.first.id}", "#{point_value}-point #{type.pluralize} touched"
= render 'shared/with_increment_buttons' do
= number_field_tag "obstacle_#{obstacles.first.id}", 0, in: 0..obstacles.count
- @maneuver.distance_targets.each do |target|
.form-group
= label_tag :"target_#{target.id}", "#{target.interval_type.capitalize} from #{target.name}"
= render 'shared/with_increment_buttons' do
= number_field_tag "target_#{target.id}", target.minimum, min: 0
.form-group
= label_tag :reversed_direction, @maneuver.name.include?('Reverse') ? 'Extra reverses' : 'Reverses'
= render 'shared/with_increment_buttons' do
= number_field_tag :reversed_direction, 0, min: 0
- if @maneuver.speed_target.present?
.form-group.form-check
= checkbox_tag :speed_achieved, true, class: 'form-check-input'
= hidden_field_tag :speed_achieved, false
= label_tag :speed_achieved, "Achieved #{@maneuver.speed_target} mph", class: 'form-check-label'
- if @maneuver.counts_additional_stops?
.form-group
= checkbox_tag :made_additional_stops, false, class: 'form-check-input'
= hidden_field_tag :made_additional_stops, false
= label_tag :made_additional_stops, 'Extra stops?', class: 'form-check-label'
.form-group.form-check
= checkbox_tag :completed_as_designed, true, class: 'form-check-input'
= hidden_field_tag :completed_as_designed, false
= label_tag :completed_as_designed, 'Completed as designed?', class: 'form-check-label'
.form-group
= submit_tag 'Save & next', class: 'btn btn-primary submit'
= link_to previous_participant_maneuver_path(@maneuver) do
%button.btn.btn-secondary.actions{ type: :button }
%i.fa-solid.fa-arrow-left
%span.d-none.d-sm-inline Previous participant
= render partial: 'form', locals: { maneuver_participant: @record,
maneuver: @maneuver,
participant: @participant,
submit_text: 'Save & next' }
= link_to previous_participant_maneuver_path(@maneuver) do
%button.btn.btn-secondary.actions{ type: :button }
%i.fa-solid.fa-arrow-left
%span.d-none.d-sm-inline Previous participant
57 changes: 12 additions & 45 deletions app/views/maneuver_participants/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,48 +1,15 @@
.scoring
- if current_user.admin? && @record.creator.present?
%h3 Score recorded by #{@record.creator.name}

= form_tag maneuver_participant_path(@record), method: :put do
= hidden_field_tag :maneuver_id, @maneuver.id
= hidden_field_tag :participant_id, @participant.id
- @maneuver.grouped_obstacles.each do |(point_value, type), obstacles|
.form-group
= label_tag :"obstacle_#{obstacles.first.id}", "#{point_value}-point #{type.pluralize} touched"
- value = @record.obstacles_hit[obstacles.first.id].try(:[], 1) || 0
= render 'shared/with_increment_buttons' do
= number_field_tag "obstacle_#{obstacles.first.id}", value, in: 0..obstacles.count
- @maneuver.distance_targets.each do |target|
.form-group
= label_tag "target_#{target.id}", "#{target.interval_type} from #{target.name}"
= render 'shared/with_increment_buttons' do
= number_field_tag "target_#{target.id}",
@record.distances_achieved[[target.minimum, target.multiplier]],
min: 0
.form-group
= label_tag :reversed_direction, @maneuver.name.include?('Reverse') ? 'Extra reverses' : 'Reverses'
= render 'shared/with_increment_buttons' do
= number_field_tag :reversed_direction, @record.reversed_direction, min: 0
- if @maneuver.speed_target.present?
.form-group.form-check
= checkbox_tag :speed_achieved, '1', @record.speed_achieved, class: 'form-check-input'
= hidden_field_tag :speed_achieved, false
= label_tag :speed_achieved, "Achieved #{@maneuver.speed_target} mph", class: 'form-check-label'
- if @maneuver.counts_additional_stops?
.form-group
= checkbox_tag :made_additional_stops, '1', @record.made_additional_stops, class: 'form-check-input'
= hidden_field_tag :made_additional_stops, false
= label_tag :made_additional_stops, 'Extra stops?', class: 'form-check-label'
.form-group.form-check
= checkbox_tag :completed_as_designed, '1', @record.completed_as_designed, class: 'form-check-input'
= hidden_field_tag :completed_as_designed, false
= label_tag :completed_as_designed, 'Completed as designed?', class: 'form-check-label'
.form-group
= submit_tag 'Save score', class: 'btn btn-primary submit'
= link_to previous_participant_maneuver_path(@maneuver, relative_to: @participant.number) do
%button.btn.btn-secondary.actions{ type: :button }
%i.fa-solid.fa-arrow-left
%span.d-none.d-sm-inline Previous participant
= link_to next_participant_maneuver_path(@maneuver, relative_to: @participant.number) do
%button.btn.btn-secondary.actions{ type: :button }
%span.d-none.d-sm-inline Next participant
%i.fa-solid.fa-arrow-right
= render partial: 'form', locals: { maneuver_participant: @record,
maneuver: @maneuver,
participant: @participant,
submit_text: 'Save score' }
= link_to previous_participant_maneuver_path(@maneuver, relative_to: @participant.number) do
%button.btn.btn-secondary.actions{ type: :button }
%i.fa-solid.fa-arrow-left
%span.d-none.d-sm-inline Previous participant
= link_to next_participant_maneuver_path(@maneuver, relative_to: @participant.number) do
%button.btn.btn-secondary.actions{ type: :button }
%span.d-none.d-sm-inline Next participant
%i.fa-solid.fa-arrow-right
18 changes: 11 additions & 7 deletions spec/system/maneuver_score_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
when_current_user_is :admin
end

it 'updates the value' do
visit maneuver_participant_path(mp.id)
fill_in 'reversed_direction', with: '1'
click_on 'Save score'
expect(page).to have_field('reversed_direction', with: '1')
context 'when updating a checkbox' do
let(:field) { 'maneuver_participant_reversed_direction' }

it 'updates the value' do
visit maneuver_participant_path(mp.id)
fill_in field, with: '1'
click_on 'Save score'
expect(page).to have_field(field, with: '1')
end
end

context 'when updating an obstacle' do
let!(:obstacle) { create(:obstacle, maneuver:) }
let(:field) { "obstacle_#{obstacle.id}" }
let(:field) { "maneuver_participant_obstacle_#{obstacle.id}" }

it 'updates obstacles hit with 1' do
visit maneuver_participant_path(mp.id)
Expand All @@ -33,7 +37,7 @@

context 'when updating a distance target' do
let!(:distance_target) { create(:distance_target, maneuver:) }
let(:field) { "target_#{distance_target.id}" }
let(:field) { "maneuver_participant_target_#{distance_target.id}" }

it 'updates distance from target with 1' do
visit maneuver_participant_path(mp.id)
Expand Down