Sha256: b87a8a686bbb5ea96f777e038fc7e7abf41fd41c516bfdd2f8837ec6872f2080

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 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
    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_styles
      return unless options[:callout_styles]

      options[:callout_styles]
    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

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/cells/decidim/announcement_cell.rb
decidim-core-0.29.0 app/cells/decidim/announcement_cell.rb
decidim-core-0.29.0.rc4 app/cells/decidim/announcement_cell.rb
decidim-core-0.29.0.rc3 app/cells/decidim/announcement_cell.rb
decidim-core-0.29.0.rc2 app/cells/decidim/announcement_cell.rb
decidim-core-0.29.0.rc1 app/cells/decidim/announcement_cell.rb