app/forms/decidim/proposals/admin/proposal_base_form.rb in decidim-proposals-0.22.0 vs app/forms/decidim/proposals/admin/proposal_base_form.rb in decidim-proposals-0.23.0

- old
+ new

@@ -3,15 +3,15 @@ module Decidim module Proposals module Admin # A form object to be used when admin users want to create a proposal. class ProposalBaseForm < Decidim::Form + include Decidim::TranslatableAttributes include Decidim::ApplicationHelper + mimic :proposal - attribute :title, String - attribute :body, String attribute :address, String attribute :latitude, Float attribute :longitude, Float attribute :category_id, Integer attribute :scope_id, Integer @@ -21,29 +21,28 @@ attribute :meeting_id, Integer attribute :suggested_hashtags, Array[String] attribute :photos, Array[String] attribute :add_photos, Array - validates :title, :body, presence: true - validates :address, geocoding: true, if: -> { current_component.settings.geocoding_enabled? } + validates :address, geocoding: true, if: ->(form) { form.has_address? && !form.geocoded? } validates :category, presence: true, if: ->(form) { form.category_id.present? } validates :scope, presence: true, if: ->(form) { form.scope_id.present? } + validates :scope_id, scope_belongs_to_component: true, if: ->(form) { form.scope_id.present? } validates :meeting_as_author, presence: true, if: ->(form) { form.created_in_meeting? } - validate :scope_belongs_to_participatory_space_scope - validate :notify_missing_attachment_if_errored delegate :categories, to: :current_component def map_model(model) + body = translated_attribute(model.body) + @suggested_hashtags = Decidim::ContentRenderers::HashtagRenderer.new(body).extra_hashtags.map(&:name).map(&:downcase) + return unless model.categorization self.category_id = model.categorization.decidim_category_id self.scope_id = model.decidim_scope_id - - @suggested_hashtags = Decidim::ContentRenderers::HashtagRenderer.new(model.body).extra_hashtags.map(&:name).map(&:downcase) end alias component current_component # Finds the Category from the category_id. @@ -55,20 +54,32 @@ # Finds the Scope from the given decidim_scope_id, uses participatory space scope if missing. # # Returns a Decidim::Scope def scope - @scope ||= @scope_id ? current_participatory_space.scopes.find_by(id: @scope_id) : current_participatory_space.scope + @scope ||= @scope_id ? current_component.scopes.find_by(id: @scope_id) : current_component.scope end # Scope identifier # # Returns the scope identifier related to the proposal def scope_id @scope_id || scope&.id 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 Meetings of the current participatory space def meetings @meetings ||= Decidim.find_resource_manifest(:meetings).try(:resource_scope, current_component) &.order(title: :asc) end @@ -104,13 +115,9 @@ def component_suggested_hashtags @component_suggested_hashtags ||= ordered_hashtag_list(current_component.current_settings.suggested_hashtags) end private - - def scope_belongs_to_participatory_space_scope - errors.add(: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.