turbo_flash 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b5d3e6af862d7563fcc1e434ce1fa6fe631279e933daf912068900f3b56deea2
4
+ data.tar.gz: 83ab0367c162d93f2c832848cf797783805895e20f4659a944a13408e0717e8d
5
+ SHA512:
6
+ metadata.gz: 87d1035eb65f988bb9837f184a528dca89a13edcaaf81e66bd95baf253a943231a2ab28c2769d96b0211776f79f42c323c83c543051132311f5a03b9997a8341
7
+ data.tar.gz: 714f176d1065e73d597aea968ebf6ae4ff42619c60b165e2290447e0eaa5ec7c4cfb165c66414b923385488edc90c9da71a577244a0119c9d82dcb09dae96708
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Josh Brody
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.md ADDED
@@ -0,0 +1,141 @@
1
+ # TurboFlash
2
+
3
+ Automagically include your flash messages in your Ruby on Rails [TurboStream](https://github.com/hotwired/turbo-rails) responses.
4
+
5
+ ![Video demo](https://i.imgur.com/vOmf6FV.mp4)
6
+
7
+ ## Usage
8
+
9
+ TurboFlash exposes a `flash.turbo` method that's similar to `flash.now`:
10
+
11
+ ```ruby
12
+ class UsersController < ApplicationController
13
+ def update
14
+ @user = current_user
15
+ unless @user.valid_password?(user_params[:current_password])
16
+ @user.errors.add(:current_password, "is invalid.")
17
+ return respond_to do |f|
18
+ f.html do
19
+ flash.now[:notice] = "There was an error."
20
+ render :show
21
+ end
22
+ f.turbo_stream do
23
+ flash.turbo[:notice] = "There was an error."
24
+ render turbo_stream: turbo_stream.replace(@user, partial: 'form')
25
+ end
26
+ end
27
+ end
28
+
29
+ # ...
30
+ end
31
+
32
+ private
33
+
34
+ def user_params
35
+ params.require(:user).permit(:current_password, :password, :password_confirmation)
36
+ end
37
+ end
38
+ ```
39
+
40
+ Note: You must explicitly call `flash.turbo` (for now). This will not use the original flash hash.
41
+
42
+ Of course, the response will be what you expect:
43
+
44
+ ```ruby
45
+ turbo_stream.replace(@user, partial: 'form')
46
+ ```
47
+
48
+ TurboFlash will also automatically inject the following into your Turbo response:
49
+
50
+ ```ruby
51
+ turbo_stream.update("flash", partial: "shared/flash", locals: { role: :notice, message: "There was an error." })
52
+ ```
53
+
54
+ If you want to get more granular, use `flash.turbo#set_options`:
55
+
56
+ ```ruby
57
+ flash.turbo.set_options(action: :append)[:notice] = "This will be appended."
58
+ ```
59
+
60
+ You could even:
61
+
62
+ ```ruby
63
+ flash.turbo.set_options(action: :append, partial: "layouts/_cool_awesome_flash")[:error] = "This will be appended from the partial cool_awesome_flash with an error role."
64
+ ```
65
+
66
+ The defaults are customizable and shown below.
67
+
68
+ ## Installation
69
+ Add this line to your application's Gemfile:
70
+
71
+ ```ruby
72
+ gem 'turbo_flash'
73
+ ```
74
+
75
+ And then execute:
76
+ ```bash
77
+ $ bundle
78
+ ```
79
+
80
+ Create a partial of `shared/_flash.html.erb`:
81
+
82
+ ```erb
83
+ <div class="flash flash-<%= local_assigns[:role] %>">
84
+ <%= local_assigns[:message] %>
85
+ </div>
86
+ ```
87
+
88
+ Ensure that the TurboStream target — a tag with an `id` of `flash` exists in your layout:
89
+
90
+ ```erb
91
+ <!DOCTYPE html>
92
+ <html>
93
+ <head>
94
+ <%= csrf_meta_tags %>
95
+ <%= csp_meta_tag %>
96
+
97
+ <%= stylesheet_link_tag 'application', media: 'all', data: { turbo_track: :reload } %>
98
+ <%= javascript_pack_tag 'application', data: { turbo_track: :reload } %>
99
+ </head>
100
+
101
+ <body>
102
+ <div id="flash">
103
+ <% flash.each do |role, message| %>
104
+ <%= render(partial: 'shared/flash', locals: { role: role, message: message }) %>
105
+ <% end %>
106
+ </div>
107
+
108
+ <%= yield %>
109
+ </body>
110
+ </html>
111
+ ```
112
+
113
+ ## Configuration
114
+
115
+ In an initializer:
116
+
117
+ ```
118
+ TurboFlash.configure do |config|
119
+ # the default TurboStream target element ID
120
+ # config.target = "flash"
121
+
122
+ # the default TurboStream action
123
+ # config.action = :append
124
+
125
+ # the default TurboStream partial
126
+ # config.partial = 'shared/flash'
127
+
128
+ # the default flash key variable name
129
+ # config.key = :role
130
+
131
+ # the default flash message variable name
132
+ # config.value = :message
133
+ end
134
+ ```
135
+
136
+ ## Contributing
137
+
138
+ There wasn't much thought put into this, but it works for me, so it might work for you!
139
+
140
+ ## License
141
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'turbo_flash/version'
4
+ require 'turbo_flash/railtie'
5
+
6
+ require 'turbo_flash/configuration'
7
+ require 'turbo_flash/flash_turbo'
8
+ require 'turbo_flash/renderer'
9
+
10
+ module TurboFlash
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.config
16
+ yield configuration
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboFlash
4
+ class Configuration
5
+ attr_accessor :target, :action, :partial, :key, :value
6
+
7
+ def initialize
8
+ @target = 'flash'
9
+ @action = :append
10
+ @partial = 'shared/flash'
11
+ @key = :role
12
+ @value = :message
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'turbo_flash/flash_turbo'
4
+
5
+ module TurboFlash
6
+ module FlashHash
7
+ def turbo
8
+ @turbo ||= ::TurboFlash::FlashTurbo.new(self)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # TODO: this is ugly af, but it works
4
+ module TurboFlash
5
+ class FlashTurbo < ::ActionDispatch::Flash::FlashNow
6
+ attr_reader :options
7
+
8
+ def initialize(flash)
9
+ super
10
+ @temp_options = nil
11
+ @default_render_target = TurboFlash.configuration.target
12
+ @default_render_options = { action: TurboFlash.configuration.action, partial: TurboFlash.configuration.partial }
13
+ @locals_key = TurboFlash.configuration.key
14
+ @locals_value = TurboFlash.configuration.value
15
+ @options = {}
16
+ end
17
+
18
+ def []=(k, v)
19
+ super
20
+ @options_for_flash = {}
21
+ @options_for_flash = @default_render_options.dup
22
+ @options_for_flash.merge!({ target: @default_render_target })
23
+ @options_for_flash[:locals] = {
24
+ @locals_key => k,
25
+ @locals_value => v
26
+ }
27
+ if @temp_options
28
+ @options_for_flash.merge!(@temp_options)
29
+ @temp_options = nil
30
+ end
31
+
32
+ @options[k.to_s] = @options_for_flash
33
+ v
34
+ end
35
+
36
+ def set_options(options = {})
37
+ @temp_options = options
38
+ self
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'turbo_flash/renderer'
4
+ require 'turbo_flash/flash_hash'
5
+
6
+ module TurboFlash
7
+ class Railtie < ::Rails::Railtie
8
+ initializer 'turbo_flash.initialize' do
9
+ ::ActiveSupport.on_load(:action_controller) do
10
+ ActionController::Base.include TurboFlash::Renderer
11
+ ActionDispatch::Flash::FlashHash.include TurboFlash::FlashHash
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboFlash
4
+ module Renderer
5
+ def render(*args)
6
+ result = super
7
+ if request.format.turbo_stream?
8
+ flash_turbo_options = flash.turbo.options
9
+ flash.turbo.instance_variable_get(:@flash).instance_variable_get(:@flashes).each do |key, _value|
10
+ flash_options = flash_turbo_options[key]
11
+ result << turbo_stream.send(flash_options.delete(:action), flash_options.delete(:target), **flash_options)
12
+ end
13
+ end
14
+ result
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TurboFlash
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turbo_flash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hotwire-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ description: Automagically include your flash messages in your Ruby on Rails TurboStream
28
+ responses.
29
+ email:
30
+ - git@josh.mn
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - lib/turbo_flash.rb
38
+ - lib/turbo_flash/configuration.rb
39
+ - lib/turbo_flash/flash_hash.rb
40
+ - lib/turbo_flash/flash_turbo.rb
41
+ - lib/turbo_flash/railtie.rb
42
+ - lib/turbo_flash/renderer.rb
43
+ - lib/turbo_flash/version.rb
44
+ homepage: https://github.com/joshmn/turbo_flash
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/joshmn/turbo_flash
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.1.4
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Automagically include your flash messages in your Ruby on Rails TurboStream
68
+ responses.
69
+ test_files: []