-
Notifications
You must be signed in to change notification settings - Fork 1
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
Automatically build missing values for valid fields. #6
base: master
Are you sure you want to change the base?
Conversation
7e58012
to
8317cd1
Compare
@Liooo I've refactored my code so that original Let me know what do you think ;) |
8317cd1
to
3b83611
Compare
@Liooo Can you please provide some feedback on this PR? Thank you! |
Hey @CrAsH1101 sorry it's late 🙏 I'll check it when I have time so plz wait bit more (I forgot ruby and need some time to remember stuff 😂) |
65e6f3a
to
8c1dc32
Compare
@Liooo Ok, no problem. In the meantime, I've added tests to completely cover new methods added in this PR. Hope you like it ;) |
ba9d291
to
63b9f84
Compare
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.
sorry it's been so late, thanks for your contribution 🙏
lib/dynabute/dynabutable.rb
Outdated
name_or_id = {name: name, id: id}.compact | ||
return nil if name_or_id.blank? && field.blank? | ||
field_obj = field || Dynabute::Field.find_by(name_or_id.merge(target_model: self.class.to_s)) | ||
fail Dynabute::FieldNotFound.new(name_or_id, field) if field_obj.nil? | ||
field_obj = field | ||
if field_obj.blank? | ||
name_or_id = {name: name, id: id}.compact | ||
return nil if name_or_id.blank? | ||
field_obj = Dynabute::Field.find_by(name_or_id.merge(target_model: self.class.to_s)) | ||
fail Dynabute::FieldNotFound.new(name_or_id, field) if field_obj.nil? | ||
end |
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.
It'd be great if there's an argument validation logic here, that throws error when 0 or more than 2 arguments are present.
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.
Original implementation was just returning nil
so I kept it that way. Please check now the method. I have updated tests too.
if field_obj.has_many | ||
if value_id | ||
value_obj = value_obj.detect{|v| v.id == value_id} if value_obj | ||
fail ValueNotFound unless value_obj | ||
else | ||
value_obj = build_dynabute_value(field: field_obj) | ||
end |
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.
set_dynabute_attributes() for has_many field sounds more like it's replacing the whole values with the given array. having append_dynabute_value
(which should throw error when called on non has_many field) seems more natural to me, what do you think?
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.
My idea was that you have single method for setting the values. It was intended to be like create_or_update
concept.
Just like rails, when submitting a form for persisted object (id
exists), it will trigger update action. In case the object is not persisted and there is no id
value, form will submit create action and the new record will be added to the database.
So first time you call method for has_many
field, it will add specified value, same as with strings and integers, but it will return an Array. Next set will just append to that Array as something already exists.
Let me know if you have something specific on your mind 😉
cd4aa54
to
65b070a
Compare
Allow update value for fields that do not have inserted record in appropriate value table.
65b070a
to
09c322a
Compare
Allow update value for fields that do not have inserted record in
appropriate value table.
Currently, initial creation of values records are done through
build_dynabute_value
method. If we want to update existing value, we can't usebuild_dynabute_value
as that would result in new value being created for the same field.The idea here is to automatically call
build_dynabute_value
if the record doesn't exist, and have helper functions to achieve that.