Sha256: 599c41c711bbb413d3eb377212f49d2b77a1ad46c325c6b279ad880ba4ba480d

Contents?: true

Size: 1.43 KB

Versions: 12

Compression:

Stored size: 1.43 KB

Contents

module ExpressTemplates
  module Components
    module Capabilities
      # Adds the capability for a component to only render
      # its markup when a condition to be evaluated in the
      # view is true.
      #
      # Example:
      #
      #     class PageHeader < ExpressTemplates::Components::Base
      #       include ExpressTemplates::Components::Capabilities::Conditionality
      #
      #         emits {
      #           h1 { content_for(:page_header) }
      #         }
      #
      #         only_if -> { content_for?(:page_header) }
      #
      #       end
      #     end
      #
      # The condition supplied to only if in the form of a proc
      # is evaluated in the view context.
      #
      # The component will render an empty string if the proc returns false.
      module Conditionality
        def self.included(base)
          base.class_eval do
            extend ClassMethods
          end
        end

        module ClassMethods

          def condition_proc
            @condition_proc
          end

          def only_if condition_proc
            @condition_proc = Proc.from_source "-> {!(#{condition_proc.source_body})}"
            inner_src = self[:markup]
            fragment_src = %Q(-> {
  unless_block(Proc.from_source(#{@condition_proc.source.inspect})) {
    #{inner_src.source_body}
  }
})
            _store :markup, Proc.from_source(fragment_src)
          end

        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
express_admin-1.2.1 vendor/gems/express_templates/lib/express_templates/components/capabilities/conditionality.rb
express_admin-1.2.0 vendor/gems/express_templates/lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.5.0 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.4.2 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.4.1 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.4.0 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.6 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.5 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.4 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.2 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.1 lib/express_templates/components/capabilities/conditionality.rb
express_templates-0.3.0 lib/express_templates/components/capabilities/conditionality.rb