Sha256: dba61435a8f85451f647740d69788ac0c5b30be70cf99d8723cdbe124599eaa2

Contents?: true

Size: 1.53 KB

Versions: 34

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # Updates the newsletter given form data.
    class UpdateNewsletter < Rectify::Command
      # Initializes the command.
      #
      # newsletter - The Newsletter to update.
      # form       - The form object containing the data to update.
      # user       - The user that updates the newsletter.
      def initialize(newsletter, form, user)
        @newsletter = newsletter
        @content_block = newsletter.template
        @form = form
        @user = user
        @organization = user.organization
      end

      def call
        return broadcast(:invalid) unless form.valid?
        return broadcast(:invalid) if newsletter.sent?
        return broadcast(:invalid) unless organization == newsletter.organization

        transaction do
          update_newsletter
          update_content_block
        end

        broadcast(:ok, newsletter)
      end

      private

      attr_reader :user, :newsletter, :content_block, :organization, :form

      def update_newsletter
        @newsletter = Decidim.traceability.update!(
          newsletter,
          user,
          subject: form.subject,
          author: user
        )
      end

      def update_content_block
        UpdateContentBlock.call(form, content_block, user) do
          on(:ok) do |content_block|
            @content_block = content_block
          end
          on(:invalid) do
            raise "There was a problem persisting the changes to the content block"
          end
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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