Skip to content

Commit

Permalink
Added comments to spec file and renamed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
acbondi authored and alecb100 committed Dec 6, 2023
1 parent e6fe9a1 commit b6d74b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/controllers/late_policies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def check_for_duplicate_name(prefix)
return valid_penalty, error_message
end

# This function validates the input.
# This function validates the input. The outputs are a boolean, which is true if the input is valid, and the second output
# is a string which contains all of the error messages. If there were no error messages, the returned string is ""
def validate_input(is_update = false)
# Validates input for create and update forms
max_penalty = params[:late_policy][:max_penalty].to_i
Expand All @@ -173,13 +174,13 @@ def validate_input(is_update = false)
error_messages << name_error if name_error

# This validates the max_penalty to make sure it's within the correct range
if max_penalty_validation(max_penalty, penalty_per_unit)
if max_penalty_valid(max_penalty, penalty_per_unit)
error_messages << "#{error_prefix(is_update)}The maximum penalty must be between the penalty per unit and 100."
valid_penalty = false
end

# This validates the penalty_per_unit and makes sure it's not negative
if penalty_per_unit_validation(penalty_per_unit)
if penalty_per_unit_valid(penalty_per_unit)
error_messages << 'Penalty per unit cannot be negative.'
valid_penalty = false
end
Expand All @@ -188,12 +189,12 @@ def validate_input(is_update = false)
end

# Validate the maximum penalty and ensure it's in the correct range
def max_penalty_validation(max_penalty, penalty_per_unit)
def max_penalty_valid(max_penalty, penalty_per_unit)
max_penalty < penalty_per_unit || max_penalty > 100
end

# Validates the penalty per unit
def penalty_per_unit_validation(penalty_per_unit)
def penalty_per_unit_valid(penalty_per_unit)
penalty_per_unit < 0
end

Expand Down
7 changes: 6 additions & 1 deletion spec/controllers/late_policies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
stub_current_user(instructor, instructor.role.name, instructor.role)
end

# Creates a late policy for various tests. Was duplicated at multiple locations, so the duplicate code was taken out
# and placed here for more readability and to satisfy DRY
def create_late_policy(policy_name, max_penalty, penalty_per_unit, instructor_id)
late_policy = LatePolicy.new
late_policy.policy_name = policy_name
Expand All @@ -14,6 +16,7 @@ def create_late_policy(policy_name, max_penalty, penalty_per_unit, instructor_id
late_policy
end

# This helper method was also created to reduce repeated code
def request_params(policy_name, max_penalty, penalty_per_unit)
{
late_policy: {
Expand All @@ -24,6 +27,8 @@ def request_params(policy_name, max_penalty, penalty_per_unit)
}
end

# This helper method was created to reduce repeated code as well. This is similar to the previous method
# but adds a unique id to it on top of the other things
def request_params_with_id(policy_name, max_penalty, penalty_per_unit, id)
{
late_policy: {
Expand Down Expand Up @@ -103,7 +108,7 @@ def request_params_with_id(policy_name, max_penalty, penalty_per_unit, id)
end
end

# Create an RSpec example to reduce code duplication
# Create an RSpec example to reduce code duplication in the following tests
RSpec.shared_examples 'late policy creation with error' do |policy_name, max_penalty, penalty_per_unit, expected_error|
before(:each) do
latePolicy = LatePolicy.new
Expand Down

0 comments on commit b6d74b1

Please sign in to comment.