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

Enabled editing the title of a collection via Stimulus #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

louispt1
Copy link
Collaborator

@louispt1 louispt1 commented Mar 5, 2025

I struggled to make the title appear editable on hover without it becoming very ugly - now it gets underlined.

@louispt1 louispt1 requested a review from noracato March 5, 2025 10:15
Copy link
Member

@noracato noracato left a comment

Choose a reason for hiding this comment

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

Hey! Nice idea with the stimulus controller. I did not check the looks of it yet, but based on the code, here are a few comments and questions.

Could you also write a spec for the update action of the controller, please?

def update
@collection = Collection.find(params[:id])

if @collection.update(create_collection_params)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if @collection.update(create_collection_params)
if @collection.update(update_collection_params)

Let's make sure a user can't accidentally update the version, or scenarios when the update form does not yet contain them. We'd need something like update_collection_params.

@@ -54,6 +54,16 @@ def new
end
end

def update
@collection = Collection.find(params[:id])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@collection = Collection.find(params[:id])

We should check authentication and not just do a find_by. If you add update to the hook load_and_authorize_resource, @collection will become available and will be validated already.

@@ -1,4 +1,12 @@
<%- content_for :menu_title, @collection.title %>
<%- content_for :menu_title do %>
<span contenteditable="true"
Copy link
Member

Choose a reason for hiding this comment

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

Is it always editable for everybody?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point! After I add update to the load_and_authorize_resource, will this work/ is this the right approach?

<span contenteditable="<%= can?(:update, @collection) %>"

Copy link
Member

Choose a reason for hiding this comment

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

Currently the collection is in the show action, not in the update one :) But I assume this would work.

However, as a rule we try to put the least logic in the view as possible. The more "complicated" the logic is, the more we want to put it in the controller or a component helper to keep our views clean, and have a clear idea from looking at the controller what is going on. The can? method is mostly used in before hooks of the controller, so we can classify it as something out of the league of a view.

One possibility for instance could be to create a boolean @editable = can?(:update, @collection) in the show action, and reference it in the view.

Copy link
Member

Choose a reason for hiding this comment

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

Or, since a Collection has only one user attached: @collection.user == current_user. Whichever you prefer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! I will have a go refactoring it in this way

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