frontend_notifier 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +55 -0
- data/Rakefile +53 -0
- data/Screenshot.png +0 -0
- data/VERSION +1 -0
- data/app/assets/images/frontend_notifier_close_notification_ico.png +0 -0
- data/app/assets/javascripts/lib/_frontend_notifier.js.coffee +45 -0
- data/app/assets/stylesheets/_frontend_notifier.css.scss +24 -0
- data/app/config/locales/en.yml +4 -0
- data/app/views/shared/_frontend_notifier.html.haml +17 -0
- data/lib/frontend_notifier/application_controller.rb +35 -0
- data/lib/frontend_notifier/frontend_notifier_engine.rb +10 -0
- data/lib/frontend_notifier.rb +2 -0
- metadata +111 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.6.4"
|
11
|
+
gem "rcov", ">= 0"
|
12
|
+
gem "rails", ">= 3.1.0"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Roman Snitko
|
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.markdown
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
FrontendNotifier
|
2
|
+
=================
|
3
|
+
|
4
|
+
![Screenshot](https://github.com/snitko/frontend_notifier/raw/master/Screenshot.png)
|
5
|
+
|
6
|
+
Shows cute notifications in frontend. No need to manually create your own html/css/js to show Rails's flash[:notice] or other flashes. This gem handles it nicely + you can customize it.
|
7
|
+
|
8
|
+
INSTALLATION
|
9
|
+
------------
|
10
|
+
|
11
|
+
1. `gem install frontend_notifier`
|
12
|
+
2. put the following in your .scss file: `@import "frontend_notifier";`
|
13
|
+
3. put the following in your .js or .coffee file: `#= require lib/_frontend_notifier`
|
14
|
+
|
15
|
+
USAGE
|
16
|
+
-----
|
17
|
+
|
18
|
+
**Regular reuqests**
|
19
|
+
FrontendNotifier will pick up any flash with the names `:error`, `:alert`, `:success` or `:notice`. You can customize this by creating your own `views/shared/frontend_notifier` template.
|
20
|
+
|
21
|
+
**Ajax requests**
|
22
|
+
Sometimes you may need to display the notification after an ajax request. For that, you may want to use `#notify_frontend` method now available in your controller and pass it either a model with (or without) errors:
|
23
|
+
|
24
|
+
def create
|
25
|
+
...
|
26
|
+
notify_frontend(@user)
|
27
|
+
end
|
28
|
+
|
29
|
+
or a message and an error type:
|
30
|
+
|
31
|
+
def create
|
32
|
+
...
|
33
|
+
notify_frontend("Email can't be blank", :error)
|
34
|
+
end
|
35
|
+
|
36
|
+
Both will result in rendering the following json:
|
37
|
+
|
38
|
+
{ type: "error", message: "Email can't be blank" }
|
39
|
+
|
40
|
+
Now you can deal with this in your ajax request callback. Assuming this is a jQuery $.ajax function:
|
41
|
+
|
42
|
+
success: function(response) { FrontendNotifier.show(response) }
|
43
|
+
|
44
|
+
In this case, `FrontendNotifier.show` will recognize the type of the notification (either "success" or "error") and display appropriately styled notification (by default, green background for success and red for error).
|
45
|
+
|
46
|
+
You can also specify the type of error to the FrontendNotifier class manually and call the .show() function anywhere in your js:
|
47
|
+
FrontendNotifier.show("Testing notifications", "success")
|
48
|
+
|
49
|
+
There's also a handy #join_model_errors method added to the ApplicationController, which you might want to look at. It basically takes errors from the model and compiles them into a one line message which may then be handed to the FrontendNotifier. So, you may want to use it like this:
|
50
|
+
|
51
|
+
flash[:error] = join_model_errors(@user, include_field_names: true) # :include_field_names is true by default
|
52
|
+
|
53
|
+
USAGE
|
54
|
+
-----
|
55
|
+
For now, the gem lacks generators, so if you want to customize scss styles or haml templates, just copy .scss/.haml files from their gem dirs into your app dirs and change them.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "frontend_notifier"
|
18
|
+
gem.homepage = "http://github.com/snitko/frontend_notifier"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Rails extension, shows cute notifications in frontend.}
|
21
|
+
gem.description = %Q{Shows cute notifications in frontend. No need to manually create your own html/css/js to show Rails's flash[:notice] or other flashes. This handles it nicely + you can customize it.}
|
22
|
+
gem.email = "subscribe@snitko.ru"
|
23
|
+
gem.authors = ["Roman Snitko"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
#require 'rake/testtask'
|
29
|
+
#Rake::TestTask.new(:test) do |test|
|
30
|
+
# test.libs << 'lib' << 'test'
|
31
|
+
# test.pattern = 'test/**/test_*.rb'
|
32
|
+
# test.verbose = true
|
33
|
+
#end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rdoc/task'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "frontend_notifier #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/Screenshot.png
ADDED
Binary file
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
Binary file
|
@@ -0,0 +1,45 @@
|
|
1
|
+
jQuery ($) ->
|
2
|
+
|
3
|
+
class _FrontendNotifier
|
4
|
+
|
5
|
+
constructor: () ->
|
6
|
+
@block = $("#frontend_notification")
|
7
|
+
@message = $(".message", @block)
|
8
|
+
@timer = $(".closingCounter .number", @block)
|
9
|
+
@update_timer = () =>
|
10
|
+
unless @stop_timer
|
11
|
+
if @timer.text() != "1"
|
12
|
+
@timer.text (@timer.text() - 1)
|
13
|
+
setTimeout @update_timer, 1000
|
14
|
+
else
|
15
|
+
this.close()
|
16
|
+
$("#close_notification_ico").click () =>
|
17
|
+
@stop_timer = true
|
18
|
+
this.close()
|
19
|
+
|
20
|
+
show: (message, type) ->
|
21
|
+
unless typeof(message) == "string"
|
22
|
+
type = message.type
|
23
|
+
message = message.message
|
24
|
+
@block.removeClass("error").removeClass("success").addClass(type)
|
25
|
+
@timer.text 5
|
26
|
+
@message.html(message)
|
27
|
+
@block.show()
|
28
|
+
@current_timestamp = new Date().getTime()
|
29
|
+
this.start_timer(@current_timestamp)
|
30
|
+
|
31
|
+
show_error: () ->
|
32
|
+
this.show $("#frontend_notification_error500").text(), "error"
|
33
|
+
|
34
|
+
close: () ->
|
35
|
+
@block.slideUp(300)
|
36
|
+
|
37
|
+
start_timer: (timestamp) ->
|
38
|
+
@stop_timer = false
|
39
|
+
setTimeout () =>
|
40
|
+
@update_timer() if timestamp == @current_timestamp
|
41
|
+
,1000
|
42
|
+
|
43
|
+
window.FrontendNotifier = new _FrontendNotifier
|
44
|
+
if $("#frontend_notification_content span").size() > 0
|
45
|
+
FrontendNotifier.show $("#frontend_notification_content span").text(), $("#frontend_notification_content span").attr("class")
|
@@ -0,0 +1,24 @@
|
|
1
|
+
@mixin clearfix {
|
2
|
+
display: block;
|
3
|
+
&:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
|
4
|
+
}
|
5
|
+
|
6
|
+
#frontend_notification {
|
7
|
+
height: 77px; position: fixed; color: #fff; z-index: 1999; width: 100%; display: none;
|
8
|
+
.contentWrapper { float: left; width: 100%; @include clearfix; }
|
9
|
+
.message { text-align: center; margin-right: 150px; padding-top: 28px; padding-left: 150px; padding-right: 20px; }
|
10
|
+
.info {
|
11
|
+
float: left; width: 132px; margin-left: -150px; padding: 10px 8px 10px 10px; font-size: 11px; text-align: right;
|
12
|
+
.closingCounter { opacity: 0.5; }
|
13
|
+
img#close_notification_ico { opacity: 0.5; float: right; margin-left: 5px; cursor: pointer; }
|
14
|
+
img#close_notification_ico:hover { opacity: 1; }
|
15
|
+
.reportBug {
|
16
|
+
background-color: #670000; padding: 5px; font-size: 11px; margin: 0 auto; margin: 10px 10px 0 0; display: inline-block;
|
17
|
+
a, a:visited { opacity: 0.5; color: #fff; text-decoration: none; }
|
18
|
+
a:hover { opacity: 1; }
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
#frontend_notification.error { background-color: #a90000; }
|
23
|
+
#frontend_notification.success { background-color: #57a900; .reportBug { display: none; } }
|
24
|
+
#frontend_notification_content, #frontend_notification_error500 { display: none; }
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#frontend_notification
|
2
|
+
.contentWrapper
|
3
|
+
.message
|
4
|
+
.info
|
5
|
+
%span.closingCounter
|
6
|
+
= t("frontend_notifier.closes_in")
|
7
|
+
%span.number 10
|
8
|
+
= t("frontend_notifier.sec")
|
9
|
+
= image_tag "frontend_notifier_close_notification_ico.png", :id => "close_notification_ico"
|
10
|
+
|
11
|
+
#frontend_notification_content
|
12
|
+
- if error = flash[:error] || flash[:alert]
|
13
|
+
%span.error= error
|
14
|
+
- elsif success = flash[:success] || flash[:notice]
|
15
|
+
%span.success= success
|
16
|
+
|
17
|
+
#frontend_notification_error500= t("frontend_notifier.error500_text")
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module FrontendNotifierControllerExtension
|
2
|
+
|
3
|
+
def notify_frontend(message, error_type="error")
|
4
|
+
unless message.kind_of? String || message.nil?
|
5
|
+
if message.errors.empty?
|
6
|
+
message = "Success"
|
7
|
+
error_type = "success"
|
8
|
+
else
|
9
|
+
message = join_model_errors(message)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
render :json => { :type => error_type, :message => message }
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def join_model_errors(model, options = { include_field_names: true })
|
18
|
+
result = []
|
19
|
+
model.errors.each do |k,v|
|
20
|
+
if k == :base
|
21
|
+
result << v
|
22
|
+
else
|
23
|
+
if options[:include_field_names]
|
24
|
+
result << "#{k} #{v}"
|
25
|
+
else
|
26
|
+
result << v
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
result = result.join("; ")
|
31
|
+
dot_at_the_end = (result =~ /\.\Z/) ? "" : "."
|
32
|
+
result + dot_at_the_end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: frontend_notifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roman Snitko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &20884280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20884280
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
requirement: &20883400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.6.4
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20883400
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rcov
|
38
|
+
requirement: &20881600 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20881600
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rails
|
49
|
+
requirement: &20903460 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *20903460
|
58
|
+
description: Shows cute notifications in frontend. No need to manually create your
|
59
|
+
own html/css/js to show Rails's flash[:notice] or other flashes. This handles it
|
60
|
+
nicely + you can customize it.
|
61
|
+
email: subscribe@snitko.ru
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files:
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.markdown
|
67
|
+
files:
|
68
|
+
- .document
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.markdown
|
72
|
+
- Rakefile
|
73
|
+
- Screenshot.png
|
74
|
+
- VERSION
|
75
|
+
- app/assets/images/frontend_notifier_close_notification_ico.png
|
76
|
+
- app/assets/javascripts/lib/_frontend_notifier.js.coffee
|
77
|
+
- app/assets/stylesheets/_frontend_notifier.css.scss
|
78
|
+
- app/config/locales/en.yml
|
79
|
+
- app/views/shared/_frontend_notifier.html.haml
|
80
|
+
- lib/frontend_notifier.rb
|
81
|
+
- lib/frontend_notifier/application_controller.rb
|
82
|
+
- lib/frontend_notifier/frontend_notifier_engine.rb
|
83
|
+
homepage: http://github.com/snitko/frontend_notifier
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: -1967541642647775528
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.10
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Rails extension, shows cute notifications in frontend.
|
111
|
+
test_files: []
|