Sha256: 2d7066435b31d828087995a42eb3bdf4ef12abd2b3caf81e69dfb2abe9e4a1be

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 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. Default to `"secondary"`
  #
  class AnnouncementCell < Decidim::ViewModel
    include Decidim::SanitizeHelper

    def show
      return if clean_body.blank? && clean_announcement.blank?

      render :show
    end

    private

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

    def callout_class
      options[:callout_class] ||= "secondary"
    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

11 entries across 11 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.8 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.7 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.6 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.5 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.4 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.3 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.2 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.1 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.0 app/cells/decidim/announcement_cell.rb
decidim-core-0.27.0.rc2 app/cells/decidim/announcement_cell.rb