Sha256: 7a4222c0ff765343ba7443b9870a073af66f2f3c38f4e4f9824b9b58427163c6

Contents?: true

Size: 1.73 KB

Versions: 28

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Decidim
  module Budgets
    module Admin
      # This class holds a Form to create/update projects from Decidim's admin panel.
      class ProjectForm < Decidim::Form
        include TranslatableAttributes
        include TranslationsHelper

        translatable_attribute :title, String
        translatable_attribute :description, String

        attribute :budget, Integer
        attribute :decidim_scope_id, Integer
        attribute :decidim_category_id, Integer
        attribute :proposal_ids, Array[Integer]

        validates :title, translatable_presence: true
        validates :description, translatable_presence: true
        validates :budget, presence: true, numericality: { greater_than: 0 }

        validates :scope, presence: true, if: ->(form) { form.decidim_scope_id.present? }
        validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? }

        def map_model(model)
          self.proposal_ids = model.linked_resources(:proposals, "included_proposals").pluck(:id)

          return unless model.categorization

          self.decidim_category_id = model.categorization.decidim_category_id
        end

        def process_scope
          current_feature.participatory_space.scope
        end

        def proposals
          @proposals ||= Decidim.find_resource_manifest(:proposals).try(:resource_scope, context.current_feature)&.order(title: :asc)&.pluck(:title, :id)
        end

        def scope
          @scope ||= current_organization.scopes.where(id: decidim_scope_id).first || process_scope
        end

        def category
          @category ||= context.current_feature.categories.where(id: decidim_category_id).first
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
decidim-budgets-0.8.4 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.8.3 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.8.2 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.8.1 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.8.0 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.7.4 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.7.3 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.7.2 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.7.1 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.7.0 app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.6.8 app/forms/decidim/budgets/admin/project_form.rb
decidim-0.6.8 decidim-budgets/app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.6.7 app/forms/decidim/budgets/admin/project_form.rb
decidim-0.6.7 decidim-budgets/app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.6.6 app/forms/decidim/budgets/admin/project_form.rb
decidim-0.6.6 decidim-budgets/app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.6.5 app/forms/decidim/budgets/admin/project_form.rb
decidim-0.6.5 decidim-budgets/app/forms/decidim/budgets/admin/project_form.rb
decidim-budgets-0.6.4 app/forms/decidim/budgets/admin/project_form.rb
decidim-0.6.4 decidim-budgets/app/forms/decidim/budgets/admin/project_form.rb