Sha256: 0cc401c6d4cfa2c88cbea8549f85c33bd88e3b1c2b943867420edf6b0bf05e3b

Contents?: true

Size: 1.59 KB

Versions: 8

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Decidim
  # This cell renders an announcement
  #
  # The `model` is expected to be a Hash with two keys:
  #   - `body` is mandatory, its the message to show
  #   - `title` is mandatory, a title to show
  #
  # {
  #   title: "...", # mandatory
  #   body: "..." # mandatory
  # }
  #
  # It can also receive a single value to show as text. It can either be a String
  # or a value accepted by the `translated_attribute` method.
  #
  # As options, the cell accepts a Hash with these keys:
  #   - `callout_class`: The Css class to apply
  #
  class AnnouncementCell < Decidim::ViewModel
    include Decidim::SanitizeHelper

    def show
      return if blank_content?

      render :show
    end

    def blank_content?
      @blank_content ||= clean_body.blank? && clean_announcement.blank?
    end

    private

    def has_title?
      announcement.is_a?(Hash) && announcement.has_key?(:title)
    end

    def text
      has_title? ? clean_body : clean_announcement
    end

    def css_class
      return unless options[:callout_class]

      options[:callout_class]
    end

    def announcement
      model
    end

    def clean_title
      clean(announcement[:title])
    end

    def body
      return announcement.presence unless announcement.is_a?(Hash)

      announcement[:body].presence
    end

    def clean_body
      return unless body

      Array(body).map { |paragraph| tag.p(clean(paragraph)) }.join
    end

    def clean_announcement
      clean(announcement)
    end

    def clean(value)
      decidim_sanitize_admin(translated_attribute(value))
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.28.5 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.4 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.3 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.2 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.1 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.0 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.0.rc5 app/cells/decidim/announcement_cell.rb
decidim-core-0.28.0.rc4 app/cells/decidim/announcement_cell.rb