typical_situation 1.0.0 → 1.0.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
  SHA256:
3
- metadata.gz: e4c1f0edb5145c0cbeab8bd20520a2be77459ff68280f2dfd99350e51c66c288
4
- data.tar.gz: f72c7aba90b5984f87ddd5cba2982c512292e4ac1f44603a07eec230db4ccdee
3
+ metadata.gz: 4fdb5e52424eb45d6f3b9bf32e1977a9b4c1375b5b89487a9a700c8450be40a6
4
+ data.tar.gz: a92d7ed71e6d90bf6555b1326daef2a69082739230c7476cf6db596cfb565e8d
5
5
  SHA512:
6
- metadata.gz: cee57ac0c5eea896aea026f06a9889cb832ce98a303e95a6a18d9b91fe9fee9d54a5bcb19fbfa8f52279a73416aa818a2fbb88335113c29c04d2b299ebc51b8e
7
- data.tar.gz: de2e3f522926679ae805b952c06ca33409d953d0d4ed3a1482081337bc1d6b87ab20bc7bbc89727037deb5fca5cef51732972c158b13c60d593d1a4a774e1835
6
+ metadata.gz: 1b2f4d58609addf3c35eb05a98f1df8b5507728c7aca7d03c342466256064e9490d420f5c373479affdfa46aef1d8b5555d0c8269a5742489d17d536d546e391
7
+ data.tar.gz: bc4fab13796303a61ac7ceee09ecdc42a61aa46ccfe03b3a636df64aadd6fa85e7dd48e451c0e55812932657a3b590a9dff56c57d69122097b166ecb62c3d7e8
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Typical Situation [![Spec CI](https://github.com/apsislabs/typical_situation/workflows/Spec%20CI/badge.svg)](https://github.com/apsislabs/typical_situation/actions)
2
2
 
3
- The missing Ruby on Rails ActionController REST API mixin.
3
+ The missing Ruby on Rails ActionController REST API mixin. Originally forked from https://github.com/mars/typical_situation
4
4
 
5
5
  A Ruby mixin (module) providing the seven standard resource actions & responses for an ActiveRecord :model_type & :collection.
6
6
 
@@ -199,6 +199,58 @@ end
199
199
 
200
200
  By default, `TypicalSituation` permits all parameters (`permit!`) when these methods return `nil` or an empty array. Override them to restrict parameters for security.
201
201
 
202
+ #### Flash Messages
203
+
204
+ Add to `config/locales/en.yml`:
205
+
206
+ ```yaml
207
+ en:
208
+ typical_situation:
209
+ flash:
210
+ create:
211
+ success: "%{resource} was successfully created"
212
+ update:
213
+ success: "%{resource} was successfully updated"
214
+ destroy:
215
+ success: "%{resource} was successfully deleted"
216
+ ```
217
+
218
+ **Custom messages per resource:**
219
+
220
+ ```yaml
221
+ en:
222
+ posts:
223
+ flash:
224
+ create:
225
+ success: "Your blog post is now live"
226
+ ```
227
+
228
+ Override translation helper:
229
+
230
+ ```ruby
231
+ def translate_with_resource(key)
232
+ model_key = "#{resource_name}.flash.#{key}"
233
+ if I18n.exists?(model_key)
234
+ I18n.t(model_key, resource: resource_name.humanize)
235
+ else
236
+ I18n.t("typical_situation.flash.#{key}", resource: resource_name.humanize)
237
+ end
238
+ end
239
+ ```
240
+
241
+ **Custom flash logic:**
242
+
243
+ ```ruby
244
+ def set_success_flash(action)
245
+ case action
246
+ when :create
247
+ flash[:notice] = "Your #{resource_name.humanize.downcase} is live"
248
+ when :destroy
249
+ flash[:warning] = translate_with_resource("#{action}.success")
250
+ end
251
+ end
252
+ ```
253
+
202
254
  #### Authorization
203
255
 
204
256
  Control access to resources by overriding the `authorized?` method:
@@ -0,0 +1,9 @@
1
+ en:
2
+ typical_situation:
3
+ flash:
4
+ create:
5
+ success: "%{resource} was successfully created"
6
+ update:
7
+ success: "%{resource} was successfully updated"
8
+ destroy:
9
+ success: "%{resource} was successfully deleted"
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypicalSituation
4
+ module FlashMessages
5
+ private
6
+
7
+ def set_success_flash(action)
8
+ flash[:notice] = translate_with_resource("#{action}.success")
9
+ end
10
+
11
+ def set_error_flash
12
+ return unless @resource&.errors&.any?
13
+
14
+ flash[:error] = @resource.errors.full_messages.join(", ")
15
+ end
16
+
17
+ def translate_with_resource(key)
18
+ I18n.t(
19
+ "typical_situation.flash.#{key}",
20
+ resource: model_type.to_s.humanize
21
+ )
22
+ end
23
+ end
24
+ end
@@ -45,6 +45,7 @@ module TypicalSituation
45
45
  yield(format) if block_given?
46
46
 
47
47
  format.html do
48
+ set_success_flash(:update)
48
49
  set_single_instance
49
50
  changed_so_redirect || render
50
51
  end
@@ -63,6 +64,7 @@ module TypicalSituation
63
64
  yield(format) if block_given?
64
65
 
65
66
  format.html do
67
+ set_success_flash(:create)
66
68
  set_single_instance
67
69
  changed_so_redirect || render
68
70
  end
@@ -93,6 +95,7 @@ module TypicalSituation
93
95
 
94
96
  def respond_as_gone
95
97
  if has_errors?
98
+ set_error_flash
96
99
  respond_as_error
97
100
  else
98
101
  respond_to do |format|
@@ -149,6 +152,8 @@ module TypicalSituation
149
152
 
150
153
  # HTML response when @resource deleted.
151
154
  def gone_so_redirect
155
+ set_error_flash if has_errors?
156
+ set_success_flash(:destroy) unless has_errors?
152
157
  redirect_to after_resource_destroyed_path(@resource)
153
158
  true
154
159
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypicalSituation
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "typical_situation/identity"
4
4
  require "typical_situation/permissions"
5
+ require "typical_situation/flash_messages"
5
6
  require "typical_situation/actions"
6
7
  require "typical_situation/operations"
7
8
  require "typical_situation/responses"
@@ -12,6 +13,7 @@ module TypicalSituation
12
13
 
13
14
  include Identity
14
15
  include Permissions
16
+ include FlashMessages
15
17
  include Operations
16
18
  include Responses
17
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typical_situation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mars Hall
@@ -190,8 +190,10 @@ files:
190
190
  - MIT-LICENSE
191
191
  - README.md
192
192
  - Rakefile
193
+ - config/locales/typical_situation.en.yml
193
194
  - lib/typical_situation.rb
194
195
  - lib/typical_situation/actions.rb
196
+ - lib/typical_situation/flash_messages.rb
195
197
  - lib/typical_situation/identity.rb
196
198
  - lib/typical_situation/operations.rb
197
199
  - lib/typical_situation/permissions.rb