Project

sweetify

0.01
No commit activity in last 3 years
No release in over 3 years
Sweetify allows you to easily use SweetAlert within your controllers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Sweetify - SweetAlert for Rails

Gem

This gem allows you to use SweetAlert or SweetAlert2 for your flash messages. See the examples below, to see how to use it

Installation

Note: This package does not provide the client-side files of SweetAlert. You have to provide them yourself.

Add this line to your application's Gemfile:

gem 'sweetify'

And then execute:

$ bundle

Next up, create the file sweetify.rb in your initializers to specify the library you are using (SweetAlert or SweetAlert2):

# possible options: 'sweetalert', 'sweetalert2' - default is 'sweetalert2'
Sweetify.sweetalert_library = 'sweetalert2'

Next add the following line to the bottom of your application's layout file:

...

<%= render 'sweetify/alert' %>

</body>
</html>

You have to restart your rails server after installing the gem

Usage

You can now easily create alerts in your controllers with any of the following methods provided by Sweetify:

# Base Method, no type specified
sweetalert(text, title = '', opts = {})

# Additional methods with the type already defined
sweetalert_info(text, title = '', opts = {})
sweetalert_success(text, title = '', opts = {})
sweetalert_error(text, title = '', opts = {})
sweetalert_warning(text, title = '', opts = {})

Example Usage

# POST /resource
def create
    sweetalert_success('Your resource is created and available.', 'Successfully created', persistent: 'Awesome!')
    redirect_to resource_path
end

That would look like this after the redirect:

Example Alert

Options

By default, all alerts will dismiss after a sensible default number of seconds.

Default Options set by Sweetify:

{
    showConfirmButton: false,
    timer:             2000,
    allowOutsideClick: true,
    confirmButtonText: 'OK'
}

The following special options provided by Sweetify are available:

# Shows the alert with a button, but will still close automatically
sweetalert('Text', 'Title', button: true)
sweetalert('Text', 'Title', button: 'Awesome!') # Custom text for the button

# Shows the alert with a button and only closes if the button is pressed
sweetalert('Text', 'Title', persistent: true)
sweetalert('Text', 'Title', persistent: 'Awesome!') # Custom text for the button

You also can use any other available option that SweetAlert accepts:

sweetalert_info('Here is a custom image', 'Sweet!', imageUrl: 'images/thumbs-up.jpg', timer: 5000)

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help: