Module: ContentBlock::Publishable

Extended by:
ActiveSupport::Concern
Included in:
ContentBlock
Defined in:
app/models/concerns/content_block/publishable.rb

Overview

DHH-style concern for publish/unpublish behavior. Extracts publishing logic to a reusable, self-contained module.

Examples:

class ContentBlock < ApplicationRecord
  include ContentBlock::Publishable
end

block.publish(user: Current.user)
block.unpublish(user: Current.user)
ContentBlock.published
ContentBlock.unpublished

Instance Method Summary collapse

Instance Method Details

#publish(user: nil) ⇒ Boolean

Publish the content block.

Parameters:

  • user (User, nil) (defaults to: nil)

    The user performing the action

Returns:

  • (Boolean)

    True if successfully published



26
27
28
29
30
31
32
33
34
# File 'app/models/concerns/content_block/publishable.rb', line 26

def publish(user: nil)
  transaction do
    self.updated_by = user if user && respond_to?(:updated_by=)
    update!(published: true)
  end
  true
rescue ActiveRecord::RecordInvalid
  false
end

#published?Boolean

Check if content block is currently published.

Returns:

  • (Boolean)


51
52
53
# File 'app/models/concerns/content_block/publishable.rb', line 51

def published?
  published == true
end

#unpublish(user: nil) ⇒ Boolean

Unpublish the content block.

Parameters:

  • user (User, nil) (defaults to: nil)

    The user performing the action

Returns:

  • (Boolean)

    True if successfully unpublished



39
40
41
42
43
44
45
46
47
# File 'app/models/concerns/content_block/publishable.rb', line 39

def unpublish(user: nil)
  transaction do
    self.updated_by = user if user && respond_to?(:updated_by=)
    update!(published: false)
  end
  true
rescue ActiveRecord::RecordInvalid
  false
end