growlyflash 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e58668dcd8da28c9ef6a2825d642358ca399b285
4
- data.tar.gz: 61cb763f5328fce65817562112b4858d1394319d
3
+ metadata.gz: ec72d3523240f5489bb3c5154b946f92aa3fb733
4
+ data.tar.gz: c3caedfa03cce596612d5cf032dfd814dd5c5bfe
5
5
  SHA512:
6
- metadata.gz: 62c3b1fbc6998b6f0d971f8b748620a4203ffc2f0978192a07cea3a97253f83ae536dedd5e6190aa0a85686950e1d10e3428876ad420e117504837f5179283fc
7
- data.tar.gz: 73b325ace30ee62de527c7e5dd57a218f5c829784a9f66ed6254dc8c07b9066594bdd97764836896dfe63c96ba41cb468a9b192b259e9e605bbccf8d1f0b7727
6
+ metadata.gz: b5f5d3cc3783a4da9eecdd93c38a4fcc43a5ed3eaef1e01c4e12c49f2f737e7b53a19dabbc808dfb5f259ef60f47f6dc0553cd083796bd8b39c63c191c3f4a1d
7
+ data.tar.gz: 49b33df20b718fcc86533d117cd3e51e81b80f774d0eec85d68e1c0b4daa3280427b799a16458eae5d7eb10e67c85f4c5499b1eb6219108737ebd2654a5145e2
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
19
+ .tm_properties
data/Gemfile CHANGED
@@ -2,3 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in growlyflash.gemspec
4
4
  gemspec
5
+
6
+ gem 'rails', '>= 3.2.0', '<= 5.0.0'
7
+ gem 'rake', require: false
8
+
9
+ group :test do
10
+ gem 'minitest'
11
+ gem 'minitest-reporters'
12
+ gem 'minitest-spec-rails'
13
+ end
data/README.md CHANGED
@@ -18,18 +18,6 @@ Add this line to your application's Gemfile:
18
18
  gem 'growlyflash', '~> 0.5.0'
19
19
  ```
20
20
 
21
- To use text flash messages as growl notifications with XHR request, just add this `after_filter` to your controllers (usually `application_controller.rb`):
22
-
23
- ```ruby
24
- after_filter :flash_to_headers, if: :is_xhr_request?
25
- ```
26
-
27
- To make notifications also available with non-XHR requests, insert the following line into your layout template inside `<head>` tag before any other javascript:
28
-
29
- ```erb
30
- <%= growlyflash_static_notices %>
31
- ```
32
-
33
21
  Require one of the following Growlyflash javascripts depending on your Bootstrap version in `app/assets/javascripts/application.js`:
34
22
 
35
23
  ```js
@@ -40,12 +28,27 @@ Require one of the following Growlyflash javascripts depending on your Bootstrap
40
28
  //= require growlyflash.bs2
41
29
  ```
42
30
 
43
- Finally, import Growlyflash style in `app/assets/stylesheets/application.css.scss` after importing Bootstrap styles:
31
+ Import Growlyflash style in `app/assets/stylesheets/application.css.scss` after importing Bootstrap styles:
44
32
 
45
33
  ```scss
46
34
  @import "growlyflash";
47
35
  ```
48
36
 
37
+ To use text flash messages as growl notifications with XHR request, add `use_growlyflash` to your controllers (usually `application_controller.rb`). This is a shorthand for `append_after_filter :flash_to_header, if: :is_xhr_request?` and takes callback parameters like `only`, `except`, `if` or `unless`:
38
+
39
+ ```ruby
40
+ use_growlyflash # except: %i[actions without growlyflash]
41
+
42
+ # Also there is another shorthand, to skip callbacks:
43
+ # skip_growlyflash only: %i[actions without growlyflash]
44
+ ```
45
+
46
+ To make notifications also available with non-XHR requests, insert the following line into your layout template inside `<head>` tag before any other javascript:
47
+
48
+ ```erb
49
+ <%= growlyflash_static_notices %>
50
+ ```
51
+
49
52
  ## Customize
50
53
 
51
54
  If you want to change default options, you can override them somewhere in your coffee/js:
@@ -57,6 +60,7 @@ Growlyflash.defaults = $.extend on, Growlyflash.defaults,
57
60
  dismiss: yes # allow to show close button
58
61
  spacing: 10 # spacing between alerts
59
62
  target: 'body' # selector to target element where to place alerts
63
+ title: no # switch for adding a title
60
64
  type: null # bootstrap alert class by default
61
65
  class: ['alert', 'growlyflash', 'fade']
62
66
  ```
data/Rakefile CHANGED
@@ -1 +1,16 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ require 'rake'
3
+
4
+ Bundler.setup
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require "rake/testtask"
8
+
9
+ desc 'Run tests'
10
+ test_task = Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ task default: :test
@@ -3,12 +3,17 @@ window.Growlyflash ?= Growlyflash
3
3
 
4
4
  Growlyflash.defaults =
5
5
  align: 'right' # horizontal aligning (left, right or center)
6
- delay: 4000 # auto-dismiss timeout (0 to disable auto-dismiss)
6
+ delay: 4000 # auto-dismiss timeout (false to disable auto-dismiss)
7
7
  dismiss: yes # allow to show close button
8
8
  spacing: 10 # spacing between alerts
9
9
  target: 'body' # selector to target element where to place alerts
10
+ title: no # switch for adding a title
10
11
  type: null # bootstrap alert class by default
11
12
  class: ['alert', 'growlyflash', 'fade']
13
+
14
+ # customizable callback to set notification position before it shows
15
+ before_show: (options) ->
16
+ @el.css @calc_css_position()
12
17
 
13
18
  Growlyflash.KEY_MAPPING =
14
19
  alert: 'warning'
@@ -25,11 +30,23 @@ class Growlyflash.FlashStruct
25
30
 
26
31
  class Growlyflash.Alert
27
32
  constructor: (@flash, options) ->
28
- {@align, @delay, @dismiss, @msg, @spacing, @target, @type, @class} = options
33
+ {@title, @align, @dismiss, @msg, @spacing, @type, @class} = options
34
+
35
+ @el = ($ '<div>',
36
+ class: @_classes().join(' ')
37
+ html: "#{@_dismiss()}#{@_title()}#{@msg}"
38
+ ).appendTo(options.target)
39
+
40
+ options.before_show.call(this, options)
41
+ @show()
29
42
 
30
- @el = ($ '<div>', class: @_classes().join(' '), html: "#{@_dismiss()}#{@msg}").appendTo(@target)
31
- @el.css(@_calc_position()).toggleClass('in')
32
- @el.delay(@delay).fadeOut(-> ($ @).remove()) if @delay > 0
43
+ return unless options.delay
44
+ setTimeout =>
45
+ @hide -> ($ @).remove()
46
+ , options.delay
47
+
48
+ show: -> @el.toggleClass 'in'
49
+ hide: (fn) -> @el.fadeOut(fn)
33
50
 
34
51
  _classes: ->
35
52
  @class.concat ("alert-#{type}" for type in [@type] when type?), ["growlyflash-#{@align}"]
@@ -38,19 +55,23 @@ class Growlyflash.Alert
38
55
  return "" unless @dismiss?
39
56
  """<a class="close" data-dismiss="alert" href="#">&times;</a>"""
40
57
 
41
- _calc_offset: ->
58
+ _title: ->
59
+ return "" unless @title?
60
+ """<strong>#{@type.charAt(0).toUpperCase()}#{@type.substring(1)}!</strong>"""
61
+
62
+ calc_top_offset: ->
42
63
  amount = parseInt(@el.css 'top')
43
64
  (@el.siblings '.growlyflash').each (_, el) =>
44
65
  amount = Math.max(amount, parseInt(($ el).css 'top') + ($ el).outerHeight() + @spacing)
45
66
  amount
46
67
 
47
- _calc_position: ->
48
- styles = {}
49
- styles.top = "#{@_calc_offset()}px"
50
- styles.marginLeft = "-#{@el.outerWidth() / 2}px" if @align is 'center'
51
- styles
68
+ calc_css_position: ->
69
+ css = {}
70
+ css.top = "#{@calc_top_offset()}px"
71
+ css.marginLeft = "-#{@el.width() / 2}px" if @align is 'center'
72
+ css
52
73
 
53
74
  $.growlyflash = (flash, options = {}) ->
54
- settings = $.extend(on, {}, Growlyflash.defaults, msg: flash.msg, type: flash.type, options)
75
+ settings = $.extend on, {}, Growlyflash.defaults, msg: flash.msg, type: flash.type, options
55
76
  alert = new Growlyflash.Alert(flash, settings)
56
- if flash instanceof Growlyflash.FlashStruct then flash else alert
77
+ if flash instanceof Growlyflash.FlashStruct then flash else alert
@@ -1,20 +1,24 @@
1
1
  # encoding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
 
5
6
  Gem::Specification.new do |spec|
6
- spec.name = "growlyflash"
7
- spec.version = "0.5.0"
8
- spec.authors = ["Tõnis Simo"]
9
- spec.email = ["anton.estum@gmail.com"]
10
- spec.homepage = "https://github.com/estum/growlyflash"
7
+ spec.name = 'growlyflash'
8
+ spec.version = '0.6.1'
9
+ spec.authors = ['Tõnis Simo']
10
+ spec.email = ['anton.estum@gmail.com']
11
+ spec.homepage = 'https://github.com/estum/growlyflash'
11
12
  spec.summary = %q{Tiny gem which provides growl-styled flash messages for Ruby on Rails with Bootstrap.}
12
13
  spec.description = %q{Tiny gem which provides growl-styled flash messages for Ruby on Rails with Bootstrap. For XHR requests flash messages are transfering in 'X-Messages' headers, otherwise they are storing in js variables.}
13
- spec.license = "MIT"
14
+ spec.license = 'MIT'
14
15
 
15
16
  spec.files = `git ls-files`.split($/)
16
17
  spec.require_paths = %w[app lib]
18
+ spec.test_files = Dir['test/**/*']
19
+
20
+ spec.required_ruby_version = ">= 1.9.3"
17
21
 
18
- spec.add_dependency "railties", ">= 3.2", "< 5.0"
19
- spec.add_dependency 'coffee-rails', ">= 3.2.1"
22
+ spec.add_dependency 'railties', '>= 3.2', '< 5.0'
23
+ spec.add_dependency 'coffee-rails', '>= 3.2.1'
20
24
  end
@@ -1,4 +1,2 @@
1
- # encoding: utf-8
2
-
3
1
  require 'growlyflash/controller_additions'
4
2
  require 'growlyflash/engine' if defined?(Rails)
@@ -5,29 +5,40 @@ module Growlyflash
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- helper_method :growlyflash_static_notices
8
+ helper_method :growlyflash_static_notices, :growlyhash
9
9
  end
10
10
 
11
- private
11
+ module ClassMethods
12
+ private
13
+ def use_growlyflash(options = {})
14
+ append_after_filter :flash_to_headers, options.reverse_merge(if: :is_xhr_request?)
15
+ end
16
+
17
+ def skip_growlyflash(options = {})
18
+ skip_after_filter :flash_to_headers, options
19
+ end
20
+ end
21
+
22
+ private
12
23
  def is_xhr_request?
13
24
  request.xhr?
14
25
  end
15
26
 
16
27
  def flash_to_headers
17
- _text_flashes = text_flashes
18
- response.headers['X-Message'] = URI.escape(_text_flashes.to_json)
28
+ response.headers['X-Message'] = URI.escape(growlyhash(true).to_json)
19
29
 
20
30
  # discard flash to prevent it appear again after refreshing page
21
- _text_flashes.each_key {|k| flash.discard(k) }
31
+ growlyhash.each_key {|k| flash.discard(k) }
22
32
  end
23
33
 
24
- def growlyflash_static_notices
34
+ def growlyflash_static_notices(js_var = 'window.flashes')
25
35
  return nil unless flash.any?
26
- view_context.javascript_tag "window.flashes = #{text_flashes.except!(:timedout, 'timedout').to_json.html_safe};", defer: 'defer'
36
+ view_context.javascript_tag "#{js_var} = #{growlyhash.to_json.html_safe};", defer: 'defer'
27
37
  end
28
38
 
29
- def text_flashes
30
- flash.to_hash.select {|k, v| v.is_a? String }
39
+ def growlyhash(force = false)
40
+ @growlyhash = nil if force
41
+ @growlyhash ||= flash.to_hash.select {|k, v| v.is_a? String }
31
42
  end
32
43
  end
33
44
  end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Growlyflash
5
+ class IntegrationTest < ActionController::TestCase
6
+ class MyController < ActionController::Base
7
+ use_growlyflash
8
+ skip_growlyflash only: [:xhr_skip_growlyflash]
9
+
10
+ def xhr_use_growlyflash
11
+ flash.notice = "Growlyflash!"
12
+ flash[:timedout] = true
13
+ render json: '{}'
14
+ end
15
+
16
+ def xhr_skip_growlyflash
17
+ flash.notice = "Growlyflash!"
18
+ render json: '{}'
19
+ end
20
+ end
21
+
22
+ tests MyController
23
+
24
+ def test_xhr_use_growlyflash
25
+ xhr :get, :xhr_use_growlyflash
26
+
27
+ assert_response 200
28
+ refute_nil @response.headers['X-Message']
29
+ assert_equal '{"notice":"Growlyflash!"}', URI.decode_www_form_component(@response.headers['X-Message'])
30
+ end
31
+
32
+ def test_xhr_skip_growlyflash
33
+ xhr :get, :xhr_skip_growlyflash
34
+
35
+ assert_response 200
36
+ assert_nil @response.headers['X-Message']
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'growlyflash'
4
+ require 'action_controller'
5
+
6
+ # Ensure backward compatibility with Minitest 4
7
+ Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
8
+
9
+ require "minitest/reporters"
10
+ Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new(:color => true)
11
+
12
+ module TestHelper
13
+ Routes = ActionDispatch::Routing::RouteSet.new
14
+ Routes.draw do
15
+ get ':controller(/:action(/:id))'
16
+ get ':controller(/:action)'
17
+ end
18
+
19
+ ActionController::Base.send :include, Routes.url_helpers
20
+ end
21
+
22
+ ActionController::TestCase.class_eval do
23
+ def setup
24
+ @routes = TestHelper::Routes
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growlyflash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tõnis Simo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-23 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -66,6 +66,8 @@ files:
66
66
  - lib/growlyflash.rb
67
67
  - lib/growlyflash/controller_additions.rb
68
68
  - lib/growlyflash/engine.rb
69
+ - test/integration/action_controller/growlyflash_test.rb
70
+ - test/test_helper.rb
69
71
  homepage: https://github.com/estum/growlyflash
70
72
  licenses:
71
73
  - MIT
@@ -79,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
81
  requirements:
80
82
  - - ">="
81
83
  - !ruby/object:Gem::Version
82
- version: '0'
84
+ version: 1.9.3
83
85
  required_rubygems_version: !ruby/object:Gem::Requirement
84
86
  requirements:
85
87
  - - ">="
@@ -87,9 +89,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
89
  version: '0'
88
90
  requirements: []
89
91
  rubyforge_project:
90
- rubygems_version: 2.2.2
92
+ rubygems_version: 2.4.3
91
93
  signing_key:
92
94
  specification_version: 4
93
95
  summary: Tiny gem which provides growl-styled flash messages for Ruby on Rails with
94
96
  Bootstrap.
95
- test_files: []
97
+ test_files:
98
+ - test/integration/action_controller/growlyflash_test.rb
99
+ - test/test_helper.rb