app/forms/decidim/budgets/admin/project_form.rb in decidim-budgets-0.21.0 vs app/forms/decidim/budgets/admin/project_form.rb in decidim-budgets-0.22.0

- old
+ new

@@ -14,20 +14,25 @@ attribute :budget, Integer attribute :decidim_scope_id, Integer attribute :decidim_category_id, Integer attribute :proposal_ids, Array[Integer] + attribute :attachment, AttachmentForm + attribute :photos, Array[String] + attribute :add_photos, Array validates :title, translatable_presence: true validates :description, translatable_presence: true validates :budget, presence: true, numericality: { greater_than: 0 } validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? } validates :scope, presence: true, if: ->(form) { form.decidim_scope_id.present? } validate :scope_belongs_to_participatory_space_scope + validate :notify_missing_attachment_if_errored + delegate :categories, to: :current_component def map_model(model) self.proposal_ids = model.linked_resources(:proposals, "included_proposals").pluck(:id) @@ -36,13 +41,12 @@ self.decidim_category_id = model.categorization.decidim_category_id end def proposals @proposals ||= Decidim.find_resource_manifest(:proposals).try(:resource_scope, current_component) - &.published + &.where(id: proposal_ids) &.order(title: :asc) - &.map { |proposal| [present(proposal).title, proposal.id] } end # Finds the Category from the decidim_category_id. # # Returns a Decidim::Category @@ -66,9 +70,17 @@ private def scope_belongs_to_participatory_space_scope errors.add(:decidim_scope_id, :invalid) if current_participatory_space.out_of_scope?(scope) + end + + # This method will add an error to the `attachment` field only if there's + # any error in any other field. This is needed because when the form has + # an error, the attachment is lost, so we need a way to inform the user of + # this problem. + def notify_missing_attachment_if_errored + errors.add(:add_photos, :needs_to_be_reattached) if errors.any? && add_photos.present? end end end end end