app/forms/decidim/budgets/admin/project_form.rb in decidim-budgets-0.26.10 vs app/forms/decidim/budgets/admin/project_form.rb in decidim-budgets-0.27.0.rc1
- old
+ new
@@ -11,10 +11,13 @@
include Decidim::ApplicationHelper
translatable_attribute :title, String
translatable_attribute :description, String
+ attribute :address, String
+ attribute :latitude, Float
+ attribute :longitude, Float
attribute :budget_amount, Integer
attribute :decidim_scope_id, Integer
attribute :decidim_category_id, Integer
attribute :proposal_ids, Array[Integer]
attribute :attachment, AttachmentForm
@@ -23,18 +26,19 @@
attachments_attribute :photos
validates :title, translatable_presence: true
validates :description, translatable_presence: true
validates :budget_amount, presence: true, numericality: { greater_than: 0 }
-
+ validates :address, geocoding: true, if: ->(form) { form.has_address? && !form.geocoded? }
validates :category, presence: true, if: ->(form) { form.decidim_category_id.present? }
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? }
validate :notify_missing_attachment_if_errored
delegate :categories, to: :current_component
+ alias component current_component
def map_model(model)
self.proposal_ids = model.linked_resources(:proposals, "included_proposals").pluck(:id)
self.selected = model.selected?
@@ -47,10 +51,22 @@
@proposals ||= Decidim.find_resource_manifest(:proposals).try(:resource_scope, current_component)
&.where(id: proposal_ids)
&.order(title: :asc)
end
+ def geocoding_enabled?
+ Decidim::Map.available?(:geocoding) && current_component.settings.geocoding_enabled?
+ end
+
+ def has_address?
+ geocoding_enabled? && address.present?
+ end
+
+ def geocoded?
+ latitude.present? && longitude.present?
+ end
+
# Finds the Budget from the decidim_budgets_budget_id.
#
# Returns a Decidim::Budgets:Budget
def budget
@budget ||= context[:budget]
@@ -65,17 +81,17 @@
# Finds the Scope from the given decidim_scope_id, uses the component scope if missing.
#
# Returns a Decidim::Scope
def scope
- @scope ||= @decidim_scope_id ? current_component.scopes.find_by(id: @decidim_scope_id) : current_component.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 project
def decidim_scope_id
- @decidim_scope_id || scope&.id
+ super || scope&.id
end
private
# This method will add an error to the `attachment` field only if there's