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

Fix photo atom state bug #1141

Merged
merged 5 commits into from
Nov 12, 2024
Merged

Fix photo atom state bug #1141

merged 5 commits into from
Nov 12, 2024

Conversation

MICHAELMUNAVU83
Copy link
Collaborator

I noticed a bug where sometimes we have the state as "approved" , String , instead of :approved , atom , this PR ensures we only use the atom version of it.
Screenshot 2024-11-12 at 09 20 49

Copy link
Collaborator

@wintermeyer wintermeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good: Good catch!
Bad: Wrong way of fixing it.

@@ -29,6 +29,13 @@ defmodule AniminaWeb.StoriesComponents do
language={@language}
delete_story_modal_text={@delete_story_modal_text}
user={@user}
state={
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

photo.state is not set by some magic in the background which we can not control. If there is a bug somewhere in our code which sets this state in a wrong way we have to fix THAT bug and not add more code afterwards to "fix" it. This shouldn't happen in the first place. Please find the original bug!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And (of course!) after fixing the original bug we have to make sure that already existing database entries are fixed too (e.g. with a migration).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this , in the database with IEX , all come as an atom , also we have it in the validation

  attribute :state, :atom do
      constraints one_of: [:pending_review, :in_review, :approved, :rejected, :error, :nsfw]

So this must be an ash bug or something similar where it displays atoms in the database as strings.
Although when you have an atom for example :stefan in the HTML , it is displayed as "stefan"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Is there code somewhere which writes into the database while bypassing the validation?
  • If this is an Ash bug: Did you ask in the elixirforum.com about it and/or created an issue in the Ash GitHub repo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We use the state transition which does so implicitly , @briankariuki , I used some of your code
 <%= state =
            if(
              is_atom(@story.photo.state),
              do: @story.photo.state,
              else: String.to_atom(@story.photo.state)
            ) %>
``` , do you mind chipping in here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there code somewhere which writes into the database while bypassing the validation?

My bad. I just re-read your comment. So in the DB everything is fine. hmmm... does seem like an Ash bug. Please ask in the elixirforum.com if there is an other explanation for this and add a link to that question here.

In addition:

  • Add a comment in the code which describes why you are doing this. Otherwise somebody will remove that code in the future since it shouldn't be a problem in the first place.
  • If possible write a test to make sure that we don't run into a problem if somebody deletes this code anyway.
  • Resubmit the PR to be reviewed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added tests and comments for clarity.
I have also asked a question on elixir forum for the same https://elixirforum.com/t/using-database-values-stored-as-atoms-in-html-in-ash-framework/67437

@MICHAELMUNAVU83 MICHAELMUNAVU83 marked this pull request as ready for review November 12, 2024 10:29
@wintermeyer wintermeyer merged commit dd52f23 into main Nov 12, 2024
1 check passed
@wintermeyer wintermeyer deleted the fix-photo-atom-state-bug branch November 12, 2024 10:33
@MICHAELMUNAVU83
Copy link
Collaborator Author

@wintermeyer , kindly let me know once you deploy this

@wintermeyer
Copy link
Collaborator

@wintermeyer , kindly let me know once you deploy this

I just triggered the deployment. ETA: +5 minutes

@MICHAELMUNAVU83
Copy link
Collaborator Author

All good , I can now see your face
Screenshot 2024-11-12 at 13 59 28
Let me do a quick check for other places we use the state conditionally

@MICHAELMUNAVU83
Copy link
Collaborator Author

Seems like we have a couple of similar issues related to this bug causing photos not be seen , There will be a PR for this

@wintermeyer
Copy link
Collaborator

Seems like we have a couple of similar issues related to this bug causing photos not be seen , There will be a PR for this

Can we add a test for this?

@MICHAELMUNAVU83
Copy link
Collaborator Author

Yes

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.

2 participants