Class: ContentBlock

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Publishable, Searchable
Defined in:
app/models/content_block.rb

Overview

Public ContentBlock model exposed to the host app.

Direct Known Subclasses

RubyCms::ContentBlock

Defined Under Namespace

Modules: Publishable, Searchable

Constant Summary collapse

CONTENT_TYPES =
%w[text rich_text image link list].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Publishable

#publish, #published?, #unpublish

Class Method Details

.accessible_by(_user) ⇒ Object



70
71
72
# File 'app/models/content_block.rb', line 70

def self.accessible_by(_user)
  all
end

.action_text_available?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'app/models/content_block.rb', line 10

def self.action_text_available?
  return false unless defined?(::ActionText::RichText)

  ActiveRecord::Base.connection.data_source_exists?("action_text_rich_texts")
rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError,
       ActiveRecord::StatementInvalid
  false
end

.active_storage_available?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'app/models/content_block.rb', line 19

def self.active_storage_available?
  return false unless defined?(::ActiveStorage::Blob)

  c = ActiveRecord::Base.connection
  c.data_source_exists?("active_storage_blobs") &&
    c.data_source_exists?("active_storage_attachments")
rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError,
       ActiveRecord::StatementInvalid
  false
end

.by_keyObject



74
75
76
# File 'app/models/content_block.rb', line 74

def self.by_key
  alphabetically
end

.find_by_key_and_locale(key, locale: nil, default_locale: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/content_block.rb', line 99

def self.find_by_key_and_locale(key, locale: nil, default_locale: nil)
  locale ||= I18n.locale.to_s
  default_locale ||= begin
    I18n.default_locale.to_s
  rescue StandardError
    "en"
  end

  block = find_by(key: key.to_s, locale: locale.to_s)
  return block if block
  return nil if locale.to_s == default_locale.to_s

  find_by(key: key.to_s, locale: default_locale.to_s)
end

Instance Method Details

#can_delete?(user) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/content_block.rb', line 82

def can_delete?(user)
  user&.can?(:manage_content_blocks, record: self)
end

#can_edit?(user) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/content_block.rb', line 78

def can_edit?(user)
  user&.can?(:manage_content_blocks, record: self)
end

#content_bodyObject



90
91
92
93
94
95
96
97
# File 'app/models/content_block.rb', line 90

def content_body
  if content_type == "rich_text" && self.class.action_text_available? &&
     respond_to?(:rich_content)
    rich_content.to_s
  else
    content.to_s
  end
end

#record_update_by(user) ⇒ Object



86
87
88
# File 'app/models/content_block.rb', line 86

def record_update_by(user)
  self.updated_by = user if user
end