Fix disallow enabling ParticipatoryText with existing proposals#5134
Conversation
|
Shouldn't we also add some kind of backend validation? |
|
@oriolgual I'm having problems making the validation in: It turns out that instance.settings.participatory_texts_enabled == instance.reload.settings.participatory_texts_enabledAnd the solutions I've come up are not compatible with the specs:
component.on(:update) do |instance|
Decidim::Component.connection.reconnect! # instance.reload is not enough to get the db's setting value
db_setting_value = Decidim::Component.find(instance.id).settings.participatory_texts_enabled
next if db_setting_value == instance.settings.participatory_texts_enabled
raise "Can't update ParticipatoryText setting when there are proposals" if Decidim::Proposals::Proposal.where(component: instance).any?
endPG::NoActiveSqlTransaction: ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks
: ROLLBACK TO SAVEPOINT active_record_1
component.on(:update) do |instance|
return unless (settings_changeset = instance.versions.last.changeset["settings"])
before = settings_changeset.first["global"]["participatory_texts_enabled"]
after = settings_changeset.last["global"]["participatory_texts_enabled"]
raise "Can't update ParticipatoryText setting when there are proposals" if before != after && Decidim::Proposals::Proposal.where(component: instance).any?
end Failure/Error: return unless (settings_changeset = instance.versions.last.changeset["settings"])
NoMethodError:
undefined method `changeset' for nil:NilClassThe second could be fixed by creating Would it be acceptable to move the validation for to the |
Yes, that seems OK! |
…ntForm No custom error message has been added after this validation fails because the checkbox is already being disabled and a help text is being added on the frontend.
|
Is everything finished here @aitorlb ? |
|
@oriolgual I was just about to push a little refactor if that is okay with you: def must_be_able_to_change_participatory_texts_setting
return unless settings[:participatory_texts_enabled] &.!= component.settings.participatory_texts_enabled
errors.add(:settings) if Decidim::Proposals::Proposal.where(component: component).any?
endI just found out that you can use the |
LOL, what does |
|
Oh, I guess it doesn't crash if |
|
It would be equivalent to the following: settings[:participatory_texts_enabled] && settings[:participatory_texts_enabled] != component.settings.participatory_texts_enabledIt's just a little shorter, hehe... |
The problem is not that it crashes but that it gives a false positive: |
🎩 What? Why?
📌 Related Issues
📋 Subtasks
CHANGELOGentry📷 Screenshots (optional)