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

391: Use importmaps and hotwire #395

Merged
merged 47 commits into from
Dec 17, 2023
Merged

Conversation

mikevallano
Copy link
Owner

@mikevallano mikevallano commented Dec 8, 2023

Related Issues & PRs

Closes #391

Problems Solved

  • Uses hotwire with turbo_frames and turbo_streams, and uses importmaps for the modern way of handling things

Screenshots

rails-hotwire-in-production.mov

Things Learned

😅

  • turbo_streams are very similar to how we were handling things back in the day
  • it's a bit of a pain in the butt to have to worry about all the links that are inside a turbo_frame because they need to have additional data attributes.
  • the above might be less of a pain in the butt with better organized code, which leads me to:
  • our code, while a monumental achievement for our budding skills, is overly complex and as we discussed, we should 💣 it up and redo the frontend.
  • i had a hell of a time trying to use the old style of sprockets asset pipeline to include a single js file.

@mikevallano mikevallano added the wip label Dec 8, 2023
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 8, 2023 18:00 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 8, 2023 18:04 Inactive
@mikevallano mikevallano force-pushed the 392-upgrade-to-rails-7-1-2 branch from a39a728 to 1ae9a05 Compare December 8, 2023 18:49
Base automatically changed from 392-upgrade-to-rails-7-1-2 to master December 8, 2023 18:53
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 15:17 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 15:21 Inactive
@mikevallano mikevallano force-pushed the 391-updates-after-upgrades branch from edafbdb to 66dfb71 Compare December 12, 2023 15:23
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 15:23 Inactive
@mikevallano mikevallano force-pushed the 391-updates-after-upgrades branch from 66dfb71 to ec853c8 Compare December 12, 2023 15:27
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 15:28 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 15:51 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 16:04 Inactive
@mikevallano mikevallano force-pushed the 391-updates-after-upgrades branch from 426edbd to b6c5236 Compare December 12, 2023 16:12
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 16:12 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 16:29 Inactive
@mikevallano mikevallano removed the wip label Dec 12, 2023
@mikevallano mikevallano marked this pull request as ready for review December 12, 2023 16:43
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 16:58 Inactive
@mikevallano mikevallano force-pushed the 391-updates-after-upgrades branch from 6014615 to 7fb7601 Compare December 12, 2023 18:05
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 12, 2023 18:05 Inactive
@@ -0,0 +1 @@
*.scss
Copy link
Owner Author

Choose a reason for hiding this comment

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

I created a separate ticket to use prettier to clean up css files, if we want to: #396

Comment on lines +11 to +12
config.responder.error_status = :unprocessable_entity
config.responder.redirect_status = :see_other
Copy link
Owner Author

Choose a reason for hiding this comment

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

These are recommended changes as part of the Devise upgrade: https://github.com/heartcombo/devise/blob/main/CHANGELOG.md#490---2023-02-17

get 'movies/modal', to: 'movies#modal', as: :movie_modal
post 'movies/modal', to: 'movies#modal', as: :movie_modal
Copy link
Owner Author

Choose a reason for hiding this comment

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

turbo_streams can't be used with get requests, so this is why this is changed to a post.

Copy link
Collaborator

Choose a reason for hiding this comment

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

huh. interesting. i guess i hadn't thought about that, but it makes sense.

Comment on lines +25 to +30
sleep 0.25
fill_in "user_login", with: user.email
find("#user_password")
fill_in "user_password", with: user.password
click_button "log_in_button_new_session"
sleep 0.25
Copy link
Owner Author

Choose a reason for hiding this comment

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

Unsure why these sleeps are needed now, but it solved the problem 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

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

it might be that waiting for hotwire thing i am about to mention in a comment below... 😆

Comment on lines +3 to +12
max_time = Capybara::Helpers.monotonic_time + Capybara.default_max_wait_time
while Capybara::Helpers.monotonic_time < max_time
finished = finished_all_ajax_requests?
if finished
break
else
sleep 0.1
end
end
raise 'wait_for_ajax timeout' unless finished
Copy link
Owner Author

Choose a reason for hiding this comment

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

Was googling on a more updated way of handling this, and found this code, which was basically copied and pasted

Copy link
Collaborator

Choose a reason for hiding this comment

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

On a related note, at work we discovered a hack where we wait for the hotwire to connect before doing the next cypress action. Just throwing that idea out there in case it becomes relevant to our interests

Copy link
Owner Author

Choose a reason for hiding this comment

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

oooh, interesting. i did a quick googling and didn't find anything. but i will def keep that in mind for the frontend redo since all the frontend tests are gonna need to be addressed.

Comment on lines +2 to +3
adapter: redis
url: redis://localhost:6379/1
Copy link
Owner Author

Choose a reason for hiding this comment

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

auto-added by rails

Comment on lines +1 to +6
{
"engines": {
"node": "20.9.0",
"yarn": "1.22.19"
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

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

this was due to warnings from heroku as well as fighting with asset pipeline stuff.

TLDR: it specifies a version of these to be used, even though we're not using them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ewww

@@ -25,16 +25,16 @@ def create

def update
@listing = Listing.find_by("list_id = ? AND movie_id = ?", params[:list_id], params[:movie_id])
# TODO: refactor to be more efficient
Copy link
Owner Author

Choose a reason for hiding this comment

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

ticket for refactoring: #400

Copy link
Owner Author

Choose a reason for hiding this comment

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

This file is no longer needed with the Rails 7 way of using importmaps

Copy link
Owner Author

Choose a reason for hiding this comment

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

The bits of this that we still need were moved to autocomplete.js (named that way because it only deals with autocmoplete)

Comment on lines +1 to +12
<%= turbo_stream.update(
"movie_rated_#{@movie.tmdb_id}",
partial: 'movies/movie_rating',
locals: { movie: @movie }
)
%>
<%= turbo_stream.update(
"movie_show_rated_#{@movie.tmdb_id}",
partial: 'movies/movie_rating',
locals: { movie: @movie }
)
%>
Copy link
Owner Author

Choose a reason for hiding this comment

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

This is a result of us having complexity around showing things in the modal vs show page, for example

@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 14, 2023 23:20 Inactive
.gitignore Outdated
@@ -38,3 +38,6 @@ coverage/

# Ignore master key for decrypting credentials and more.
/config/master.key
config/credentials.yml.enc
Copy link
Collaborator

@lortza lortza Dec 15, 2023

Choose a reason for hiding this comment

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

This config/credentials.yml.enc file is not supposed to be in the .gitignore file. If it is, I will not have access to the credentials within. This file works in conjunction with the master.key file (which should be in the gitignore and never committed). Rails uses the master.key to decrypt the credentials file so i can access the values stored within, such as api tokens, etc.

Copy link
Owner Author

Choose a reason for hiding this comment

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

oooooh, got it. i removed it from the gitignore. thanks!

@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 15, 2023 23:33 Inactive
@mikevallano mikevallano temporarily deployed to rocky-headland-81947 December 16, 2023 17:52 Inactive
Comment on lines +10 to +11
gem 'hotwire-rails'
gem 'importmap-rails'
Copy link
Collaborator

Choose a reason for hiding this comment

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

🔥

@@ -16,3 +16,20 @@
width: 250px;
margin-left: 40px;
}

// SIGN OUT LINK NEEDS TO BE A BUTTON_TO. STYLES MATCH BOOTSTRAP
.navbar #sign_out_nav_link {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that this style is using an id here instead of a class. Is that just because it is already what is in place? If this is a new style, we should be using a class instead.

I'm also seeing this for other styles, so that ^ comment applies to all of the places we have ids for styles.

Copy link
Owner Author

Choose a reason for hiding this comment

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

yeah, it was already in place. i'll just leave it on this branch because on redoing the frontend, i plan to remove all of these ids that i believe were added for tests.

@@ -13,7 +13,7 @@ def create

respond_to do |format|
if listing.save
format.js {}
format.turbo_stream
format.html { redirect_to user_list_path(listing.list.owner, listing.list), notice: 'added to your list.' }
format.json { render :show, status: :created, location: listing }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious: do we need format.json? If not we could delete it. But maybe that's a separate cleanup PR.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good call. no, we do not use it at all. I created a separate issue to clean that up: #402

@@ -16,10 +16,12 @@ def destroy
@tagging = current_user.taggings.find_by("tag_id = ? AND movie_id = ?", params[:tag_id], params[:movie_id])
respond_to do |format|
if @tagging && @tagging.destroy
format.js {}
format.turbo_stream {}
format.html { redirect_to movie_path(@movie), notice: 'Tag was destroyed.' }
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm vaguely remembering something from the pragmatic studio tutorial about how we need to handle notices or errors or something in controllers... like we have to specifically declare (:fan: ✋) something now

not a super helpful comment 😆 my brain is sleepy

Copy link
Owner Author

Choose a reason for hiding this comment

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

hmm, yeah, this is a weird one because it'll always be a turbo_stream response. but general handling of notices/errors can be part of the frontend redo.

Comment on lines +3 to +12
max_time = Capybara::Helpers.monotonic_time + Capybara.default_max_wait_time
while Capybara::Helpers.monotonic_time < max_time
finished = finished_all_ajax_requests?
if finished
break
else
sleep 0.1
end
end
raise 'wait_for_ajax timeout' unless finished
Copy link
Collaborator

Choose a reason for hiding this comment

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

On a related note, at work we discovered a hack where we wait for the hotwire to connect before doing the next cypress action. Just throwing that idea out there in case it becomes relevant to our interests

Comment on lines +25 to +30
sleep 0.25
fill_in "user_login", with: user.email
find("#user_password")
fill_in "user_password", with: user.password
click_button "log_in_button_new_session"
sleep 0.25
Copy link
Collaborator

Choose a reason for hiding this comment

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

it might be that waiting for hotwire thing i am about to mention in a comment below... 😆

@@ -11,17 +11,19 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />

<%= stylesheet_link_tag 'application', media: 'all'%>
<%= javascript_include_tag 'application'%>
<%= javascript_include_tag 'application' %>
<%= javascript_import_module_tag "autocomplete" %>
Copy link
Collaborator

Choose a reason for hiding this comment

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

😅 thanks for sticking with it and making this work!

Comment on lines +18 to 19
<!--TODO: use importmap for this-->
<script src="//use.fontawesome.com/16a020f0b8.js"></script>
Copy link
Collaborator

Choose a reason for hiding this comment

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

i have also experienced a lot of bootstrap drama with rails 7 upgrades and general fontawesome unhappiness. so, yeah i'm down to switch to material.

get 'movies/modal', to: 'movies#modal', as: :movie_modal
post 'movies/modal', to: 'movies#modal', as: :movie_modal
Copy link
Collaborator

Choose a reason for hiding this comment

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

huh. interesting. i guess i hadn't thought about that, but it makes sense.

Comment on lines +1 to +6
{
"engines": {
"node": "20.9.0",
"yarn": "1.22.19"
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

ewww

@mikevallano mikevallano merged commit 7a91d20 into master Dec 17, 2023
@mikevallano mikevallano deleted the 391-updates-after-upgrades branch December 17, 2023 15:08
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.

Update app to use latest ways of doing things in rails
2 participants