Sha256: 3984cbe4a0e9c564f8f8a60283ea5d340b4238632b29bfa360ced9630a27e9c4

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Decidim
  module Debates
    # A command with all the business logic when a user updates a debate.
    class UpdateDebate < Rectify::Command
      # Public: Initializes the command.
      #
      # form         - A form object with the params.
      def initialize(form)
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid, together with the debate.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?
        return broadcast(:invalid) unless form.debate.editable_by?(form.current_user)

        update_debate
        broadcast(:ok, @debate)
      end

      private

      attr_reader :form

      def update_debate
        @debate = Decidim.traceability.update!(
          @form.debate,
          @form.current_user,
          attributes,
          visibility: "public-only"
        )
      end

      def attributes
        parsed_title = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.title, current_organization: form.current_organization).rewrite
        parsed_description = Decidim::ContentProcessor.parse_with_processor(:hashtag, form.description, current_organization: form.current_organization).rewrite
        {
          category: form.category,
          title: {
            I18n.locale => parsed_title
          },
          description: {
            I18n.locale => parsed_description
          },
          scope: form.scope
        }
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

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