unobtrusive_flash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ # Unobtrusive flash messages for Rails
2
+
3
+ Ever got tired of pages that can't be cached because they contain flash messages?
4
+
5
+ Ever got tired of writing custom code to handle flash messages passed in AJAX responses?
6
+
7
+ Here comes the solution.
8
+
9
+ `unobtrusive_flash` takes your flash messages for the backend and automagically passes them to the frontend via
10
+ HTTP cookies. This works with both regular page loads and AJAX requests, does not tamper with the page body and requires
11
+ about 3 extra lines of code in your app - how's that for unobtrusive?
12
+
13
+ You can pass up to 4K of text into flash this way, and you don't need to worry about cookie size since they are
14
+ cleared immediately upon rendering.
15
+
16
+ Tested and fully functional in:
17
+
18
+ * Internet Explorer 8
19
+ * Firefox 4b7
20
+ * Chrome 7
21
+ * Safari 5
22
+ * Opera 10.6
23
+ * *please let me know if it works in firefox 3 and ie 6, 7*
24
+
25
+ ## Requirements
26
+
27
+ * Rails 2.3 or 3
28
+ * jQuery (maybe I'll make it agnostic in the future)
29
+
30
+ ## Usage
31
+
32
+ 1. Add the gem to your Rails app.
33
+ 2. Run `script/generate unobtrusive_flash` or `rails g unobtrusive_flash:install`
34
+ 3. Add the following to the controllers that generate flash messages (or better, to the `ApplicationController`):
35
+
36
+ after_filter :prepare_unobtrusive_flash
37
+
38
+ 4. Include `unobtrusive_flash.css` and `unobtrusive_flash.js` in your layouts. No custom markup is needed.
39
+ 5. Remove existing flash rendering code from your layouts.
40
+ 6. That's all! You can also edit the generated CSS/JS to your liking.
41
+
42
+ * * *
43
+
44
+ © 2010 Leonid Shevtsov, released under the MIT license
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ class UnobtrusiveFlashGenerator < Rails::Generator::Base
4
+
5
+ def manifest
6
+ record do |m|
7
+ m.directory File.join('public', 'stylesheets')
8
+ m.template 'unobtrusive_flash.css', File.join('public', 'stylesheets', 'unobtrusive_flash.css')
9
+
10
+ m.directory File.join('public', 'javascripts')
11
+ m.template 'unobtrusive_flash.js', File.join('public', 'stylesheets', 'unobtrusive_flash.js')
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def banner
18
+ %{Usage: #{$0} #{spec.name}\nCopies unobtrusive_flash.css and .js to their corresponding locations}
19
+ end
20
+
21
+ def source_root
22
+ File.expand_path('../../../lib/generators/templates', __FILE__)
23
+ end
24
+
25
+ end
26
+
27
+
@@ -0,0 +1,23 @@
1
+ #flash-messages {
2
+ position: fixed;
3
+ top: 0;
4
+ left: 0;
5
+ width: 100%;
6
+ z-index: 10000;
7
+ }
8
+
9
+ #flash-messages .flash-message {
10
+ padding: 10px 0;
11
+ background: #dcecaa;
12
+ opacity: 0.8;
13
+ }
14
+
15
+ #flash-messages .flash-message.flash-error {
16
+ background: #ffe2e2;
17
+ }
18
+
19
+ #flash-messages .flash-message .message {
20
+ width: 960px;
21
+ margin: 0 auto;
22
+ }
23
+
@@ -0,0 +1,67 @@
1
+ // This requires jQuery at least for now
2
+ $(function() {
3
+ $('<div id="flash-messages"></div>').prependTo('body');
4
+
5
+ function hideFlash($flash) {
6
+ $flash.slideUp(100,function(){$flash.remove()});
7
+ }
8
+
9
+ $.flash = function(message, options) {
10
+
11
+ options = $.extend({type: 'notice', timeout: 5000}, options);
12
+
13
+ var $flash = $('<div class="flash-message flash-'+options.type+' invisible"><div class="message">'+message+'</div></div>');
14
+
15
+ $('#flash-messages').prepend($flash);
16
+ $flash.slideDown(100);
17
+
18
+ $flash.click(function() {
19
+ hideFlash($flash);
20
+ });
21
+
22
+ if (options.timeout>0) {
23
+ setTimeout(function() {
24
+ hideFlash($flash);
25
+ },options.timeout);
26
+ }
27
+ }
28
+
29
+
30
+
31
+ function loadFlashFromCookies() {
32
+ if (document.cookie && document.cookie != '') {
33
+ var cookies = document.cookie.split(';');
34
+ var name = 'flash';
35
+ var cookieValue = null;
36
+ var data = null;
37
+
38
+ for (var i = 0; i < cookies.length; i++) {
39
+ var cookie = jQuery.trim(cookies[i]);
40
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
41
+ // replace fixes problems with Rails escaping. Duh.
42
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1).replace(/\+/g,'%20'));
43
+ break;
44
+ }
45
+ }
46
+
47
+ try {
48
+ data = $.parseJSON(cookieValue);
49
+ } catch(e) {
50
+ }
51
+
52
+ if (data!=null) {
53
+ $.each(data, function(i, d) {
54
+ $.flash(d[1], {type: d[0]});
55
+ });
56
+ }
57
+
58
+ document.cookie = 'flash=; path=/';
59
+ }
60
+ }
61
+
62
+ loadFlashFromCookies();
63
+
64
+ $('html').ajaxSuccess(function(event,request,options) {
65
+ loadFlashFromCookies();
66
+ });
67
+ });
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module UnobtrusiveFlash
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copies unobtrusive_flash.css and .js to their corresponding locations"
5
+
6
+ source_root File.expand_path('../../../templates', __FILE__)
7
+
8
+ def copy_files
9
+ empty_directory 'public/stylesheets'
10
+ template 'unobtrusive_flash.css', 'public/stylesheets/unobtrusive_flash.css'
11
+ empty_directory 'public/javascripts'
12
+ template 'unobtrusive_flash.js', 'public/javascripts/unobtrusive_flash.js'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), *%w[unobtrusive_flash railtie]) if defined?(::Rails::Railtie)
2
+
3
+ module UnobtrusiveFlash
4
+
5
+ protected
6
+
7
+ def prepare_unobtrusive_flash
8
+ if flash.any?
9
+ cookie_flash = []
10
+ if cookies['flash']
11
+ cookie_flash = JSON.parse(cookies['flash']) rescue nil
12
+ cookie_flash=[] unless cookie_flash.is_a? Array
13
+ end
14
+
15
+ cookie_flash += flash.to_a
16
+
17
+ cookies['flash'] = cookie_flash.to_json
18
+ flash.discard
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ require 'unobtrusive_flash'
2
+ require 'rails'
3
+
4
+ module UnobtrusiveFlash
5
+ class Railtie < Rails::Railtie
6
+ initializer 'unobtrusive_flash.initialize', :after => :after_initialize do
7
+ ActionController::Base.send :include, UnobtrusiveFlash
8
+ end
9
+ end
10
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "init"))
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unobtrusive_flash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Leonid Shevtsov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-18 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: |
23
+ # Unobtrusive flash messages for Rails
24
+
25
+ Ever got tired of pages that can't be cached because they contain flash messages?
26
+
27
+ Ever got tired of writing custom code to handle flash messages passed in AJAX responses?
28
+
29
+ Here comes the solution.
30
+
31
+ `unobtrusive_flash` takes your flash messages for the backend and automagically passes them to the frontend via
32
+ HTTP cookies. This works with both regular page loads and AJAX requests, does not tamper with the page body and requires
33
+ about 3 extra lines of code in your app - how's that for unobtrusive?
34
+
35
+ You can pass up to 4K of text into flash this way, and you don't need to worry about cookie size since they are
36
+ cleared immediately upon rendering.
37
+
38
+ Tested and fully functional in:
39
+
40
+ * Internet Explorer 8
41
+ * Firefox 4b7
42
+ * Chrome 7
43
+ * Safari 5
44
+ * Opera 10.6
45
+ * *please let me know if it works in firefox 3 and ie 6, 7*
46
+
47
+ ## Requirements
48
+
49
+ * Rails 2.3 or 3
50
+ * jQuery (maybe I'll make it agnostic in the future)
51
+
52
+ ## Usage
53
+
54
+ 1. Add the gem to your Rails app.
55
+ 2. Run `script/generate unobtrusive_flash` or `rails g unobtrusive_flash:install`
56
+ 3. Add the following to the controllers that generate flash messages (or better, to the `ApplicationController`):
57
+
58
+ after_filter :prepare_unobtrusive_flash
59
+
60
+ 4. Include `unobtrusive_flash.css` and `unobtrusive_flash.js` in your layouts. No custom markup is needed.
61
+ 5. Remove existing flash rendering code from your layouts.
62
+ 6. That's all! You can also edit the generated CSS/JS to your liking.
63
+
64
+ * * *
65
+
66
+ &copy; 2010 Leonid Shevtsov, released under the MIT license
67
+
68
+ email: leonid@shevtsov.me
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - lib/generators/templates/unobtrusive_flash.css
77
+ - lib/generators/templates/unobtrusive_flash.js
78
+ - lib/generators/unobtrusive_flash/install/install_generator.rb
79
+ - lib/unobtrusive_flash.rb
80
+ - lib/unobtrusive_flash/railtie.rb
81
+ - generators/unobtrusive_flash/unobtrusive_flash_generator.rb
82
+ - rails/init.rb
83
+ - README.markdown
84
+ has_rdoc: true
85
+ homepage: http://github.com/leonid-shevtsov/unobtrusive_flash
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Unobtrusive flash messages for rails
118
+ test_files: []
119
+