Skip to content

Add helper methods (wasExchanged, wasFilled, wasCleared, origin) to ModelTrait #276

Description

@DenTray

Description

What needs to be done?

  • Update project dependencies to ensure compatibility: upgrade Laravel to version >=12.28 and update all required dependencies accordingly to avoid conflicts and runtime errors, since the getPrevious method is not available in earlier Laravel versions.
  • Add the following methods to the ModelTrait:
public function wasExchanged(string $fieldName): bool
{
return $this->wasChanged($fieldName) && !is_null($this->origin($fieldName));
}

public function wasFilled(string $fieldName): bool
{
return $this->wasChanged($fieldName) && is_null($this->origin($fieldName));
}

public function wasCleared(string $fieldName): bool
{
return $this->wasChanged($fieldName) && is_null($this->getAttribute($fieldName));
}

public function origin(string $fieldName): mixed
{
return Arr::get($this->getPrevious(), $fieldName);
}

  • Implement unit tests to cover the functionality of both methods, ensuring they work correctly under various scenarios.

Expected Outcome

  • The ModelTrait includes the wasExchanged , wasFilled, wasCleared and origin methods.
  • Each new methods have comprehensive test coverage validating their expected behavior.
  • Tests verify correct detection of changed fields and retrieval of previous values.
  • Project dependencies are updated, and Laravel version is >=12.28 with no dependency conflicts.

Verification Scenarios

  1. Test wasExchanged returns true only when a specified field has changed and its previous value is not null.
  2. Test wasFilled returns true only when a specified field has changed and its previous value is null.
  3. Test wasCleared returns true only when a specified field has changed and its current value is null.
  4. Test origin retrieves the previous value of a given field accurately.
  5. Confirm behavior when fields are unchanged or previous values are null.
  6. Verify methods handle edge cases and invalid inputs gracefully.
  7. Ensure all tests pass successfully in the test suite.
  8. Confirm the application runs correctly after dependency updates without errors.

Resources


Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions