Skip to content

Refactor redundant status validation that raises exceptions #43

@kanejamison

Description

@kanejamison

Description

The Publishable concern has a validate_status_value method that raises an ArgumentError during validation. This breaks Rails conventions and is also redundant with the existing inclusion validation.

Current code

lib/bunko/models/post_methods/publishable.rb:41-46:

def validate_status_value
  return if status.blank?

  unless Bunko.configuration.valid_statuses.include?(status)
    raise ArgumentError, "#{status} is not a valid status"
  end
end

Problems this causes

  1. Validators should add to errors, not raise exceptions
  2. Impossible to recover from - breaks form error handling
  3. The inclusion validation on line 11-14 already handles this exact check
  4. Inconsistent with how other validations work in the gem
  5. Users can't build forms with graceful error messages

Pros of refactoring

  • Follows Rails validation conventions
  • Allows graceful error handling in forms
  • Removes duplicate validation logic
  • Consistent behavior with other validations
  • Cleaner, less code

Cons of refactoring

  • None - the inclusion validation already provides this functionality

Recommended approach

Simply delete the validate_status_value method and the validate :validate_status_value callback. The existing inclusion validation already validates that status is in the configured list of valid statuses.

References

  • lib/bunko/models/post_methods/publishable.rb:41-46
  • lib/bunko/models/post_methods/publishable.rb:11-14 (existing inclusion validation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions