-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add presence validation for object in SnapshotItem model #56
Add presence validation for object in SnapshotItem model #56
Conversation
object
in SnapshotItem model
Can we reduce the scope of this PR to adding the validation only? The other change regarding changes to the object method to add the |
test/models/snapshot_item_test.rb
Outdated
|
||
[:item_id, :item_type, :snapshot_id].each do |attr| | ||
%i[item_id item_type snapshot_id object].each do |attr| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just keep the original form
%i[item_id item_type snapshot_id object].each do |attr| | |
[:item_id, :item_type, :snapshot_id, :object].each do |attr| |
end | ||
|
||
@object = YAML.send(yaml_method, self[:object]) | ||
@object = super() ? YAML.public_send(yaml_method, super()) : {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the rest of the changes in this method are no longer relevant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I have to remove the rest or change the error class?
@object = self[:object] | ||
@object = super() | ||
else | ||
raise ArgumentError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure that ArgumentError is appropriate here as storage_method wasnt a method argument. Maybe just StandardError is fine.
raise ArgumentError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`" | |
raise StandardError, "Unhandled ActiveSnapshot.config.storage_method" |
elsif ActiveSnapshot.config.storage_method_native_json? | ||
@object = self[:object] | ||
@object = super |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the mentions of super
do not seem relevant in this PR. Should they not remain as self[:object]
|
||
def object | ||
return @object if @object | ||
|
||
if ActiveSnapshot.config.storage_method_json? | ||
@object = JSON.parse(self[:object]) | ||
@object = self[:object] ? JSON.parse(self[:object]) : {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing this empty object fallback on every line Maybe we can do @object ||= {}
after the if else storage method statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better, but the new instance with fresh @object
will go to JSON.parse(self[:object]) # => Error
#51
Improved Data Integrity
By adding
validates :object, presence: true
, you ensure that the object attribute cannot be null or empty when aSnapshotItem
is created or updated. This helps maintain data integrity and prevents situations where object might be inadvertently left empty.Error Handling for Unsupported Storage Methods
An explicit error is raised if an unsupported
storage_method
is used, making it clear when configuration issues occur and preventing unexpected behavior.