AlertMessages for Hanami

A helper to add alert messages (including flash ... aah! messages) to an Hanami application.

Status

Gem Version Build Status Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'alert-messages-for-hanami', '~> 0.1'

Usage

Configuration

In your application.rb include

require 'alert-messages-for-hanami'

and then

# Configure the code that will yield each time Admin::Action is included
# This is useful for sharing common functionality
#
# See: http://www.rubydoc.info/gems/hanami-controller#Configuration
controller.prepare do
    # include MyAuthentication # included in all the actions
    # before :authenticate!    # run an authentication before callback

    include CabezaDeTermo::AlertMessages::Behaviour
end

Adding messages in your actions

To add a message to show in the current page, do

module Web::Controllers::Login
    class Create
        include Web::Action

        def call(params)
            alert_messages[:info] << 'You know nothing, John Snow'
        end
    end
end

To add a message to show in next request or redirect, first enable sessions in your application and then you can do

module Web::Controllers::Login
    class Create
        def call(params)
            alert_messages.flash_at(:info) << 'Flash! aah ... king of the immmmpossible'
        end
    end
end

Showing messages

In your template do something like

- alert_messages[:success].each do |success_message|
    .alert.alert-success role="alert"
        = success_message

- alert_messages[:info].each do |info_message|
    .alert.alert-info role="alert"
        = info_message

- alert_messages[:warnings].each do |warning_message|
    .alert.alert-warning role="alert"
        = warning_message

- alert_messages[:errors].each do |error_message|
    .alert.alert-danger role="alert"
        = error_message

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cabeza-de-termo/alert-messages.

License

The gem is available as open source under the terms of the MIT License.