app/forms/decidim/initiatives/admin/initiative_form.rb in decidim-initiatives-0.22.0 vs app/forms/decidim/initiatives/admin/initiative_form.rb in decidim-initiatives-0.23.0

- old
+ new

@@ -17,11 +17,11 @@ attribute :area_id, Integer attribute :signature_type, String attribute :signature_start_date, Decidim::Attributes::LocalizedDate attribute :signature_end_date, Decidim::Attributes::LocalizedDate attribute :hashtag, String - attribute :offline_votes, Integer + attribute :offline_votes, Hash[String => Integer] attribute :state, String attribute :attachment, AttachmentForm validates :title, :description, presence: true validates :area, presence: true, if: ->(form) { form.area_id.present? } @@ -33,22 +33,33 @@ } validates :signature_end_date, date: { after: Date.current }, if: lambda { |form| form.signature_start_date.blank? && form.signature_end_date.present? } - validates :offline_votes, - numericality: { - only_integer: true, - greater_than: 0 - }, allow_blank: true - validate :notify_missing_attachment_if_errored validate :area_is_not_removed def map_model(model) self.type_id = model.type.id self.decidim_scope_id = model.scope&.id + self.offline_votes = model.offline_votes + + if offline_votes.empty? + self.offline_votes = model.votable_initiative_type_scopes.each_with_object({}) do |initiative_scope_type, all_votes| + all_votes[initiative_scope_type.decidim_scopes_id || "global"] = [0, initiative_scope_type.scope_name] + end + else + offline_votes.delete("total") + self.offline_votes = offline_votes.each_with_object({}) do |(decidim_scope_id, votes), all_votes| + scope_name = model.votable_initiative_type_scopes.find do |initiative_scope_type| + initiative_scope_type.global_scope? && decidim_scope_id == "global" || + initiative_scope_type.decidim_scopes_id == decidim_scope_id.to_i + end.scope_name + + all_votes[decidim_scope_id || "global"] = [votes, scope_name] + end + end end def signature_type_updatable? @signature_type_updatable ||= begin state ||= context.initiative.state @@ -65,10 +76,10 @@ end def scoped_type_id return unless type && decidim_scope_id - type.scopes.find_by!(decidim_scopes_id: decidim_scope_id).id + type.scopes.find_by(decidim_scopes_id: decidim_scope_id.presence).id end def area @area ||= current_organization.areas.find_by(id: area_id) end