app/commands/decidim/assemblies/admin/update_assembly.rb in decidim-assemblies-0.16.0 vs app/commands/decidim/assemblies/admin/update_assembly.rb in decidim-assemblies-0.16.1

- old
+ new

@@ -11,10 +11,11 @@ # assembly - the Assembly to update # form - A form object with the params. def initialize(assembly, form) @assembly = assembly @form = form + @parent = Assembly.find_by(id: @assembly.parent) end # Executes the command. Broadcasts these events: # # - :ok when everything is valid. @@ -40,10 +41,11 @@ attr_reader :form, :assembly def update_assembly @assembly.assign_attributes(attributes) save_assembly if @assembly.valid? + update_children_count end def save_assembly transaction do @assembly.save! @@ -104,9 +106,18 @@ @participatory_processes ||= assembly.participatory_space_sibling_scope(:participatory_processes).where(id: @form.participatory_processes_ids) end def link_participatory_processes(assembly) assembly.link_participatory_spaces_resources(participatory_processes(assembly), "included_participatory_processes") + end + + # Resets the children counter cache to its correct value using an SQL count query. + # Fixes Rails decrementing twice error when updating the parent to nil. + # + # Returns nothing. + def update_children_count + return unless @parent + Assembly.reset_counters(@parent.id, :children_count) end end end end end