base_editing_bootstrap 0.14.0 → 0.15.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: 1f4da39d416d3107b6e4f40d63a33c136eed02e5d5c8cb6b2e91423be99460c1
4
- data.tar.gz: f71d9cfd3efeef8edbd20ee5da7ec9b98f198ce6a9fc99ed7b8cc0af7038983c
3
+ metadata.gz: 6aef652b3fc0651d54b2a217bbb32a6e750135ffc1216faa82dee66ad788e5b4
4
+ data.tar.gz: b9cf8df48a6c26bb2e67b7d65d9059e4fa974defb7e58ef1863f8e065e75af7a
5
5
  SHA512:
6
- metadata.gz: c93f3d73d379b66d37a039064ac5f6cb8638f32e9785d2998c7d3353f060dde6c6be02db38d8ac6f1e2ef29e3e0a1ab4e265b73959bbd3dacbff5ecc367d87e1
7
- data.tar.gz: ef9c876ff6628cdbc7b200240b3785952e58d951a610bd8275a587d2d6c9b6ea03724bd03c5f6def17c81c04b268a79c5330a7288931418ace32062356c214df
6
+ metadata.gz: 0d639657a9314a263c38b30e390af56f21b94071f835f4d59ccea7818f7799a3899081c534e2183349122968bd81347904ea1eb46996fa9190521039785b881d
7
+ data.tar.gz: b87959282627c61dc5b8f233503a3932cc0cb0a53ba25a192065a9ee765b95a754b55efc5998e4ec1032ca9ecdee0db334edce0f19563621c0026d97d873be2e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
  All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.
3
3
 
4
4
  - - -
5
+ ## 0.15.0 - 2024-11-11
6
+ #### Bug Fixes
7
+ - Add Password for in standard fields - (593c8c2) - Marino Bonetti
8
+ - Form helper for non ActiveRecords - (9e124de) - Marino Bonetti
9
+ - Removed old code - (cbf1cd0) - Marino Bonetti
10
+ - Add translations for actionmodel - (3be05ca) - Marino Bonetti
11
+ #### Features
12
+ - Add debug helper logger for PunditNotAuthorized - (a9b30d6) - Marino Bonetti
13
+ - Human action messages (#10) - (63f9de9) - Marino Bonetti
14
+ #### Tests
15
+ - Add check for human_action_message method - (38af466) - Marino Bonetti
16
+ - Better tests messages - (cda150e) - Marino Bonetti
17
+
18
+ - - -
19
+
5
20
  ## 0.14.0 - 2024-10-14
6
21
  #### Bug Fixes
7
22
  - Namespaced resources (#7) - (e85f926) - Marino Bonetti
data/README.md CHANGED
@@ -221,6 +221,29 @@ RSpec.describe ServicePolicy, type: :policy do
221
221
  end
222
222
  ```
223
223
 
224
+ ## Message translations
225
+ I messaggi di generati per il flash provengono dal metodo BaseEditingBootstrap::ActionTranslation.human_action_message
226
+ e seguono una logica simile ad human_attribute_name.
227
+ Sono già presenti i messaggi di default, a cui viene passato il nome del modello,
228
+ ma è possibile fare override del messaggio con la classe:
229
+
230
+ ```yaml
231
+ LANG:
232
+ activerecord:
233
+ successful:
234
+ messages:
235
+ created: "example %{model}"
236
+ updated:
237
+ destroyed:
238
+ CLASS_NAME:
239
+ created: "customized %{model} created"
240
+ unsuccessful:
241
+ messages:
242
+ created:
243
+ updated:
244
+ ```
245
+
246
+
224
247
 
225
248
  ## Contributing
226
249
  1. Setup env with:
@@ -186,13 +186,13 @@ class BaseEditingController < RestrictedAreaController
186
186
  format.all do
187
187
  redirect_to index_custom_polymorphic_path(base_class),
188
188
  status: :see_other,
189
- notice: t('activerecord.successful.messages.destroyed', model: base_class.model_name.human)
189
+ notice: human_action_message(action: :destroyed)
190
190
  end
191
191
  end
192
192
 
193
193
  def _failed_create(format)
194
194
  format.html do
195
- flash.now.alert = t('activerecord.unsuccessful.messages.created', model: base_class.model_name.human)
195
+ flash.now.alert = human_action_message(action: :created, successful: false)
196
196
  render action: :new, status: :unprocessable_entity
197
197
  end
198
198
  end
@@ -207,13 +207,13 @@ class BaseEditingController < RestrictedAreaController
207
207
  end
208
208
  redirect_to path,
209
209
  status: :see_other,
210
- notice: t('activerecord.successful.messages.created', model: base_class.model_name.human)
210
+ notice: human_action_message(action: :created)
211
211
  end
212
212
  end
213
213
 
214
214
  def _failed_update(format)
215
215
  format.html do
216
- flash.now.alert = t('activerecord.unsuccessful.messages.updated', model: base_class.model_name.human)
216
+ flash.now.alert = human_action_message(action: :updated, successful: false)
217
217
  render action: :edit, status: :unprocessable_entity
218
218
  end
219
219
  end
@@ -228,7 +228,10 @@ class BaseEditingController < RestrictedAreaController
228
228
  end
229
229
  redirect_to path,
230
230
  status: :see_other,
231
- notice: t('activerecord.successful.messages.updated', model: base_class.model_name.human)
231
+ notice: human_action_message(action: :updated)
232
232
  end
233
233
  end
234
+
235
+ delegate :human_action_message, to: :base_class
236
+
234
237
  end
@@ -18,6 +18,7 @@ class RestrictedAreaController < BaseEditingBootstrap.inherited_controller.const
18
18
  private
19
19
 
20
20
  def user_not_authorized(exception)
21
+ Rails.logger.debug{"Pundit::NotAuthorizedError [#{exception.message}]"}
21
22
  policy_name = exception.policy.class.to_s.underscore
22
23
 
23
24
  flash[:error] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
@@ -18,10 +18,14 @@ module Utilities
18
18
  # @param [Symbol] field
19
19
  def form_print_field(form, field)
20
20
  locals = {form:, field:}
21
- if form.object.class.defined_enums.key?(field.to_s)
21
+ if form.object.class.respond_to?(:defined_enums) && form.object.class.defined_enums.key?(field.to_s)
22
22
  generic_field = "enum"
23
23
  else
24
- type = form.object.class.type_for_attribute(field).type
24
+ if form.object.class.respond_to?(:type_for_attribute)
25
+ type = form.object.class.type_for_attribute(field).type
26
+ else
27
+ type = :string
28
+ end
25
29
  case type
26
30
  when :datetime
27
31
  generic_field = "datetime"
@@ -40,6 +40,16 @@ it:
40
40
  # post_policy:
41
41
  # update?: 'You cannot edit this post!'
42
42
  # create?: 'You cannot create posts!'
43
+ activemodel:
44
+ successful:
45
+ messages:
46
+ created: "%{model} creato correttamente."
47
+ updated: "%{model} aggiornato correttamente."
48
+ destroyed: "%{model} è stato correttamente cancellato."
49
+ unsuccessful:
50
+ messages:
51
+ created: "Ci sono errori che impediscono la creazione di %{model}"
52
+ updated: "Ci sono errori che impediscono l'aggiornamento di %{model}"
43
53
  activerecord:
44
54
  successful:
45
55
  messages:
@@ -1 +1 @@
1
- 0.14.0
1
+ 0.15.0
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/concern'
3
+
4
+ module BaseEditingBootstrap
5
+ module ActionTranslation
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+
11
+ ##
12
+ # Viene generato un messaggio rispetto all'azione nel caso di successo o fallimento
13
+ # i messaggi seguono la stessa logica del human_attribute_name con inheritance delle classi`
14
+ #
15
+ # {i18n_scope della classe}.{successful|unsuccessful}.messages.{class_name}.{action}
16
+ # {i18n_scope della classe}.{successful|unsuccessful}.messages.{class_ancestors}.{action}
17
+ # {i18n_scope della classe}.{successful|unsuccessful}.messages.{action}
18
+ def human_action_message(action:, successful: true, **options)
19
+
20
+ successful_string = successful ? "successful" : "unsuccessful"
21
+
22
+ defaults = lookup_ancestors.map do |klass|
23
+ :"#{i18n_scope}.#{successful_string}.messages.#{klass.model_name.i18n_key}.#{action}"
24
+ end
25
+
26
+ defaults << options[:default] if options[:default]
27
+ defaults << :"#{i18n_scope}.#{successful_string}.messages.#{action}"
28
+
29
+ options.reverse_merge!(model: model_name.human)
30
+
31
+ I18n.t(
32
+ defaults.shift,
33
+ default: defaults,
34
+ **options
35
+ )
36
+ end
37
+ end
38
+ end
39
+ end
@@ -6,6 +6,7 @@ module BaseEditingBootstrap
6
6
 
7
7
  included do
8
8
  include IsValidated
9
+ include ActionTranslation
9
10
  delegate :ransackable_attributes, :ransackable_associations, to: :@class
10
11
  end
11
12
 
@@ -16,8 +17,8 @@ module BaseEditingBootstrap
16
17
  else
17
18
  Pundit.policy(User.new, self.new).permitted_attributes_for_ransack.map(&:to_s)
18
19
  end
19
- end
20
-
20
+ end
21
+
21
22
  def ransackable_associations(auth_object = nil)
22
23
  if auth_object
23
24
  Pundit.policy(auth_object, self.new).permitted_associations_for_ransack.map(&:to_s)
@@ -4,6 +4,7 @@ module BaseEditingBootstrap::Forms
4
4
  class Base < ActionView::Helpers::FormBuilder
5
5
  [
6
6
  :text_field,
7
+ :password_field,
7
8
  :text_area,
8
9
  :date_field,
9
10
  :datetime_field,
@@ -130,6 +130,7 @@ RSpec.shared_examples "base editing controller" do |factory: nil, only: [], exce
130
130
  # edit
131
131
  expect(response).to redirect_to(url_for_edit.call(assigns[:object]))
132
132
  end
133
+ expect(flash.to_hash).to include("notice" => be_present)
133
134
  end
134
135
 
135
136
  unless skip_invalid_checks
@@ -154,6 +155,7 @@ RSpec.shared_examples "base editing controller" do |factory: nil, only: [], exce
154
155
  # edit
155
156
  expect(response).to redirect_to(url_for_edit.call(assigns[:object]))
156
157
  end
158
+ expect(flash.to_hash).to include("notice" => be_present)
157
159
  end
158
160
 
159
161
  unless skip_invalid_checks
@@ -171,6 +173,7 @@ RSpec.shared_examples "base editing controller" do |factory: nil, only: [], exce
171
173
  delete url_for(persisted_instance)
172
174
  expect(assigns[:object]).to be_an_instance_of(model)
173
175
  expect(response).to redirect_to(url_for_succ_delete)
176
+ expect(flash.to_hash).to include("notice" => be_present)
174
177
  end
175
178
 
176
179
  it "not valid" do
@@ -180,7 +183,7 @@ RSpec.shared_examples "base editing controller" do |factory: nil, only: [], exce
180
183
  end
181
184
  delete url_for(persisted_instance)
182
185
  expect(response).to redirect_to(url_for_fail_delete)
183
- expect(flash[:error]).not_to be_blank
186
+ expect(flash.to_hash).to include("error" => be_present)
184
187
  end
185
188
  end
186
189
  end
@@ -199,7 +202,7 @@ RSpec.shared_examples "fail with unauthorized" do |request: default_unathorized_
199
202
 
200
203
  instance_exec(&request)
201
204
  expect(response).to redirect_to(root_path)
202
- expect(flash[:error]).not_to be_nil
205
+ expect(flash.to_hash).to include("error" => be_present)
203
206
  end
204
207
  end
205
208
 
@@ -2,6 +2,10 @@ RSpec.shared_examples "a base model" do |ransack_permitted_attributes: [], ransa
2
2
 
3
3
  it_behaves_like "a validated? object"
4
4
 
5
+ it "human_action_message" do
6
+ expect(described_class).to respond_to(:human_action_message)
7
+ end
8
+
5
9
  describe "with ransackables" do
6
10
  where(:base_model_method, :policy_method, :policy_output, :result) do
7
11
  [
@@ -54,7 +58,6 @@ end
54
58
 
55
59
  RSpec.shared_examples "a validated? object" do
56
60
  subject {
57
- # prendiamo un modello a caso per testare questa cosa
58
61
  described_class.new
59
62
  }
60
63
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base_editing_bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marino Bonetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-14 00:00:00.000000000 Z
11
+ date: 2024-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -358,6 +358,7 @@ files:
358
358
  - config/routes.rb
359
359
  - lib/base_editing_bootstrap.rb
360
360
  - lib/base_editing_bootstrap/VERSION
361
+ - lib/base_editing_bootstrap/action_translation.rb
361
362
  - lib/base_editing_bootstrap/base_model.rb
362
363
  - lib/base_editing_bootstrap/engine.rb
363
364
  - lib/base_editing_bootstrap/forms/base.rb