Skip to content

Commit

Permalink
add setting to support both sweetalert and sweetalert2
Browse files Browse the repository at this point in the history
resolves #6 and #7
  • Loading branch information
Atrox committed May 5, 2019
1 parent 299e91e commit d4dbc5f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gem](https://img.shields.io/gem/v/sweetify.svg?style=flat-square)](https://rubygems.org/gems/sweetify)

This gem allows you to use [SweetAlert](http://t4t5.github.io/sweetalert/) for your flash messages.
This gem allows you to use [SweetAlert](http://t4t5.github.io/sweetalert/) or [SweetAlert2](https://github.com/limonte/sweetalert2) for your flash messages.
_See the examples below, to see how to use it_

## Installation
Expand All @@ -19,6 +19,12 @@ And then execute:
$ bundle
```

Next up, create the file `sweetify.rb` in your initializers to specify the library you are using (SweetAlert or SweetAlert2):
```ruby
# 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:
```html
...
Expand Down
4 changes: 2 additions & 2 deletions app/views/sweetify/_alert.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if flash[:sweetify].present? %>
<script>
swal(<%= flash[:sweetify].html_safe %>)
<%= Sweetify.render(flash[:sweetify]) %>
</script>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions lib/sweetify.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'sweetify/engine'
require 'sweetify/sweetalert'
require 'sweetify/render'
13 changes: 13 additions & 0 deletions lib/sweetify/render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sweetify
class << self
attr_writer :sweetalert_library

def sweetalert_library
@sweetalert_library || 'sweetalert2'
end

def render(json)
(sweetalert_library == 'sweetalert2' ? "Swal.fire(#{json})" : "swal(#{json})").html_safe
end
end
end
12 changes: 12 additions & 0 deletions lib/sweetify/sweetalert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def sweetalert(text, title = '', opts = {})
opts.delete(:persistent)
end

# sweetalert changes
if Sweetify.sweetalert_library == 'sweetalert'
opts[:icon] = opts.delete(:type)
opts[:closeOnClickOutside] = opts.delete(:allowOutsideClick)

if opts.delete(:showConfirmButton)
opts[:button] = opts[:confirmButtonText]
else
opts[:button] = false
end
end

flash_config(opts)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sweetify/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sweetify
VERSION = '1.0.0'.freeze
VERSION = '2.0.0'.freeze
end

0 comments on commit d4dbc5f

Please sign in to comment.