typical_situation 1.0.0 → 1.0.2

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: 59384fee031d1440a474689f99be97760aedc645343e4febf4d1356ed05ee801
4
+ data.tar.gz: '08b482a57e976dbe1ac86604648a4f237346c09db88c672483dfae604d566cf1'
5
5
  SHA512:
6
- metadata.gz: cee57ac0c5eea896aea026f06a9889cb832ce98a303e95a6a18d9b91fe9fee9d54a5bcb19fbfa8f52279a73416aa818a2fbb88335113c29c04d2b299ebc51b8e
7
- data.tar.gz: de2e3f522926679ae805b952c06ca33409d953d0d4ed3a1482081337bc1d6b87ab20bc7bbc89727037deb5fca5cef51732972c158b13c60d593d1a4a774e1835
6
+ metadata.gz: dbcec21abd36a2da29b901bb2ffcb0468b6cf4170b4e974bef1cd640d174813c557fa8a87bddcda23e92c5709ea5c89c4fa39de7968f62ea08d5db6ac99a01a3
7
+ data.tar.gz: d12f5d0e3d6408a7060d46704e8dbe47cc516119fd7ae360e863bd3d00592def5e0ce99ac2bd2e6b491575284022469299689fef943368831003da8235c0a5a3
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,6 @@
1
+ require 'rails/engine'
2
+
3
+ module TypicalSituation
4
+ class Engine < Rails::Engine
5
+ end
6
+ end
@@ -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.2"
5
5
  end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "typical_situation/identity"
4
- require "typical_situation/permissions"
5
- require "typical_situation/actions"
6
- require "typical_situation/operations"
7
- require "typical_situation/responses"
3
+ require 'typical_situation/engine'
4
+ require 'typical_situation/identity'
5
+ require 'typical_situation/permissions'
6
+ require 'typical_situation/flash_messages'
7
+ require 'typical_situation/actions'
8
+ require 'typical_situation/operations'
9
+ require 'typical_situation/responses'
8
10
 
9
11
  module TypicalSituation
10
12
  class Error < StandardError; end
@@ -12,6 +14,7 @@ module TypicalSituation
12
14
 
13
15
  include Identity
14
16
  include Permissions
17
+ include FlashMessages
15
18
  include Operations
16
19
  include Responses
17
20
 
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mars Hall
@@ -190,8 +190,11 @@ 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/engine.rb
197
+ - lib/typical_situation/flash_messages.rb
195
198
  - lib/typical_situation/identity.rb
196
199
  - lib/typical_situation/operations.rb
197
200
  - lib/typical_situation/permissions.rb