sweet-alert2-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +14 -0
- data/Gemfile +41 -0
- data/MIT-LICENSE +20 -0
- data/README.md +86 -0
- data/Rakefile +12 -0
- data/bin/rake +17 -0
- data/doc/after.png +0 -0
- data/doc/before.png +0 -0
- data/lib/assets/javascripts/sweet-alert2-rails.js +85 -0
- data/lib/sweet-alert2-rails.rb +6 -0
- data/lib/sweet-alert2-rails/rails/engine.rb +6 -0
- data/lib/sweet-alert2-rails/railtie.rb +9 -0
- data/lib/sweet-alert2-rails/version.rb +3 -0
- data/lib/sweet-alert2-rails/view_helpers.rb +39 -0
- data/spec/dummy/.gitignore +2 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +17 -0
- data/spec/dummy/app/assets/javascripts/application_turbolinks.js +18 -0
- data/spec/dummy/app/assets/javascripts/confirms.js +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +16 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/confirms_controller.rb +13 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/confirms/delete_cow.html.erb +1 -0
- data/spec/dummy/app/views/confirms/delete_cow.js.erb +1 -0
- data/spec/dummy/app/views/confirms/get_cow.html.erb +1 -0
- data/spec/dummy/app/views/confirms/index.html.erb +19 -0
- data/spec/dummy/app/views/confirms/pre_index.html.erb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +18 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +78 -0
- data/spec/dummy/config/environments/test.rb +43 -0
- data/spec/dummy/config/initializers/assets.rb +8 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/secret_token.rb +1 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/features/sweet_alert_confirm_spec.rb +116 -0
- data/spec/spec_helper.rb +50 -0
- data/sweet-alert2-rails.gemspec +24 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c598d499deadbc23ccc9674a88eec76a7239e08a
|
4
|
+
data.tar.gz: bf0c7c3e2b8254afe75005b614be32973a864adf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f19ce1e88cd8427460442d4b747b24ff79bd3e2bf4ab7db2ca478a86d52140fff5f10d925778d20427df4cf0662988a3986580551833fad3cb325055b76165cf
|
7
|
+
data.tar.gz: 67c6868197c24dd14c694926a01197104d9f79d13d3f1a4c6361eba5072be3b2e5053643b33d012077bc98782b329fbf5b53cb0a7d4cb1497622a80e151dd842
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in sweet_rails_confirm.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
15
|
+
|
16
|
+
rails_version = ENV['RAILS_VERSION'] || 'default'
|
17
|
+
|
18
|
+
rails = case rails_version
|
19
|
+
when 'master'
|
20
|
+
{ github: 'rails/rails' }
|
21
|
+
when 'default'
|
22
|
+
'>= 4.1.0'
|
23
|
+
else
|
24
|
+
"~> #{rails_version}"
|
25
|
+
end
|
26
|
+
|
27
|
+
gem 'rails', rails
|
28
|
+
gem 'turbolinks'
|
29
|
+
|
30
|
+
source 'https://rails-assets.org' do
|
31
|
+
gem 'rails-assets-sweetalert2', '~> 5.1.1'
|
32
|
+
end
|
33
|
+
|
34
|
+
group :development, :test do
|
35
|
+
gem 'jquery-rails'
|
36
|
+
gem 'test-unit'
|
37
|
+
gem 'sass-rails'
|
38
|
+
gem 'capybara'
|
39
|
+
gem 'capybara-webkit'
|
40
|
+
gem 'rake'
|
41
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Nicolas Blanco
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
sweet-alert2-rails
|
2
|
+
==================
|
3
|
+
|
4
|
+
This library easily integrates the SweetAlert 2 library into a Rails application.
|
5
|
+
|
6
|
+
Using the same Rails markup :
|
7
|
+
|
8
|
+
```Ruby
|
9
|
+
= link_to 'Delete', item, method: :delete, data: { confirm: 'Are you sure you want to delete this item?' }
|
10
|
+
```
|
11
|
+
|
12
|
+
You go from:
|
13
|
+
|
14
|
+
<img src="https://github.com/nicolasblanco/sweet-alert2-rails/blob/master/doc/before.png?raw=true" width="500" alt="before" />
|
15
|
+
|
16
|
+
To:
|
17
|
+
|
18
|
+
<img src="https://github.com/nicolasblanco/sweet-alert2-rails/blob/master/doc/after.png?raw=true" width="500" alt="after" />
|
19
|
+
|
20
|
+
It is heavily based on the work from Moises Viloria with SweetAlert. Some cleaning and refactoring were made to make it compatible with SweetAlert 2.
|
21
|
+
|
22
|
+
It replaces the browser confirm dialog when using `data: { confirm: 'message' }` with the link_to helper with a nice looking sweet alert.
|
23
|
+
|
24
|
+
## Requirements
|
25
|
+
|
26
|
+
Rails >= 4.2, tested with Rails 5
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Add the SweetAlert2 and sweet-alert2-rails to your Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'rails-assets-sweetalert2', '~> 5.1.1', source: 'https://rails-assets.org'
|
34
|
+
gem 'sweet-alert2-rails'
|
35
|
+
```
|
36
|
+
|
37
|
+
Add the following to application.js:
|
38
|
+
|
39
|
+
```javascript
|
40
|
+
//= require sweetalert2
|
41
|
+
//= require sweet-alert2-rails
|
42
|
+
```
|
43
|
+
Add the following to application.css:
|
44
|
+
|
45
|
+
```css
|
46
|
+
/*
|
47
|
+
*= require sweetalert2
|
48
|
+
*/
|
49
|
+
```
|
50
|
+
|
51
|
+
Try it:
|
52
|
+
|
53
|
+
```Ruby
|
54
|
+
= link_to "Delete", user, method: :delete, data: { confirm: 'Are you sure?' }
|
55
|
+
```
|
56
|
+
|
57
|
+
### Custom options
|
58
|
+
|
59
|
+
You may pass options in `data:`
|
60
|
+
```Ruby
|
61
|
+
data: {
|
62
|
+
confirm: 'Are you ready?',
|
63
|
+
'confirm-button-text': 'Im ready',
|
64
|
+
'cancel-button-text': 'No way',
|
65
|
+
'confirm-button-color': '#66CD00',
|
66
|
+
'sweet-alert-type': 'info',
|
67
|
+
text: 'This is a subtitle',
|
68
|
+
'image-url': '/pic.png'
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
Default options that will be used application wide so it is not necessary to set the option on each link. Put this object inside your app to override default options with the `sweetAlertConfirmConfig` object.
|
73
|
+
|
74
|
+
```Javascript
|
75
|
+
window.sweetAlertConfirmConfig = {
|
76
|
+
title: 'Are you sure?',
|
77
|
+
type: 'warning',
|
78
|
+
showCancelButton: true,
|
79
|
+
confirmButtonColor: '#DD6B55',
|
80
|
+
confirmButtonText: 'Ok'
|
81
|
+
};
|
82
|
+
```
|
83
|
+
|
84
|
+
## Contribute
|
85
|
+
|
86
|
+
Fork the repo & make a pull request with your fix or feature. Thanks :).
|
data/Rakefile
ADDED
data/bin/rake
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rake' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("rake", "rake")
|
data/doc/after.png
ADDED
Binary file
|
data/doc/before.png
ADDED
Binary file
|
@@ -0,0 +1,85 @@
|
|
1
|
+
var sweetAlertConfirmConfig = sweetAlertConfirmConfig || {}; // Add default config object
|
2
|
+
|
3
|
+
(function($) {
|
4
|
+
var sweetAlertConfirm = function(event) {
|
5
|
+
|
6
|
+
swalDefaultOptions = {
|
7
|
+
title: sweetAlertConfirmConfig.title || 'Are you sure?',
|
8
|
+
type: sweetAlertConfirmConfig.type || 'warning',
|
9
|
+
showCancelButton: sweetAlertConfirmConfig.showCancelButton || true,
|
10
|
+
confirmButtonText: sweetAlertConfirmConfig.confirmButtonText || "Ok",
|
11
|
+
cancelButtonText: sweetAlertConfirmConfig.cancelButtonText || "Cancel"
|
12
|
+
}
|
13
|
+
if (sweetAlertConfirmConfig.confirmButtonColor !== null) {
|
14
|
+
swalDefaultOptions.confirmButtonColor = sweetAlertConfirmConfig.confirmButtonColor
|
15
|
+
}
|
16
|
+
|
17
|
+
$linkToVerify = $(this);
|
18
|
+
var swalOptions = swalDefaultOptions;
|
19
|
+
var optionKeys = [
|
20
|
+
'text',
|
21
|
+
'showCancelButton',
|
22
|
+
'confirmButtonColor',
|
23
|
+
'cancelButtonColor',
|
24
|
+
'confirmButtonText',
|
25
|
+
'cancelButtonText',
|
26
|
+
'html',
|
27
|
+
'imageUrl',
|
28
|
+
'allowOutsideClick',
|
29
|
+
'customClass'
|
30
|
+
];
|
31
|
+
|
32
|
+
function sweetAlertConfirmedCallback() {
|
33
|
+
if ($linkToVerify.data().remote === true) {
|
34
|
+
$.rails.handleRemote($linkToVerify)
|
35
|
+
}
|
36
|
+
else if($linkToVerify.data().method !== undefined) {
|
37
|
+
$.rails.handleMethod($linkToVerify);
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
if($linkToVerify.attr('type') == 'submit') {
|
41
|
+
var name = $linkToVerify.attr('name'),
|
42
|
+
data = name ? {name: name, value: $linkToVerify.val()} : null;
|
43
|
+
$linkToVerify.closest('form').data('ujs:submit-button', data);
|
44
|
+
$linkToVerify.closest('form').submit();
|
45
|
+
}
|
46
|
+
else {
|
47
|
+
$linkToVerify.data('swal-confirmed', true).click();
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
if ($linkToVerify.data('swal-confirmed')) {
|
53
|
+
$linkToVerify.data('swal-confirmed', false);
|
54
|
+
return true;
|
55
|
+
}
|
56
|
+
|
57
|
+
$.each($linkToVerify.data(), function(key, val) {
|
58
|
+
if ($.inArray(key, optionKeys) >= 0) {
|
59
|
+
swalOptions[key] = val;
|
60
|
+
}
|
61
|
+
});
|
62
|
+
|
63
|
+
if ($linkToVerify.attr('data-sweet-alert-type')) {
|
64
|
+
swalOptions['type'] = $linkToVerify.attr('data-sweet-alert-type');
|
65
|
+
}
|
66
|
+
|
67
|
+
message = $linkToVerify.attr('data-sweet-alert-confirm')
|
68
|
+
swalOptions['title'] = message
|
69
|
+
swal(swalOptions).then(sweetAlertConfirmedCallback, function(dismiss) { return true; });
|
70
|
+
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
|
74
|
+
$(document).on('ready turbolinks:load page:update ajaxComplete', function() {
|
75
|
+
$('[data-sweet-alert-confirm]').on('click', sweetAlertConfirm)
|
76
|
+
});
|
77
|
+
|
78
|
+
$(document).on('ready turbolinks:load page:load', function() {
|
79
|
+
//To avoid "Uncaught TypeError: Cannot read property 'querySelector' of null" on turbolinks
|
80
|
+
if (typeof window.sweetAlertInitialize === 'function') {
|
81
|
+
window.sweetAlertInitialize();
|
82
|
+
}
|
83
|
+
});
|
84
|
+
|
85
|
+
})(jQuery);
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module SweetAlert2Rails
|
2
|
+
module ViewHelpers
|
3
|
+
def link_to(*args, &block)
|
4
|
+
html_options = args[block_given? ? 1 : 2] || {}
|
5
|
+
|
6
|
+
if options_has_confirm?(html_options)
|
7
|
+
html_options['data-sweet-alert-confirm'] = html_options.delete(:confirm) ||
|
8
|
+
html_options[:data].delete(:confirm)
|
9
|
+
end
|
10
|
+
|
11
|
+
super *args, &block
|
12
|
+
end
|
13
|
+
|
14
|
+
def submit_tag(value = 'Save changes', options = {})
|
15
|
+
if options_has_confirm?(options)
|
16
|
+
options['data-sweet-alert-confirm'] = options[:data].delete(:confirm)
|
17
|
+
end
|
18
|
+
|
19
|
+
super value, options
|
20
|
+
end
|
21
|
+
|
22
|
+
def button_tag(*args, &block)
|
23
|
+
html_options = args[block_given? ? 0 : 1] || {}
|
24
|
+
|
25
|
+
if options_has_confirm?(html_options)
|
26
|
+
html_options['data-sweet-alert-confirm'] = html_options.delete(:confirm) ||
|
27
|
+
html_options[:data].delete(:confirm)
|
28
|
+
end
|
29
|
+
|
30
|
+
super *args, &block
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def options_has_confirm?(options)
|
36
|
+
options[:data] && options[:data][:confirm]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/dummy/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require sweetalert2
|
16
|
+
//= require sweet-alert2-rails
|
17
|
+
// require_tree .
|