Sha256: bf573d5caa948e2a11fa9a6d0b782e4de59fea69c01448c81b728b36260187cb

Contents?: true

Size: 918 Bytes

Versions: 1

Compression:

Stored size: 918 Bytes

Contents

module DismissibleBlocksHelper
  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-1.1.0 app/helpers/dismissible_blocks_helper.rb