Sha256: 6cae341a383ff91f8db49734e197e1f6458fb685bd7d724cb31369b70ac6c09e

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module Refinery
  module Pages
    # Knows how to build the html for a section. A section is part of the visible html, that has
    # content wrapped in some particular markup. Construct with the relevant options, and then
    # call wrapped_html to get the resultant html.
    #
    # The content rendered will usually be the value of fallback_html, unless an override_html
    # is specified. However, on rendering, you can elect not display sections that have no
    # override_html by passing in false for can_use_fallback.
    #
    # Sections may be hidden, in which case they wont display at all.
    class SectionPresenter
      include ActionView::Helpers::TagHelper

      def initialize(initial_hash = {})
        initial_hash.map do |key, value|
          send("#{key}=", value)
        end
      end

      attr_reader :id, :fallback_html, :hidden
      alias_method :hidden?, :hidden
      attr_accessor :override_html

      def visible?
        !hidden?
      end

      def has_content?(can_use_fallback = true)
        visible? && content_html(can_use_fallback).present?
      end

      def wrapped_html(can_use_fallback = true)
        return if hidden?

        content = content_html(can_use_fallback)
        if content.present?
          wrap_content_in_tag(content)
        end
      end

      def hide
        self.hidden = true
      end

      def not_present_css_class
        "no_#{id}"
      end

    protected

      def content_html(can_use_fallback)
        override_html.presence || html_from_fallback(can_use_fallback)
      end

      def html_from_fallback(can_use_fallback)
        fallback_html.presence if can_use_fallback
      end

    private

      attr_writer :id, :fallback_html, :hidden

      def wrap_content_in_tag(content)
        content_tag(:section, content_tag(:div, content, :class => 'inner'), :id => id)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinerycms-pages-3.0.1 app/presenters/refinery/pages/section_presenter.rb
refinerycms-pages-3.0.0 app/presenters/refinery/pages/section_presenter.rb