show_message 1.1.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e5862ff3c181739c267887d1bab6d5b52a07a877b48c29184f2b9e6a0d6a635
4
- data.tar.gz: 9670ee92fcaf45089d57a694b704e6992ae3eab0d4b8184310d0e5d88ca1375e
3
+ metadata.gz: 3bde680317f76e6b572431af8bc42180fb248c330c3201d48863373d9ef7f8a4
4
+ data.tar.gz: 5d530062072d13f3715b48fe087703068528c20ac8b0b639fd2a1490b03b9e9a
5
5
  SHA512:
6
- metadata.gz: b9433e9e330847597eed9680a4df047ca6374b88ecd84956ce9dd5d13372e338f5eca531fda897b059e7ec7cb462414c592158e45dc28f69846b39b59bbdb2fc
7
- data.tar.gz: 347b76452c70a412da24a08f69f92418fc158401ebc33643676ff368748f4163eef36011b6f1b0496bcd14018611b6fd9438d78288963bc91cb686d982d4bd88
6
+ metadata.gz: b67ece0552015d2f38d9620650fe9e8def7d4b1c4914961e7970a2a204ea87f92ece5bca21924d951693c317296315bdaa8a6b1c7b43e805b24d8b775bad8204
7
+ data.tar.gz: 210c6ffe6828604f4fc51c5ea4a8e53f5cc920ea83f613760b63c65ab5040c3b36f51f7d2b66af45cec8cfbd3733315dae5832cc634286988adbeec86e5a4eb0
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [Unreleased]
6
+
7
+ ## [2.0.0] - 2020-03-05
8
+
9
+ This is major rewrite of the way the gem works. This version introduces many
10
+ breaking changes. Please review the README.
data/README.md CHANGED
@@ -26,16 +26,20 @@ See further down for the HTML this outputs so you know how to style it.
26
26
  <%= show_message %>
27
27
  ```
28
28
 
29
- Now, set a message in your controller. You can use :error, :success, :myCoolMessage or :whatever. You get the point.
30
- The only thing you should not use is and "_". Read on to see why.
29
+ Now, set a message in your controller. You can use :success, :error, :warning,
30
+ :info, :notice or :alert.
31
31
 
32
- Also, it you can set a single message as a String or multiple as an Array
32
+ Also, you can set a single message as a String or multiple as an Array
33
33
 
34
34
  ```ruby
35
35
  class MyController
36
36
 
37
37
  def test_function
38
- flash[:success] = "Some Success Message"
38
+ # short versions
39
+ show_success('Some Success Message')
40
+ show_error('Some Error Message')
41
+
42
+ show_message :success, 'Some Success Message'
39
43
  end
40
44
 
41
45
  end
@@ -45,7 +49,7 @@ Now, when using remote forms I came across the issue where I would have multiple
45
49
  This led to some issues so I added the ability to set an id on show_message
46
50
 
47
51
  ```erb
48
- <%= show_message(id: :create_object) %>
52
+ <%= show_message(:create) %>
49
53
  ```
50
54
 
51
55
  There is a slight twist to setting the flash now. I use an "_" to find what I call "scoped" messages.
@@ -54,26 +58,34 @@ There is a slight twist to setting the flash now. I use an "_" to find what I ca
54
58
  class MyController
55
59
 
56
60
  def test_function
57
- flash[:error_create_object] = "Some Error Message"
61
+ # short version
62
+ show_success_for(:create, 'Some Success Message')
63
+ show_error_for(:create, 'Some Error Message')
64
+
65
+ show_message :success, 'Some Success Message', id: :create
58
66
  end
59
67
 
60
68
  end
61
69
  ```
62
70
 
63
- Lastly, I found the need to insert a margin below the message sometimes.
71
+ ## Additional Options
72
+
73
+ Currently supported options only include `class` which allows you to specify
74
+ additional classes to be appended to the outermost div's classlist.
64
75
 
65
76
  ```erb
66
- <%= show_message(space: 10) %>
67
- <!-- will insert a 10px margin below -->
77
+ <%= show_message(class: 'my-special-class my-second-special-class') %>
78
+ <%= show_message(:create, class: 'my-special-class my-second-special-class') %>
68
79
  ```
69
80
 
70
81
  ## show_message - HTML Output
71
82
 
72
- Below is HTML that show_message will output. You will need to create a few styles to make yours look nice.
73
- I left it free of styles since I was using Zurb Foundation. I also figured most people would want to style their own anyway.
83
+ Below is HTML that show_message will output. You will need to create a few
84
+ styles to make yours look nice. The type of message (success, error, etc) will
85
+ be appended to the outermost div's classlist.
74
86
 
75
87
  ```html
76
- <div class="alert-box [the flash symbol will be here to]">
88
+ <div class="alert-box [success, info, error...]">
77
89
  <div class="message">
78
90
  Message gets outputed here.
79
91
  </div>
@@ -1,10 +1,7 @@
1
1
  <% data.each do |m| %>
2
- <div class="alert-box <%= m[:class] %> <%= options[:classes] %>">
2
+ <div class="alert-box <%= m[:class] %> <%= options[:class] %>">
3
3
  <div class="message">
4
4
  <%= raw m[:message] %>
5
5
  </div>
6
6
  </div>
7
7
  <% end %>
8
- <% if options[:space].present? %>
9
- <div style="margin-top:<%= options[:space] %>px;"></div>
10
- <% end %>
@@ -1,4 +1,5 @@
1
1
  require 'show_message/view_helpers'
2
+
2
3
  if defined? Rails
3
4
  require 'show_message/railtie'
4
5
  require 'show_message/engine'
@@ -6,14 +7,26 @@ end
6
7
 
7
8
  module ShowMessage
8
9
 
9
- def show_message(type, message, id: true)
10
- flash_sym =
11
- if type.to_s.include?('_')
12
- type
13
- else
14
- id ? "#{type}_#{controller_name}" : type.to_s
15
- end
16
- flash[flash_sym] = message
10
+ def show_message(type, message, id: nil)
11
+ id ||= 'all'
12
+ flash[type] ||= {}
13
+ flash[type][id.to_s] = message
14
+ end
15
+
16
+ def show_success(message)
17
+ show_message(:success, message)
18
+ end
19
+
20
+ def show_success_for(id, message)
21
+ show_message(:success, message, id: id)
22
+ end
23
+
24
+ def show_error(message)
25
+ show_message(:error, message)
26
+ end
27
+
28
+ def show_error_for(id, message)
29
+ show_message(:error, message, id: id)
17
30
  end
18
31
 
19
32
  end
@@ -2,8 +2,18 @@ require 'show_message/view_helpers'
2
2
 
3
3
  module ShowMessage
4
4
  class Railtie < Rails::Railtie
5
- ActiveSupport.on_load(:action_view) do
6
- ::ActionView::Base.send :include, ShowMessage::ViewHelpers
5
+
6
+ initializer 'show_message.setup_action_view' do
7
+ ActiveSupport.on_load :action_view do
8
+ include ShowMessage::ViewHelpers
9
+ end
10
+ end
11
+
12
+ initializer 'show_message.setup_action_controller' do
13
+ ActiveSupport.on_load :action_controller do
14
+ include ShowMessage
15
+ end
7
16
  end
8
- end
9
- end
17
+
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShowMessage
4
- VERSION = '1.1.6'.freeze
4
+ VERSION = '2.0.0'.freeze
5
5
  end
@@ -1,35 +1,42 @@
1
1
  module ShowMessage
2
2
  module ViewHelpers
3
3
 
4
- def show_message(options = {})
5
- scope =
6
- if options[:id].nil?
7
- controller_name
8
- elsif options[:id] == false
9
- false
10
- else
11
- options[:id].to_s
12
- end
13
- if scope.present? && flash.keys.none? { |k| k.to_s.include?(scope) }
14
- return
4
+ # render the applicable messages
5
+ #
6
+ # @param options_or_ids options or the ids to target
7
+ # @param options options for the view
8
+ def show_message(options_or_ids = nil, options = nil)
9
+ if options_or_ids.is_a?(Hash)
10
+ display_ids = []
11
+ options = options_or_ids
12
+ else
13
+ display_ids = options_or_ids
14
+ options ||= {}
15
15
  end
16
16
 
17
- data = []
17
+ display_ids = display_ids.is_a?(Array) ? display_ids : [display_ids]
18
+ display_ids = display_ids.compact.map(&:to_s)
19
+ # target all messages if no target is specified
20
+ display_ids.push('all') if display_ids.empty?
18
21
 
19
- flash.each do |key, value|
20
- next if scope.present? && !key.to_s.include?(scope)
21
- next unless value
22
+ data = []
23
+ message_keys = %w(success error warning info notice alert)
22
24
 
23
- flash_type = key.to_s.split('_').first
24
- next unless flash_type.in? %w(success error warning info notice)
25
+ flash.to_h.slice(*message_keys).each do |key, message_hash|
26
+ # support basic usage of flash[:success] or flash[:error] which
27
+ # would normally just return a string
28
+ if message_hash.is_a?(Hash)
29
+ message_hash.each do |id, messages|
30
+ next unless display_ids.include?('all') || display_ids.include?(id)
25
31
 
26
- if key.is_a?(Array)
27
- value.each do |message|
28
- data.push(message: message, class: flash_type)
32
+ build_data(data, messages, key)
29
33
  end
30
34
  else
31
- data.push(message: value, class: flash_type)
35
+ next unless display_ids.include?('all')
36
+
37
+ build_data(data, message_hash, key)
32
38
  end
39
+
33
40
  flash.discard(key)
34
41
  end
35
42
 
@@ -38,5 +45,15 @@ module ShowMessage
38
45
  }
39
46
  end
40
47
 
48
+ private
49
+
50
+ # support for messages to be an array. this will add multiple items to the
51
+ # data array to be rendered.
52
+ def build_data(data, messages, key)
53
+ messages = messages.is_a?(Array) ? messages : [messages]
54
+ messages.each do |message|
55
+ data.push(message: message, class: key)
56
+ end
57
+ end
41
58
  end
42
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_message
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarrett Lusso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-28 00:00:00.000000000 Z
11
+ date: 2020-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,6 +48,7 @@ extra_rdoc_files: []
48
48
  files:
49
49
  - ".gitignore"
50
50
  - ".travis.yml"
51
+ - CHANGELOG.md
51
52
  - Gemfile
52
53
  - LICENSE.txt
53
54
  - README.md
@@ -78,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  - !ruby/object:Gem::Version
79
80
  version: '0'
80
81
  requirements: []
81
- rubygems_version: 3.0.3
82
+ rubygems_version: 3.0.6
82
83
  signing_key:
83
84
  specification_version: 4
84
85
  summary: show_message makes it easy to output your flash[:success], flash[:error]