Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CrAsH1101
Copy link
Contributor

@CrAsH1101 CrAsH1101 commented Jan 17, 2022

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 use build_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.

@CrAsH1101 CrAsH1101 force-pushed the auto-build-for-update branch 12 times, most recently from 7e58012 to 8317cd1 Compare January 20, 2022 15:32
@CrAsH1101
Copy link
Contributor Author

@Liooo I've refactored my code so that original dynabute_value method is not automatically preparing new nested object in case one is missing. Instead, I have added few helper methods that deals with data management of the nested record without a need to instantly make changes in the database.

Let me know what do you think ;)

@CrAsH1101
Copy link
Contributor Author

@Liooo Can you please provide some feedback on this PR?

Thank you!

@Liooo
Copy link
Owner

Liooo commented Jan 29, 2022

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 😂)

@CrAsH1101 CrAsH1101 force-pushed the auto-build-for-update branch 2 times, most recently from 65e6f3a to 8c1dc32 Compare January 29, 2022 12:47
@CrAsH1101
Copy link
Contributor Author

@Liooo Ok, no problem. In the meantime, I've added tests to completely cover new methods added in this PR. Hope you like it ;)

README.md Outdated Show resolved Hide resolved
Copy link
Owner

@Liooo Liooo left a 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 🙏

Comment on lines 67 to 145
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
Copy link
Owner

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.

Copy link
Contributor Author

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.

README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
Comment on lines +78 to +84
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
Copy link
Owner

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?

Copy link
Contributor Author

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 😉

@CrAsH1101 CrAsH1101 force-pushed the auto-build-for-update branch 2 times, most recently from cd4aa54 to 65b070a Compare October 27, 2022 00:06
Allow update value for fields that do not have inserted record in
appropriate value table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants