Giter Site home page Giter Site logo

atrox / sweetify Goto Github PK

View Code? Open in Web Editor NEW
20.0 6.0 7.0 9 KB

SweetAlert flash messages for Ruby on Rails

Home Page: https://rubygems.org/gems/sweetify

License: BSD 3-Clause "New" or "Revised" License

Ruby 97.89% HTML 2.11%
sweetalert rails flash-messages

sweetify's Introduction

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:

sweetify's People

Contributors

atrox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sweetify's Issues

Rails 6 - sweetify

Hi, I am trying to integrate sweetify into my rails 6 app using webpacker. Is it possible to do this? if so, I was wondering if you have a guide on how to do this. At the moment I am getting a swal is not defined error.

auto closing message

Hi Atrox! how are you?
Do you know if it's possible to insert autoclosing on the messages?
Because I'm using: "useRejections: false" but I receive this error: "Unhandled Promise Rejection: timer"

And looking in google I see that it happens because "useRejections" are used in another gem called SweetAlert2.

Do you have any other suggestion?
Thanks!

Unhandled Promise Rejection: timer - Rails 5

Hi Guys! I'm with a little problem here, everything is working fine, I just have this problem:
In my alert I have this code:
sweetalert_success('Bem vindo ' + current_user.name.to_s + "!", 'Olá!')
without timer and without button, the message appear but I have this message in my log:
Unhandled Promise Rejection: timer
And after to see this message my view stay freeze, and I can't do nothing, so when I press F5 my view refresh and back to work until to appear other alert message..
Do you know what can I do to fix it?
Thanks!

SweetAlert button options have changed

Hello! Thank you for making this gem, it works great. I noticed while using it that there were some deprecation warnings to do with button options.

screen shot 2018-03-24 at 12 44 59 pm

The new button options are here. If you're interested, I'd be happy to work on a PR to update the options hash and get rid of these warnings.

Alert not working

The result are return 200 or 400 but not working alert.
Why this code not work?

#POST
class PasswordsController < Devise::PasswordsController

    def create
        self.resource = resource_class.send_reset_password_instructions(resource_params)
        yield resource if block_given?

        if successfully_sent?(resource)
            alert_success "Enviamos o email com instruções para redefinir sua senha."
            head 200
        else
            alert_warning "Email não encontrado.", "Ops!"
            head 400
        end

    end

end
<!-- application.html.erb -->
...
<%= javascript_include_tag "application" %>
<%= render 'sweetify/alert' %>

Don't working in smartphones (environment production) - Rails 4

Hi Guys, i dont know why, but my messages not working in production when i try using smartphones, but in development everything works fine.

And when i try using browsers in whatever SO works fine in production or development.

I did everything again and nothing change, this is my code:

APPLICATION.JS
//= require jquery
//= require jquery.min
//= require jquery_ujs
//= require jquery-ui
//= require autocomplete-rails
//= require bootstrap
//= require twitter/bootstrap
//= require bootstrap-datepicker
//= require bootstrap-datepicker/core
//= require maskedinput

//= require fusioncharts/fusioncharts
//= require fusioncharts/fusioncharts.charts
//= require fusioncharts/fusioncharts.theme.fint
//= require demo
//= require dash
//= require jquery.touchSwipe.min
//= require sweetalert
//= require dropzone
//= require bootstrap/modal
//= require_tree .

APPLICATION.HTML.ERB
<%= javascript_include_tag 'demo', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'dash', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'jquery.touchSwipe.min', 'data-turbolinks-track' => true %>
<%= render 'sweetify/alert' %>

GEM FILE
source 'https://rubygems.org'

para o dropzone images

gem 'paperclip'
gem 'dropzonejs-rails'

para mensagens personalizadas em javascript

gem 'sweetalert-rails'
gem 'sweetalert2'

para as flash messages

gem 'sweetify'

para autocompletar na consulta do produto

gem 'rails4-autocomplete'
gem 'jquery-ui-rails'

usando o thin server

gem 'thin'

para gerar o grafico por mes

gem 'fusioncharts-rails'

exportar para XLS

gem 'to_xls-rails'# Last officially released gem

usando simple calendar

gem "simple_calendar", "~> 2.0"

gerar os graficos

gem "chartkick"

gems adicionadas para esse projeto manualmente

gem 'twitter-bootstrap-rails'

gem 'bootstrap-sass'

só funciona instalando pelo prompt esse versão, e especificando abaixo a versão 3.1.10

gem install bcrypt --platform=ruby

gem 'bcrypt', '3.1.11'

paginar as paginas

gem 'will_paginate'

envio de emails

gem 'actionmailer'

carrega o datetime picker nas datas

gem 'bootstrap-datepicker-rails', :require => 'bootstrap-datepicker-rails',

:git => 'git://github.com/Nerian/bootstrap-datepicker-rails.git'

gem 'bootstrap-datepicker-rails'

tive que adicionar essa gem para funcionar o jquery no heroku

gem 'rails_12factor'

pdf

gem 'wicked_pdf'
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
gem 'responders'

Bundle edge Rails instead: gem 'rails', github: 'rails/rails'

gem 'rails', '4.2.5'

Use postgresql as the database for Active Record

gem 'pg', '~> 0.15'

Use SCSS for stylesheets

gem 'sass-rails', '~> 5.0'

Use Uglifier as compressor for JavaScript assets

gem 'uglifier', '>= 1.3.0'

Use CoffeeScript for .coffee assets and views

gem 'coffee-rails', '~> 4.1.0'

See https://github.com/rails/execjs#readme for more supported runtimes

gem 'therubyracer', platforms: :ruby

Use jquery as the JavaScript library

gem 'jquery-rails'

Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks

gem 'turbolinks'

Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder

gem 'jbuilder', '~> 2.0'

bundle exec rake doc:rails generates the API under doc/api.

gem 'sdoc', '~> 0.4.0', group: :doc

Use ActiveModel has_secure_password

gem 'bcrypt', '~> 3.1.7'

Use Unicorn as the app server

gem 'unicorn'

Use Capistrano for deployment

gem 'capistrano-rails', group: :development

group :development, :test do

Call 'byebug' anywhere in the code to stop execution and get a debugger console

gem 'byebug'
end

group :development do

Access an IRB console on exception pages or by using <%= console %> in views

gem 'web-console', '~> 2.0'
end

Windows does not include zoneinfo files, so bundle the tzinfo-data gem

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Help me please! thank you.

Sweetify Alert without redirect_to

This is more of a Query than an Issue.

Using the sweetify gem extensively in my project. However, I've a requirement to show the alert message on click of form submit, if there are any errors like duplicate data and etc. With redirect_to, i can able to easily display the sweetify alert message, but redirect_to is refreshing the page and thus clearing/resetting the form. Is there any way, i can use this alert mechanism without any redirect_to? Or is there any better way to address the problem?
Any help or advice is well appreciated. Thanks in Advance.

Sample code:

rescue ActiveRecord::ActiveRecordError => ex
 sweetalert_error(ex.message, 'Error! Failed to create a record.', persistent: 'OK')
 redirect_to new_records_path

Swal is not defined

Hi, I'm trying to implement your Gem in my Rails App and i dont know why i'm receiving this error:

Uncaught ReferenceError: Swal is not defined

I did everything:
APPLICATION.JS
//= require jquery
//= require bootstrap
//= require jquery_ujs
//=require jquery.turbolinks
//= require jquery-ui
//= require twitter/bootstrap
//= require autocomplete-rails
//= require turbolinks
//= require underscore
//= require gmaps/google
//= require rails-ujs
//= require sweetalert2
//= require sweet-alert2-rails
//= require chartkick
//= require fusioncharts/fusioncharts
//= require fusioncharts/fusioncharts.charts
//= require maskedinput
//= require masks
//= require progressbar
//= require layout
//= require_tree .

APPLICATION.CSS
*= require_self
*= require sweetalert2
*= require font-awesome
*= require_tree .
*/

RAILS VERSION
Rails 5.1.7
RUBY VERSION
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

GEM FILE
gem 'sweetify'
gem 'rails-assets-sweetalert2', '~> 5.1.1', source: 'https://rails-assets.org'
gem 'sweet-alert2-rails'

Do you know what more can i do to fix it? thanks!

Uncaught ReferenceError: Swal is not defined

Hi Atrox! how are you doing?
I'm facing a strange problem after to format my Notebook..
Everything was working perfect using your gem, but now when i try to show some message, i have this in my log:

Uncaught ReferenceError: Swal is not defined
And i don't changed nothing..

I tried to run rails assets:precompile, and i checked if i have the code below in my application.html.erb, and everything is there..
Gem file:
gem 'sweetify'
gem 'rails-assets-sweetalert2', '~> 5.1.1', source: 'https://rails-assets.org'
gem 'sweet-alert2-rails'

application.js:
//= require sweetalert2
//= require sweet-alert2-rails

application.html.erb:
<%= render 'sweetify/alert' %>

Do you have some suggestion?
thanks!

Timer can´t be set

Hello, I've installed sweetify and tried to set the timer to 5 sec:
sweetalert_success('Your resource is created and available.', 'Successfully created', persistent: 'Awesome!', timer: 5000)
But instead of disappearing, the alert stays there. Also, when I tried to get back to normal (deleting the 'timer: 5000' part) the default timer didn´t seemed to work anymore. Am I doing something wrong?

Show datas (like table data) on dialogbox and count each data then save it to database

Hi Guys, I want show alert with datas (like table data) in dialogbox alert, I have try with this code

@alerts = Rails.cache.read('safety_alerts').where(profile_id: @organization.profiles.pluck(:id)).unread.order('created_at DESC').group_by(&:profile)
@alerts.each do |profile, alert|
sweetalert_error(+ profile.full_name + " - " + alert.last.alert_msg + "!", 'Need Attenion!', persistent: 'OK!')
end

but only one data show on dialogbox ?
And i wan to count each data appear and save it to database, when button click
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.