Sha256: dbb02a1ca7e047a9d023f26f7d3a5b341b5e938a60f004be88a6951d44ce81ec

Contents?: true

Size: 928 Bytes

Versions: 6

Compression:

Stored size: 928 Bytes

Contents

module DismissibleBlocksHelper
  def render_dismissible_block(name, &block)
    unless dismissed?(name)
      if block_given?
        contents = capture(name, &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

6 entries across 6 versions & 1 rubygems

Version Path
dismissible_blocks-2.0.4 app/helpers/dismissible_blocks_helper.rb
dismissible_blocks-2.0.3 app/helpers/dismissible_blocks_helper.rb
dismissible_blocks-2.0.2 app/helpers/dismissible_blocks_helper.rb
dismissible_blocks-2.0.1 app/helpers/dismissible_blocks_helper.rb
dismissible_blocks-2.0.0 app/helpers/dismissible_blocks_helper.rb
dismissible_blocks-1.2.0 app/helpers/dismissible_blocks_helper.rb