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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/animina_web/components/profile/stories_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

if @story.photo && is_atom(@story.photo.state) do
@story.photo.state
else
make_sure_photo_state_is_atom(@story.photo)
end
}
current_user_green_flags={@current_user_green_flags}
current_user_red_flags={@current_user_red_flags}
/>
Expand Down Expand Up @@ -194,6 +201,14 @@ defmodule AniminaWeb.StoriesComponents do
with_locale(language, fn -> gettext("Error") end)
end

defp make_sure_photo_state_is_atom(nil) do
""
end

defp make_sure_photo_state_is_atom(photo) do
String.to_atom(photo.state)
end

attr :story, :any, required: true
attr :current_user, :any, required: true
attr :dom_id, :string, required: false
Expand Down
15 changes: 15 additions & 0 deletions lib/animina_web/components/profile/story_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ defmodule AniminaWeb.StoryComponent do
{:ok, socket}
end

defp make_sure_photo_state_is_atom(nil) do
""
end

defp make_sure_photo_state_is_atom(photo) do
String.to_atom(photo.state)
end

@impl true
def render(assigns) do
~H"""
Expand All @@ -34,6 +42,13 @@ defmodule AniminaWeb.StoryComponent do
current_user={@current_user}
dom_id={@dom_id}
user={@user}
state={
if @story.photo && is_atom(@story.photo.state) do
@story.photo.state
else
make_sure_photo_state_is_atom(@story.photo)
end
}
current_user_green_flags={@current_user_green_flags}
language={@language}
current_user_red_flags={@current_user_red_flags}
Expand Down
Loading