Class: ContentBlock

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Publishable, Searchable, Versionable
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, Versionable

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Versionable

#current_version_number, #rollback_to_version!

Methods included from Publishable

#publish, #published?, #unpublish

Class Method Details

.accessible_by(_user) ⇒ Object



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

def self.accessible_by(_user)
  all
end

.action_text_available?Boolean

Returns:

  • (Boolean)


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

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)


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

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



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

def self.by_key
  alphabetically
end

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



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

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)


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

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

#can_edit?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#content_bodyObject



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

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



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

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