Conversation
Current coverage is 97.70% (diff: 100%)@@ master #137 diff @@
==========================================
Files 154 156 +2
Lines 2257 2348 +91
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 2204 2294 +90
- Misses 53 54 +1
Partials 0 0
|
There was a problem hiding this comment.
Either the docs or the var name are wrong.
There was a problem hiding this comment.
I don't get the comment. Names match :/
There was a problem hiding this comment.
I was confused because the var name says order_stirng, but then it's an Array of IDs, I didn't get it was a JSON string or similar (maybe it could be added)
There was a problem hiding this comment.
order_string should be something like: "[2, 1, 3, 5, 7, 9, 8]", so a String representing an Array of IDs. Each element of the array is an ID of a step, and its index is the position field value.
There was a problem hiding this comment.
This is weird. Coercing that string into an array should be the transport's responsibility (ujs via the controller).
There was a problem hiding this comment.
I didn't want to rescue a JSON parsing in the controller but yeah, feels weird to me too. Changing it!
There was a problem hiding this comment.
Missing new line after return
There was a problem hiding this comment.
Missing new line before return
There was a problem hiding this comment.
I'd move this to a method (order). This way you can do return broadcast(:invalid) unless order and encapsulate all the JSON and presence logic in a method.
There was a problem hiding this comment.
I don't understand this method. Also:
order.each_with_index.inject({}) do |data, (id, index)|
data.update(id: { position: index })
endThere was a problem hiding this comment.
Your version does an update for every single element in the collection. My version uses a single update for all the collection:
Edit: OK checking the code it actually does an update for every element in the array :/
There was a problem hiding this comment.
That's why I said I didn't understand, what is this doing?
There was a problem hiding this comment.
Not sure if we should create another controller or not.
There was a problem hiding this comment.
Probably, with a create action. Will do.
There was a problem hiding this comment.
We don't do anything. If I put a render or a redirect here, then page refreshes and misses all the point.
We could render some JS here and then do something in the layout, but if we need to show a success message every time the user drags and drops... I think it's too much.
There was a problem hiding this comment.
What you should do is put a spinner when you start the request, and delete that spinner when it finishes. But hey, ujs is a hack, this won't last long as we'll replace this with React+GraphQL (already working on it).
There was a problem hiding this comment.
Should we move this to a JS file?
There was a problem hiding this comment.
I won't accept any JS inside html templates... 👎
There was a problem hiding this comment.
We should add some validations to the model (numericality, minimum, maximum, etc). Should we set the position on creation or do we allow nil values?
There was a problem hiding this comment.
Right now, steps are created with position: nil, thus positioning themselves in the beginning of the list. I don't know if this is the expected or we'd rather have them in the end. If we want to have them in the end, I can create the steps with a really high position (position: 9999 for example). Once the user reorders this, position will go back to a "normal" number
There was a problem hiding this comment.
We can add validations allowing blank values. Regarding default values, I'd expect new steps to be added to the end, not the begining. Maybe the default value can be the previous + 1 (resort solves this 😝)
There was a problem hiding this comment.
Validations are in the model. As for the default value, I don't know if resort would be too much for just this (could be a before_save method or similar), and I don't know if I'll need other resort features by now :/
There was a problem hiding this comment.
Agreed offline to just add a before_create action to set the value when it's nil.
|
I recommend adding a javascript linter before it is too late. I recommend eslint (https://github.com/appfolio/eslint-rails) and adding this as a CI step. |
There was a problem hiding this comment.
See previous discussion in https://github.com/codegram/decidim/pull/137#discussion_r84491443
There was a problem hiding this comment.
I'd go with "No news good news" for now.
There was a problem hiding this comment.
Protip: You can run rubocop -a and will probably auto-fix this.
9593bb6 to
d85c3a3
Compare
There was a problem hiding this comment.
This is weird. Coercing that string into an array should be the transport's responsibility (ujs via the controller).
d85c3a3 to
1cf6bab
Compare
| def create | ||
| authorize ParticipatoryProcessStep | ||
|
|
||
| byebug |
| expect { subject.call }.to broadcast(:ok) | ||
| end | ||
|
|
||
| it "deactivates it" do |
| return if position.present? | ||
| return self.position = 0 if participatory_process.steps.empty? | ||
|
|
||
| self.position = participatory_process.steps.last.position + 1 |
There was a problem hiding this comment.
What about participatory_process.steps.pluck(:position).last ? (I know we'll load more rows, but we'll load less data, not sure which is best though.)
| # manually, it will be set to 0, and when it is not the only one it will be | ||
| # set to the last step's position + 1. | ||
| # | ||
| # Note: This allows manual positioning, so it can cause different steps to |
There was a problem hiding this comment.
Shouldn't we add a unique validation then? (And also to the index)
| class AddPositionToSteps < ActiveRecord::Migration[5.0] | ||
| def change | ||
| add_column :decidim_participatory_process_steps, :position, :integer | ||
| add_index :decidim_participatory_process_steps, [:decidim_participatory_process_id, :position], order: { position: :desc }, name: "index_check_position_for_steps" |
There was a problem hiding this comment.
This order doesn't match the one at the relation scope
🎩 What? Why?
We need to reorder steps. This can be done now with a drag&drop of table rows.
📋 Subtasks
positionvalue for new steps.jsfileeslint) => Moved to Add ESlint config #141👻 GIF