Sha256: defd4f3bed44653b84bf273aa4f2a0141a7dad84b327b0a5efb7cc93e13ca7f2

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

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

        mimic :budget

        translatable_attribute :title, String
        attribute :weight, Integer, default: 0
        translatable_attribute :description, String
        attribute :total_budget, Integer, default: 0
        attribute :decidim_scope_id, Integer

        validates :title, translatable_presence: true
        validates :weight, numericality: { greater_than_or_equal_to: 0 }
        validates :total_budget, numericality: { greater_than: 0 }
        validates :scope, presence: true, if: ->(form) { form.decidim_scope_id.present? }
        validates :decidim_scope_id, scope_belongs_to_component: true, if: ->(form) { form.decidim_scope_id.present? }

        # Finds the Scope from the given decidim_scope_id, uses the compoenent scope if missing.
        #
        # Returns a Decidim::Scope
        def scope
          @scope ||= @attributes["decidim_scope_id"].value ? current_component.scopes.find_by(id: @attributes["decidim_scope_id"].value) : current_component.scope
        end

        # Scope identifier
        #
        # Returns the scope identifier related to the meeting
        def decidim_scope_id
          super || scope&.id
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-budgets-0.28.4 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.9 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.3 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.8 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.2 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.7 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.1 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.6 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.0 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.5 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.0.rc5 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.28.0.rc4 app/forms/decidim/budgets/admin/budget_form.rb
decidim-budgets-0.27.4 app/forms/decidim/budgets/admin/budget_form.rb