Sha256: d10198f6fb3cfbdc57156d5a27c958d9d36a8847a3f1768975bc12f27e43fa7b

Contents?: true

Size: 1.08 KB

Versions: 25

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    # This class holds a Form to close debates from Decidim's public views.
    class CloseDebateForm < Decidim::Form
      mimic :debate

      attribute :conclusions, Decidim::Attributes::CleanString
      attribute :debate, Debate

      validates :debate, presence: true
      validates :conclusions, presence: true, length: { minimum: 10, maximum: 10_000 }
      validate :user_can_close_debate

      def closed_at
        debate&.closed_at || Time.current
      end

      def map_model(debate)
        super
        self.debate = debate

        # Debates can be translated in different languages from the admin but
        # the public form doesn't allow it. When a user closes a debate the
        # user locale is taken as the text locale.
        self.conclusions = debate.conclusions&.values&.first
      end

      private

      def user_can_close_debate
        return if !debate || !debate.respond_to?(:closeable_by?)

        errors.add(:debate, :invalid) unless debate.closeable_by?(current_user)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-debates-0.26.10 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.9 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.8 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.7 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.5 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.4 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.3 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.2 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.1 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.0 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.0.rc2 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.26.0.rc1 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.2 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.1 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.0 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.0.rc4 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.0.rc3 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.0.rc2 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.25.0.rc1 app/forms/decidim/debates/close_debate_form.rb
decidim-debates-0.24.3 app/forms/decidim/debates/close_debate_form.rb