Class: Decidim::DecidimAwesome::ContentBlocks::RichTextCell

Inherits:
ContentBlocks::BaseCell
  • Object
show all
Defined in:
app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb

Constant Summary collapse

GRID_CLASSES =
{
  2 => "md:grid-cols-2",
  3 => "md:grid-cols-3",
  4 => "md:grid-cols-4",
  5 => "md:grid-cols-5"
}.freeze
PLACEMENT_CLASSES =
{
  "cover_center" => "awesome-rich-text--bg-cover-center",
  "cover_top" => "awesome-rich-text--bg-cover-top",
  "cover_bottom" => "awesome-rich-text--bg-cover-bottom",
  "contain_center" => "awesome-rich-text--bg-contain-center",
  "repeat" => "awesome-rich-text--bg-repeat"
}.freeze

Instance Method Summary collapse

Instance Method Details

#block_idObject



13
14
15
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 13

def block_id
  sanitize_id(model.settings.block_id).presence || "awesome-rich-text-#{model.id}"
end

#column_background_color(column) ⇒ Object



35
36
37
38
39
40
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 35

def column_background_color(column)
  color = column.background_color.presence
  return unless color&.match?(/\A#(?:[0-9a-fA-F]{3}){1,2}\z/)

  color
end

#column_background_image(index) ⇒ Object



42
43
44
45
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 42

def column_background_image(index)
  @column_background_images ||= {}
  @column_background_images[index] ||= model.images_container.attached_uploader(:"background_image_#{index}").url
end

#column_placement_class(column, index) ⇒ Object



60
61
62
63
64
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 60

def column_placement_class(column, index)
  return if column_background_image(index).blank?

  PLACEMENT_CLASSES[column.background_image_placement] || PLACEMENT_CLASSES["cover_center"]
end

#column_styles(column, index) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 47

def column_styles(column, index)
  color = column_background_color(column)
  image_url = column_background_image(index)

  styles = []
  styles << "--awesome-rich-text-bg: #{color}" if color
  if image_url.present?
    escaped_url = image_url.gsub("'", "%27").gsub(")", "%29")
    styles << "--awesome-rich-text-bg-image: url('#{escaped_url}')"
  end
  styles.join("; ")
end

#columnsObject



29
30
31
32
33
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 29

def columns
  @columns ||= RichTextColumn.from_settings(model.settings.columns).select do |col|
    translated_attribute(col.body).present?
  end
end

#extra_classesObject



17
18
19
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 17

def extra_classes
  "awesome-rich-text"
end

#grid_classObject



66
67
68
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 66

def grid_class
  GRID_CLASSES[columns.size]
end

#i18n_scopeObject



21
22
23
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 21

def i18n_scope
  "decidim.decidim_awesome.content_blocks.rich_text"
end

#rendered_body(column) ⇒ Object

Returns the rendered body HTML for a column, applying server-side restrictions for non-authenticated users when configured.



72
73
74
75
76
77
78
79
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 72

def rendered_body(column)
  html = decidim_sanitize_editor_admin(translated_attribute(column.body))
  return html if current_user

  html = strip_videos(html) if column.restrict_videos
  html = strip_links(html) if column.restrict_links
  html
end

#showObject



7
8
9
10
11
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 7

def show
  return if columns.empty?

  super
end

#titleObject



25
26
27
# File 'app/cells/decidim/decidim_awesome/content_blocks/rich_text_cell.rb', line 25

def title
  translated_attribute(model.settings.title).presence
end