-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
app/views/collections/show.html.erb
Outdated
@@ -1,4 +1,12 @@ | |||
<%- content_for :menu_title, @collection.title %> | |||
<%- content_for :menu_title do %> | |||
<span contenteditable="true" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) %>"
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
I struggled to make the title appear editable on hover without it becoming very ugly - now it gets underlined.