Sha256: 8b67f728ab2be1aa27897c16f7246cea7959f70c553fde603eb27acdc554893e

Contents?: true

Size: 921 Bytes

Versions: 1

Compression:

Stored size: 921 Bytes

Contents

module DismissibleBlocks::Helper

  def render_dismissible_block(name, &block)
    unless dismissed?(name)
      if block_given?
        contents = capture(&block)
        add_block_name_to_attributes(contents, name)
      else
        raise DismissibleBlocks::ContentMissing
      end
    end
  end

  private

    def dismissed?(name)
      if respond_to?(:current_user)
        if !current_user.respond_to?(:dismissed_blocks)
          raise DismissibleBlocks::AttributeUnavailable
        elsif !current_user.dismissed_blocks.is_a?(Array)
          raise DismissibleBlocks::AttributeNotArray
        else
          current_user.dismissed_blocks.include?(name)
        end
      else
        raise DismissibleBlocks::ModelUnavailable
      end
    end

    def add_block_name_to_attributes(contents, name)
      contents.gsub! /(data-dismissible)(-hide)?(?!-)/, "\\1\\2='#{name}'"
      contents.html_safe
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dismissible_blocks-0.0.2 lib/dismissible_blocks/helper.rb