-
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
Feat allow admin to edit observations #363
Conversation
@tsubik I updated the |
Also, the field |
app/admin/observation.rb
Outdated
@@ -413,6 +413,7 @@ def self.observations | |||
end | |||
|
|||
form do |f| | |||
allow_override = ENV.fetch('OVERRIDE_OBSERVATION_STATUS', nil)&.upcase == '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.
why not just ENV.fetch('OVERRIDE_OBSERVATION_STATUS', 'false').upcase == 'TRUE'
and why do we need to have it as a flag? Do you think we want to change it back and forth?
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.
@tsubik yeah, this is supposed to be for them to temporarily edit observations that were created wrongly.
I'm guessing this will happen more times in the future.
if f.object.observation_type == 'operator' | ||
f.input :fmu_id, as: :nested_select, | ||
level_1: { | ||
attribute: :country_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.
indent level is weird here.
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.
@tsubik indeed... I'll check it. I think it was Rubocop that changed it.
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.
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.
@tsubik I didn't understand the screenshot. Which fields do not work? I tested it locally and it's filtering by the right field.
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.
Let me check it once again, as it was long time ago and I don't remember what was not working. Yeah, the screenshot could be better as it's not showing much.
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.
@santostiago both Operator and FMU selects do not work correctly
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.
@tsubik ohh.. now I get what you mean. The filtered query is not being used correctly, no. Because we're using a translation table, it seems like ransack cannot interpret it properly.
For example, with the Democratic Republic of Congo
selected, when I search for the operator Andre
the ajax query made is:
http:https://localhost:3000/admin/producers?order=operator_translations.name_desc&q[groupings][0][m]=or&q[groupings][0][operator_translations.name_contains]=andre&q[combinator]=and&q[country_id_eq]=7
I'm not sure what these 2 params do:
q[groupings][0][m]=or
&q[combinator]=and
I need to check ransack. I'm also not sure if the right way to query the name is by using q[groupings][0][operator_translations.name_contains]=andre
. But this is done by the gem. It might be easier if I just manage to change the controller of the Operator
AA resource to handle this properly.
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.
I think there is a task to remove name from translated fields for operator (maybe that would help here), not sure if FMU as well.
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.
it would solve it, yes. (I just dug into ransack code, and the issue is that when there's a query with groupings, ransack searches for the field name in his context - and he doesn't have the translations in his context).
I made a quick hack to fix it. Not an ideal solution, but I wouldn't waste more time on this.
app/models/observation.rb
Outdated
# Returns whether the admin can override the status of an observations and its fields. | ||
# This is defined by the ENV var `OVERRIDE_OBSERVATION_STATUS` which should be 'TRUE' | ||
def admin_can_override? | ||
user_type == :admin && ENV.fetch('OVERRIDE_OBSERVATION_STATUS', nil)&.upcase == '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.
What is user type here, and where is it set to be :admin
?
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.
Looks like for admin panel validation of status changes will never be invoked, right?
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.
Looks like for admin panel validation of status changes will never be invoked, right?
Yup.
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.
I meant, I have not seen setting user_type to admin anywhere that's why I'm asking if that's a mistake to check it here?
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.
@tsubik you're absolutely right.
In fact, the rules seemed to have been wrong before. We were just validating the transitions when it came from the observations tool.
I'm changing it.
return unless operator_id_was == operator_id | ||
|
||
old_operator = Operator.find_by id: operator_id_was | ||
ScoreOperatorObservation.recalculate! old_operator if old_operator.present? |
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.
Changing operator is still disabled, so how this code could be invoked? Or maybe you forgot to remove disabled
from operator input. I thought operator should be changeable and that was the point of the feature?
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.
@tsubik mmm... I'm not sure what you're referring to. The operator can be modified (what cannot be is the type of observation, because I didn't want to check all the consequences and I don't think that should change).
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.
I cannot modify operator, it has disabled: operator
in the input. Select stays disabled in admin panel.
spec/models/observation_spec.rb
Outdated
@@ -71,28 +71,80 @@ | |||
|
|||
|
|||
describe 'Validations' do | |||
describe 'Status changes' do | |||
describe '#status_changes' do | |||
let(:country) { FactoryBot.create(:country) } |
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.
I'm pretty sure I didn't write FactoryBot
.. Could it be that Rubocop did it? (or maybe I'm going crazy :P )
@tsubik I modified it, but I'll wait for Sophie to know if this is what they want. |
Ok, so we decided to just go with having |
Alright, so many modifications are needed in that case :/ |
a592118
to
9c477f8
Compare
@tsubik ok, so:
The only thing that was not very clear on Sophie's spec was if the admins could modify the status at will. I'm going to confirm that. |
app/admin/observation.rb
Outdated
resource.update(validation_status: Observation.validation_statuses['QC in progress']) | ||
redirect_to collection_path, notice: 'QC in progress for observations' | ||
resource.validation_status = Observation.validation_statuses['QC in progress'] | ||
notice = resource.save ? 'Observation approved' : 'Observation not modified' |
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.
Why all messages are changed to "observation approved"?
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.
Mmmm.. right. I'll change them to the previous one.
app/admin/observation.rb
Outdated
redirect_to collection_path, notice: 'QC started' | ||
end | ||
# Bulk actions should be available only if this env flag is set to `true` | ||
if ENV.fetch('BULK_EDIT_OBSERVATIONS', 'false').upcase == '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.
If we don't need that, I would vote to just remove it and not take space here ;)
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.
Sophie said it shouldn't be done. But I'd bet it's gonna be needed again in a year or so.
Do you think we should just get rid of it?
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.
Then, maybe better to keep them without disabling?
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.
But keeping code we should also maintain it, and we should not maintain something that might be needed in future, but probably don't, so either we remove it or keep it. That's my opinion.
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.
As discussed with Sophie, let's keep this hidden under an ENV var.
operator = object.operator_id.present? ? true : false | ||
fmu = object.fmu_id.present? ? true : false | ||
government = object.government_ids.present? ? true : false | ||
allow_override = current_user.user_permission.user_role == 'admin' |
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.
The form got very big and confusion, but maybe that's ok. Just thinking if for bo manager
we can only show necessary fields if there are any and the fields that the user can change. Not sure if that is a good idea
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.
@tsubik I thought of it, but they need to see those in order to approve or not and to comment.
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.
Ok. Good point
9c477f8
to
6d72ac3
Compare
Going to rebase it now. |
e05e192
to
66730ac
Compare
redirect_to collection_path, notice: 'QC started' | ||
end | ||
# Bulk actions should be available only if this env flag is set to `true` | ||
if ENV.fetch('BULK_EDIT_OBSERVATIONS', 'TRUE').upcase == '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.
@tsubik I ended up changing it to be on by default and we add the var with false
in the server.
Doing it the other way was proving to be complicated to test (I was trying to remove the constant Admin::ObservationsController
and then reloading the file, but I'd have to reload it using the AA loader and it wasn't working)
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.
ok
Hey @santostiago, but I see there are no valid status transitions for |
764dbec
to
fe3164a
Compare
@tsubik we're sending the requests from the observations admin page as admin, so it will have the same permissions (this can be improved, because we should instead check who made the request and set the permissions there, but I'll do it in another PR). |
Allows admins to edit most of the fields of observations, and makes bo_managers only capable of modifying the 'status' and 'admin comment' fields.
93f0771
to
c77f507
Compare
@tsubik can you review? The solution is a bit hacky, but I didn't want to waste more time with this. |
app/admin/operator.rb
Outdated
@@ -280,6 +282,24 @@ | |||
end | |||
|
|||
controller do | |||
def index |
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.
Problem is not only for operator but for many other attributes that are using this component, category, subcategory and others. Let me think about it.
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.
With hotwire that would be easy to just refresh form on every country select change or other select changes.
use different ransack hack to work with globalize attributes
Task
When a flag to override observations is on, the admins can change observations in ways that are forbidden under normal circumstances.
The env var
OVERRIDE_OBSERVATION_STATUS
should be set toTRUE
.Pull request checklist
Please check if your PR fulfills the following requirements:
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
Admins cannot edit all the fields in observations
Issue Number: #181285745
What is the new behavior?
Admins can now edit all the fields in observations
Does this introduce a breaking change?
Other information