Sha256: 3571c6abbc6dfbefe629ae7a1d9789aaa289405fd0215733528735363cb4de6c

Contents?: true

Size: 1.5 KB

Versions: 34

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # Creates a newsletter and assigns the right author and
    # organization.
    class CreateNewsletter < Rectify::Command
      # Initializes the command.
      #
      # form - The source fo data for this newsletter.
      # content_block - An instance of `Decidim::ContentBlock` that holds the
      #     newsletter attributes.
      # user - The User that authored this newsletter.
      def initialize(form, content_block, user)
        @form = form
        @content_block = content_block
        @user = user
      end

      def call
        return broadcast(:invalid) unless form.valid?

        transaction do
          create_newsletter
          create_content_block
        end

        broadcast(:ok, newsletter)
      end

      private

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

      def create_newsletter
        @newsletter = Decidim.traceability.create!(
          Newsletter,
          user,
          subject: form.subject,
          author: user,
          organization: user.organization
        )
      end

      def create_content_block
        UpdateContentBlock.call(form, content_block, user) do
          on(:ok) do |content_block|
            content_block.update(scoped_resource_id: newsletter.id)
            @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/create_newsletter.rb
decidim-admin-0.26.9 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.8 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.7 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.5 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.4 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.3 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.2 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.1 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.0 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.0.rc2 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.26.0.rc1 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.2 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.1 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.0 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.0.rc4 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.0.rc3 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.0.rc2 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.25.0.rc1 app/commands/decidim/admin/create_newsletter.rb
decidim-admin-0.24.3 app/commands/decidim/admin/create_newsletter.rb