base_editing_bootstrap 1.4.0 → 1.5.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: ec2173d4ede7597e31056daba4097d643b427878115e0e2898e5e43c09d80d80
4
- data.tar.gz: 6e8325daa90163681e969003f1a32612eab37e0bcc40fac78618b5f79cf24d35
3
+ metadata.gz: aee3afb2ce6b96c834dbc1491588c42355e8e726ce1ba030f1ebd9c31d368800
4
+ data.tar.gz: 6ee2f8827e7ec00045a710c113f1e432a874a3900d840257f9602902aaad6466
5
5
  SHA512:
6
- metadata.gz: 7ce5b95dd4da6535fbe6e3388d7dbc65b4ec11ea1c304b4141f7c299a2974d3a30b72ce65fb2d000216fb92cc028c24ef4b2c4bdd4819c033bc50696ea8ce801
7
- data.tar.gz: d923d2daed952f049ad97b13e723d6c5343639fce7b873d923aee776bfca3f3021e78d45db4c199c524a9a4a8ebd1bd5a6c5ea509c0a14ba5bbd9644b546c0e8
6
+ metadata.gz: ce1a79b4abb27bc8f1297b7100bdc0ed5beb6589a0fe2ddf0915bc5f258a85cfa071644a84f35ff1b0a03444c57246d15bebb2a1dcf9a738c654675f48f45b19
7
+ data.tar.gz: b65de7345289bb1ef0724f27279a4afba4017ca3620714982b94f1fdc958e0322b91c8ee9a3e3ccc82040bd5c2a7b632b376a34f5e95d7ee7c00364e8efd57e3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,25 @@
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
+ ## 1.5.1 - 2025-01-22
6
+ #### Bug Fixes
7
+ - Ricerca template anche con contesto del controller - (ac93a9f) - Marino Bonetti
8
+ #### Miscellaneous Chores
9
+ - Remove unused files - (0b56f94) - Marino Bonetti
10
+
11
+ - - -
12
+
13
+ ## 1.5.0 - 2025-01-16
14
+ #### Documentation
15
+ - Update documentation - (3a29e34) - Marino Bonetti
16
+ #### Features
17
+ - Render enum in index - (c520cd1) - Marino Bonetti
18
+ - Belongs_to Form fields - (6572b9f) - Marino Bonetti
19
+ #### Refactoring
20
+ - Estrapolazione policy in metodo - (711746b) - Marino Bonetti
21
+
22
+ - - -
23
+
5
24
  ## 1.4.0 - 2025-01-13
6
25
  #### Bug Fixes
7
26
  - Correct generator for values - (a83f07a) - Marino Bonetti
data/README.md CHANGED
@@ -145,7 +145,20 @@ Utilizzo per modello base, in questo esempio prendiamo come modello Post come es
145
145
  **Cell Field**:
146
146
  - created_at => timestamps.html.erb
147
147
  - updated_at => timestamps.html.erb
148
+ - Enum => _enum.html.erb
149
+ Per gli enum, le traduzioni dei labels di ogni valore provengono da i18n
150
+ attraverso l'helper: `Utilities::EnumHelper#enum_translation` con variant `:cell_field`
151
+ il quale sfrutta human_attribute_name del modello con 'attributo.enum_value',
152
+ quindi ad esempio per un modello `Post` con enum `categoria` e un enum `importante`, la ricerca nelle traduzioni
153
+ saranno così composte:
154
+ - it.activerecord.attributes.post/categoria.importante_cell_field
155
+ - it.activerecord.attributes.categoria.importante_cell_field
156
+ - it.attributes.importante_cell_field
157
+ - it.activerecord.attributes.post/categoria.importante
158
+ - it.activerecord.attributes.categoria.importante
159
+ - it.attributes.importante => nil
148
160
  - default => base.html.erb
161
+
149
162
  **Form Field**
150
163
  - Integer => _integer.html.erb
151
164
  - Float => _decimal.html.erb
@@ -154,9 +167,23 @@ Utilizzo per modello base, in questo esempio prendiamo come modello Post come es
154
167
  - Date => _date.html.erb
155
168
  - Boolean => _boolean.html.erb
156
169
  - Enum => _enum.html.erb
157
- Per gli enum, le traduzioni dei labels di ogni valore provvengono da i18n
158
- attraverso l'helper: `Utilities::EnumHelper#enum_translation`
159
- il quale utilizza il nome dell'attributo con
170
+ Per gli enum, le traduzioni dei labels di ogni valore provengono da i18n
171
+ attraverso l'helper: `Utilities::EnumHelper#enum_translation`" con variante `:form_field`
172
+ il quale sfrutta human_attribute_name del modello con 'attributo.enum_value',
173
+ quindi ad esempio per un modello `Post` con enum `categoria` e un enum `importante`, la ricerca nelle traduzioni
174
+ saranno così composte:
175
+ - it.activerecord.attributes.post/categoria.importante_form_field
176
+ - it.activerecord.attributes.categoria.importante_form_field
177
+ - it.attributes.importante_form_field
178
+ - it.activerecord.attributes.post/categoria.importante
179
+ - it.activerecord.attributes.categoria.importante
180
+ - it.attributes.importante => nil
181
+ - belongs_to => _belongs_to_select.html.erb
182
+ Come si può leggere dal partial, il modello che viene utilizzato come base dati per la collection deve
183
+ avere come metodo `option_label` che deve ritornare la label da utilizzare nelle options.
184
+ Di default questo metodo utilizza il semplice #to_s
185
+ Ha anche un metodo per il valore da utilizzare come chiave, di default viene dedotto dalla reflection
186
+ come anche il nome della classe da utilizzare come sorgente dei dati della collection
160
187
  - Default/String => _base.html.erb
161
188
 
162
189
  In futuro si prevede di aggiungere automatismi per renderizzare senza
@@ -3,6 +3,7 @@ module Utilities
3
3
  include TemplateHelper
4
4
  include EnumHelper
5
5
  include IconHelper
6
+ include Pundit::Authorization
6
7
  ##
7
8
  # Metodo su cui eseguire override per i campi specifici rispetto all'oggetto gestito dal controller
8
9
  # @deprecated Utilizza form_print_field(form, field) senza sovrascriverlo
@@ -20,7 +21,16 @@ module Utilities
20
21
  def form_print_field(form, field)
21
22
  locals = {form:, field:}
22
23
  if form.object.class.respond_to?(:defined_enums) && form.object.class.defined_enums.key?(field.to_s)
24
+ type = :enum
23
25
  generic_field = "enum"
26
+ elsif form.object.class.respond_to?(:reflect_on_association) &&
27
+ form.object.class.reflect_on_association(field.to_s).is_a?(ActiveRecord::Reflection::BelongsToReflection)
28
+ # Abbiamo una relazione belongs_to da gestire
29
+ reflection = form.object.class.reflect_on_association(field.to_s)
30
+ type = :belongs_to
31
+ generic_field = "belongs_to_select"
32
+ locals[:relation_class] = reflection.klass
33
+ locals[:foreign_key] = reflection.foreign_key
24
34
  else
25
35
  if form.object.class.respond_to?(:type_for_attribute)
26
36
  type = form.object.class.type_for_attribute(field).type
@@ -61,8 +71,8 @@ module Utilities
61
71
  "form_field",
62
72
  generic_field
63
73
  )
64
- Rails.logger.debug { "#{type}->#{generic_field}->#{template}->#{ locals.inspect}" }
65
- render template, **locals
74
+ Rails.logger.debug { "#{type}->#{generic_field}->#{template.short_identifier}->#{ locals.inspect}" }
75
+ template.render(self, locals)
66
76
  end
67
77
 
68
78
  end
@@ -3,6 +3,7 @@ module Utilities
3
3
  include TemplateHelper
4
4
  include PageHelper
5
5
  include IconHelper
6
+ include EnumHelper
6
7
 
7
8
  ##
8
9
  # Per aggiungere bottoni:
@@ -31,7 +32,7 @@ module Utilities
31
32
  # @return [ActiveSupport::SafeBuffer]
32
33
  def render_cell_field(obj, field)
33
34
  template = template_for_column(obj.class, field, "cell_field")
34
- render template, obj:, field:
35
+ template.render(self,{obj:, field:})
35
36
  end
36
37
 
37
38
  ##
@@ -41,7 +42,7 @@ module Utilities
41
42
  # @return [ActiveSupport::SafeBuffer]
42
43
  def render_header_cell_field(search_instance, field)
43
44
  template = template_for_column(search_instance.model_klass, field, "header_field")
44
- render template, obj: search_instance.model_klass, field:, search_instance: search_instance
45
+ template.render(self,{obj: search_instance.model_klass, field:, search_instance: search_instance})
45
46
  end
46
47
 
47
48
  ##
@@ -71,10 +72,16 @@ module Utilities
71
72
  when :created_at, :updated_at
72
73
  "timestamps"
73
74
  else
74
- type = klazz.type_for_attribute(field).type
75
+ if klazz.respond_to?(:defined_enums) && klazz.defined_enums.key?(field.to_s)
76
+ type = :enum
77
+ else
78
+ type = klazz.type_for_attribute(field).type
79
+ end
75
80
  case type
76
81
  when :boolean
77
82
  "boolean"
83
+ when :enum
84
+ "enum"
78
85
  else
79
86
  "base"
80
87
  end
@@ -17,10 +17,23 @@ module Utilities::TemplateHelper
17
17
  # avere la partial_path
18
18
  partial_path = (obj.respond_to? :to_partial_path) ? obj.to_partial_path : obj._to_partial_path
19
19
  obj_base_path = "#{partial_path}/#{base_path}"
20
- return "#{obj_base_path}/#{field}" if lookup_context.exists?(field, [obj_base_path], true)
21
- return "#{obj_base_path}/#{generic_field}" if lookup_context.exists?(generic_field, [obj_base_path], true)
22
- return "base_editing/#{base_path}/#{generic_field}" if lookup_context.find_all("base_editing/#{base_path}/_#{generic_field}").any?
23
- "base_editing/#{base_path}/base"
20
+
21
+ # Precedenza modello e campo specifico
22
+ if lookup_context.exists?(field, [obj_base_path], true)
23
+ return lookup_context.find(field, [obj_base_path], true)
24
+ end
25
+ # Ricerca tramite campo generico e prefissi di contesto che contiene anche controller e namespace di controller
26
+ if lookup_context.exists?("#{base_path}/#{generic_field}", lookup_context.prefixes, true)
27
+ view = lookup_context.find_all("#{base_path}/#{generic_field}", lookup_context.prefixes, true)
28
+ return view.first
29
+ end
30
+ if lookup_context.exists?(generic_field, [obj_base_path], true)
31
+ return lookup_context.find(generic_field, [obj_base_path], true)
32
+ end
33
+ if lookup_context.exists?("base_editing/#{base_path}/#{generic_field}", [], true)
34
+ return lookup_context.find_all("base_editing/#{base_path}/#{generic_field}", [], true).first
35
+ end
36
+ lookup_context.find("base_editing/#{base_path}/base", [], true)
24
37
  end
25
38
 
26
39
  end
@@ -0,0 +1,3 @@
1
+ <%# Renderizza il campo nella tabella della pagina dei risultati %>
2
+ <%# locals: (obj:,field:) -%>
3
+ <td><%= enum_translation(obj.class, field, obj.send(field), :cell_field) %></td>
@@ -0,0 +1,6 @@
1
+ <%# locals: (form:, field:,relation_class:,foreign_key:,value_method: :id, label_method: :option_label) -%>
2
+ <%= form.select(foreign_key, options_from_collection_for_select(policy_scope(relation_class),
3
+ value_method, label_method,
4
+ selected: form.object.send(foreign_key)),
5
+ include_blank: true
6
+ ) %>
@@ -1,2 +1,2 @@
1
1
  <%# locals: (form:, field:) -%>
2
- <%= form.select(field, enum_collection(form.object.class, field)) %>
2
+ <%= form.select(field, enum_collection(form.object.class, field, :form_field)) %>
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.5.1
@@ -8,6 +8,14 @@ module BaseEditingBootstrap
8
8
  include IsValidated
9
9
  include ActionTranslation
10
10
  delegate :ransackable_attributes, :ransackable_associations, to: :@class
11
+
12
+
13
+ ##
14
+ # Label da utilizzare nelle option per quando si genera le select dei belongs to
15
+ # @return [String,NilClass]
16
+ def option_label
17
+ to_s
18
+ end
11
19
  end
12
20
 
13
21
  class_methods do
@@ -37,21 +37,27 @@ module BaseEditingBootstrap::Searches
37
37
  end
38
38
 
39
39
  def search_fields
40
- Pundit.policy(@user, @model_klass).search_fields.collect { |f| Field.new(self, f) }
40
+ policy.search_fields.collect { |f| Field.new(self, f) }
41
41
  end
42
42
 
43
43
  def search_result_fields
44
- Pundit.policy(@user, @model_klass).search_result_fields
44
+ policy.search_result_fields
45
45
  end
46
46
 
47
47
  ##
48
48
  # Ritorna se il campo deve essere ordinabile o meno
49
49
  def sortable?(field)
50
- Pundit.policy(@user, @model_klass).sortable_search_result_fields.include?(field)
50
+ policy.sortable_search_result_fields.include?(field)
51
51
  end
52
52
 
53
53
  def persisted?
54
54
  false
55
55
  end
56
+
57
+ ##
58
+ # @return [ApplicationPolicy]
59
+ def policy
60
+ Pundit.policy(@user, @model_klass)
61
+ end
56
62
  end
57
63
  end
@@ -1,4 +1,4 @@
1
- RSpec.shared_examples "a base model" do |ransack_permitted_attributes: [], ransack_permitted_associations: []|
1
+ RSpec.shared_examples "a base model" do |ransack_permitted_attributes: [], ransack_permitted_associations: [], option_label_method: :to_s|
2
2
 
3
3
  it_behaves_like "a validated? object"
4
4
 
@@ -6,6 +6,14 @@ RSpec.shared_examples "a base model" do |ransack_permitted_attributes: [], ransa
6
6
  expect(described_class).to respond_to(:human_action_message)
7
7
  end
8
8
 
9
+ it "have method for belongs_to options" do
10
+ instance = described_class.new
11
+ expect(instance).to respond_to(:option_label)
12
+
13
+ expect(instance).to receive(option_label_method).and_call_original
14
+ instance.option_label
15
+ end
16
+
9
17
  ##
10
18
  # Oggetto solitamente di classe User che identifichi l'utente a cui eseguire il check dei permessi
11
19
  let(:auth_object) { :auth_object }
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: 1.4.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marino Bonetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-13 00:00:00.000000000 Z
11
+ date: 2025-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -320,11 +320,8 @@ files:
320
320
  - README.md
321
321
  - Rakefile
322
322
  - app/assets/config/base_editing_bootstrap_manifest.js
323
- - app/assets/images/base_editing_bootstrap/.keep
324
- - app/assets/stylesheets/base_editing_bootstrap/.keep
325
323
  - app/controllers/.keep
326
324
  - app/controllers/base_editing_controller.rb
327
- - app/controllers/concerns/.keep
328
325
  - app/controllers/restricted_area_controller.rb
329
326
  - app/helpers/.keep
330
327
  - app/helpers/base_editing_helper.rb
@@ -334,10 +331,6 @@ files:
334
331
  - app/helpers/utilities/page_helper.rb
335
332
  - app/helpers/utilities/search_helper.rb
336
333
  - app/helpers/utilities/template_helper.rb
337
- - app/jobs/.keep
338
- - app/mailers/.keep
339
- - app/models/.keep
340
- - app/models/concerns/.keep
341
334
  - app/policies/base_model_policy.rb
342
335
  - app/views/.keep
343
336
  - app/views/base_editing/_edit_page_title_header.html.erb
@@ -363,9 +356,11 @@ files:
363
356
  - app/views/base_editing/_tabs.html.erb
364
357
  - app/views/base_editing/cell_field/_base.html.erb
365
358
  - app/views/base_editing/cell_field/_boolean.html.erb
359
+ - app/views/base_editing/cell_field/_enum.html.erb
366
360
  - app/views/base_editing/cell_field/_timestamps.html.erb
367
361
  - app/views/base_editing/edit.html.erb
368
362
  - app/views/base_editing/form_field/_base.html.erb
363
+ - app/views/base_editing/form_field/_belongs_to_select.html.erb
369
364
  - app/views/base_editing/form_field/_boolean.html.erb
370
365
  - app/views/base_editing/form_field/_date.html.erb
371
366
  - app/views/base_editing/form_field/_datetime.html.erb
File without changes
File without changes
File without changes
data/app/jobs/.keep DELETED
File without changes
data/app/mailers/.keep DELETED
File without changes
data/app/models/.keep DELETED
File without changes
File without changes