Sha256: 074e322d54cdfdd4d3a733f91d764a06b919cd5f4651ccb02abdcfa4b4c13a6e

Contents?: true

Size: 1.34 KB

Versions: 27

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Decidim
  module Blogs
    module Admin
      # This command is executed when the user creates a Post from the admin
      # panel.
      class CreatePost < Rectify::Command
        def initialize(form, current_user)
          @form = form
          @current_user = current_user
        end

        # Creates the post if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if @form.invalid?

          transaction do
            create_post!
            send_notification
          end

          broadcast(:ok, @post)
        end

        private

        def create_post!
          attributes = {
            title: @form.title,
            body: @form.body,
            component: @form.current_component,
            author: @current_user
          }

          @post = Decidim.traceability.create!(
            Post,
            @current_user,
            attributes,
            visibility: "all"
          )
        end

        def send_notification
          Decidim::EventsManager.publish(
            event: "decidim.events.blogs.post_created",
            event_class: Decidim::Blogs::CreatePostEvent,
            resource: @post,
            followers: @post.participatory_space.followers
          )
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
decidim-blogs-0.24.3 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.6 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.24.2 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.5 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.24.1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.24.0 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.24.0.rc2 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.4 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.24.0.rc1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.3 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.2 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.1.rc1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.23.0 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.22.0 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.21.0 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.20.1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.20.0 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.19.1 app/commands/decidim/blogs/admin/create_post.rb
decidim-blogs-0.18.1 app/commands/decidim/blogs/admin/create_post.rb