Sha256: c1c554b8d26fc97dbb170f5ff4f2af885d3a6fa7526b3b5677a9e82979a7947a

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # Updates the newsletter given form data.
    class UpdateNewsletter < Decidim::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

12 entries across 12 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.8 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.7 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.6 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.5 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.4 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.3 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.2 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.1 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.0 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.0.rc2 app/commands/decidim/admin/update_newsletter.rb
decidim-admin-0.27.0.rc1 app/commands/decidim/admin/update_newsletter.rb