Sha256: b74f6919a7e9f987a99eac2327ccdab6970970b4ebc47aedaf1f7d4c427bc2d9

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module Blogs
    module Admin
      # This command is executed when the user changes a Blog from the admin
      # panel.
      class UpdatePost < Decidim::Command
        # Initializes a UpdateBlog Command.
        #
        # form - The form from which to get the data.
        # blog - The current instance of the page to be updated.
        def initialize(form, post, user)
          @form = form
          @post = post
          @user = user
        end

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

          transaction do
            update_post!
          end

          broadcast(:ok, post)
        end

        private

        attr_reader :form, :post

        def update_post!
          Decidim.traceability.update!(
            post,
            @user,
            attributes
          )
        end

        def attributes
          {
            title: form.title,
            body: form.body,
            published_at: form.published_at,
            author: form.author
          }.reject do |attribute, value|
            value.blank? && attribute == :published_at
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-blogs-0.28.4 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.3 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.2 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.1 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.0 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.0.rc5 app/commands/decidim/blogs/admin/update_post.rb
decidim-blogs-0.28.0.rc4 app/commands/decidim/blogs/admin/update_post.rb