pg_rails 7.0.8.pre.alpha.8 → 7.0.8.pre.alpha.9

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: 8b3571f15d522de34ce58f735dd40b7dd3b44bc2f8292233dbcc5e451dd49f7a
4
- data.tar.gz: 315e2de0d4c61085eaf12fb48bc2298d928e1909c2546a9bfda57e8e3e9c88ed
3
+ metadata.gz: 55efac7692668130210916f2d27c9d5f649660dc6a7341ea615fcfe66b263909
4
+ data.tar.gz: 1827e613adbfbaed350d8d84f69131c302a14ef9b7f7cef0c2fc030171b13261
5
5
  SHA512:
6
- metadata.gz: 305ac09bdf7ac2a6172f71979253e8dbf90123d5c4218d5ffda7ab7a035052271ea9c6c9f3f182a7b76064075294ba2e71e3b157b49461753b954a762f9d1de4
7
- data.tar.gz: 7d4744860de97968cd3e67710bd859a64e4610dcca69439daba8a05422fd6b8d092fbf56ef55eddee79aacc9a43154d7545b481bde7060c492694bdefebfb843
6
+ metadata.gz: ca7acd8b6732ba7e5fb832b443a32d74d12490d013d1c0a72ed7bfbd499ce959f458815c22a980f9b94f81cdbddfa4e024e4b8de5533ab0f637ec7ed6c1de735
7
+ data.tar.gz: 4335852cf4c82da4e90504ed5872472f5612378fb92b674bb3da4185c26fb040e81b55522bcf67dfe71db81c36fec146c112f4ff14bbc02832eb01dc481a03ee
data/README.md CHANGED
@@ -89,3 +89,5 @@ be rails pg_engine_engine:install:migrations
89
89
 
90
90
  ruby-vips
91
91
  pdftoppm
92
+
93
+ atributos no pueden terminar en _text
@@ -1,5 +1,5 @@
1
1
  module PgEngine
2
- module ResourceHelper
2
+ module Resource
3
3
  def self.included(clazz)
4
4
  clazz.before_action :authenticate_user!
5
5
  clazz.helper_method :atributos_para_listar
@@ -98,11 +98,12 @@ module PgEngine
98
98
  'No'
99
99
  end
100
100
 
101
- def print_currency(number, moneda = 'pesos')
101
+ def print_currency(number, simbolo: '$')
102
102
  return if number.blank?
103
103
 
104
- "#{simbolo_moneda(moneda)} #{number_with_precision(number, delimiter: '.', separator: ',',
105
- precision: 2)}"
104
+ precision = (number % 1).positive? ? 2 : 0
105
+ "#{simbolo} #{number_with_precision(number, delimiter: '.', separator: ',',
106
+ precision:)}"
106
107
  end
107
108
 
108
109
  def simbolo_moneda(moneda)
@@ -0,0 +1,37 @@
1
+ module PgEngine
2
+ module ErrorHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ def merge_association_errors(object, details, assoc_key)
6
+ details = details.except(assoc_key)
7
+ assoc_items = object.send(assoc_key).map(&:errors).map(&:details)
8
+ merged = assoc_items.inject({}) { |acc, el| acc.merge(el) }
9
+ merged = merged.transform_values { |errs| errs.pluck(:error) }
10
+ details.merge(merged)
11
+ end
12
+
13
+ def error_types(object, associations: [])
14
+ details = object.errors.details.transform_values do |errs|
15
+ errs.pluck(:error)
16
+ end
17
+ associations.each do |assoc_key|
18
+ next unless details.key? assoc_key
19
+
20
+ details = merge_association_errors(object, details, assoc_key)
21
+ end
22
+ details.values.flatten.uniq
23
+ end
24
+
25
+ def error_message_for(object, associations: [])
26
+ types = error_types(object, associations:)
27
+
28
+ if types == [:blank]
29
+ :only_presence_errors
30
+ elsif types.include? :blank
31
+ :multiple_error_types
32
+ elsif types.present?
33
+ :not_presence_errors
34
+ end
35
+ end
36
+ end
37
+ end
@@ -2,12 +2,27 @@
2
2
 
3
3
  class PgFormBuilder < SimpleForm::FormBuilder
4
4
  include PgAssociable::FormBuilderMethods
5
+ include PgEngine::ErrorHelper
5
6
 
6
7
  def mensajes_de_error
7
- errors_to_show = object.errors[:base]
8
- message = (errors_to_show.map(&:to_s).join('<br>') if errors_to_show.present?)
8
+ base_errors = object.errors[:base]
9
+ base_message = (base_errors.map(&:to_s).join('<br>') if base_errors.present?)
10
+ title = error_notification(message: mensaje, class: 'text-danger mb-2') if mensaje
11
+ base_tag = error_notification(message: base_message, class: 'alert alert-danger') if base_message
12
+ (title || '') + (base_tag || '')
13
+ end
14
+
15
+ def mensaje
16
+ scope = error_message_for(object, associations:)
17
+ return if scope.blank?
18
+
19
+ I18n.t("simple_form.error_notification.#{scope}")
20
+ end
9
21
 
10
- error_notification(message:)
22
+ def associations
23
+ object.class.reflect_on_all_associations
24
+ .select { |a| a.instance_of? ActiveRecord::Reflection::HasManyReflection }
25
+ .map(&:name)
11
26
  end
12
27
 
13
28
  map_type :date, to: PgEngine::FechaInput
@@ -33,6 +33,15 @@ module PgEngine
33
33
  model_name.human(count: 1)
34
34
  end
35
35
 
36
+ def self.human_attribute_name(attribute, options = {})
37
+ # Si es un enumerized
38
+ if attribute.to_s.ends_with?('_text')
39
+ super(attribute[0..-6], options)
40
+ else
41
+ super(attribute, options)
42
+ end
43
+ end
44
+
36
45
  def to_s
37
46
  %i[nombre name].each do |campo|
38
47
  return "#{send(campo)} ##{id}" if try(campo).present?
@@ -7,15 +7,6 @@ es:
7
7
  creado_por: Creado por
8
8
  ancestry:
9
9
  exclude_self: No puede ser hijx de si mismx
10
- simple_form:
11
- # labels:
12
- # user:
13
- # username: 'User name'
14
- # password: 'Password'
15
- # hints:
16
- error_notification:
17
- default_message: "hubo algunos errores:"
18
-
19
10
  simple_form:
20
11
  "yes": 'Sí'
21
12
  "no": 'No'
@@ -26,7 +17,10 @@ es:
26
17
  # When using html, text and mark won't be used.
27
18
  # html: '<abbr title="required">*</abbr>'
28
19
  error_notification:
29
- default_message: "Por favor, revisá los errores debajo:"
20
+ default_message: "Por favor, revisá los siguientes errores:"
21
+ not_presence_errors: "Por favor, revisá los siguientes errores:"
22
+ only_presence_errors: 'Por favor, revisá los campos obligatorios:'
23
+ multiple_error_types: 'Por favor, revisá los campos obligatorios y otros errores:'
30
24
  # Examples
31
25
  # labels:
32
26
  # defaults:
@@ -32,7 +32,7 @@ SimpleForm.setup do |config|
32
32
  config.include_default_input_wrapper_class = false
33
33
 
34
34
  # CSS class to add for error notification helper.
35
- config.error_notification_class = 'alert alert-danger'
35
+ config.error_notification_class = ''
36
36
 
37
37
  # Method used to tidy up errors. Specify any Rails Array method.
38
38
  # :first lists the first message for each field.
@@ -57,6 +57,7 @@ SimpleForm.setup do |config|
57
57
  b.use :label, class: 'form-label'
58
58
  b.use :input, class: 'form-control', error_class: 'is-invalid'
59
59
  b.use :full_error, wrap_with: { class: 'invalid-feedback' }
60
+ b.optional :error, wrap_with: { class: 'invalid-feedback' }
60
61
  b.use :hint, wrap_with: { class: 'form-text' }
61
62
  end
62
63
 
@@ -66,6 +67,7 @@ SimpleForm.setup do |config|
66
67
  b.use :label, class: 'form-label'
67
68
  b.use :input, class: 'form-select', error_class: 'is-invalid'
68
69
  b.use :full_error, wrap_with: { class: 'invalid-feedback' }
70
+ b.optional :error, wrap_with: { class: 'invalid-feedback' }
69
71
  b.use :hint, wrap_with: { class: 'form-text' }
70
72
  end
71
73
 
@@ -47,16 +47,20 @@ module PgEngine
47
47
  initializer 'byebug_bullet' do
48
48
  if Rails.env.local?
49
49
  # Byebug
50
- require 'byebug/core'
51
- begin
52
- Byebug.start_server 'localhost', ENV.fetch('BYEBUG_SERVER_PORT', 8989).to_i
53
- if ENV.fetch('SLEEP_AFTER_BYEBUG', false)
54
- puts 'waiting 3 secs after starting byebug server for connections'
55
- sleep 3
50
+ # :nocov:
51
+ if ENV['RUBY_DEBUG_OPEN']
52
+ require 'byebug/core'
53
+ begin
54
+ Byebug.start_server 'localhost', ENV.fetch('BYEBUG_SERVER_PORT', 8989).to_i
55
+ if ENV.fetch('SLEEP_AFTER_BYEBUG', false)
56
+ puts 'waiting 3 secs after starting byebug server for connections'
57
+ sleep 3
58
+ end
59
+ rescue Errno::EADDRINUSE
60
+ Rails.logger.debug 'Byebug server already running'
56
61
  end
57
- rescue Errno::EADDRINUSE
58
- Rails.logger.debug 'Byebug server already running'
59
62
  end
63
+ # :nocov:
60
64
 
61
65
  # Bullet
62
66
  Bullet.enable = true
@@ -19,8 +19,8 @@ if Rails.env.development?
19
19
  'position_in_serializer' => 'before',
20
20
  'show_foreign_keys' => 'true',
21
21
  'show_complete_foreign_keys' => 'false',
22
- 'show_indexes' => 'true',
23
- 'simple_indexes' => 'false',
22
+ 'show_indexes' => 'false',
23
+ 'simple_indexes' => 'true',
24
24
  'model_dir' => 'app/models',
25
25
  'root_dir' => '',
26
26
  'include_version' => 'false',
@@ -0,0 +1,62 @@
1
+ require 'rails_helper'
2
+
3
+ describe PgEngine::ErrorHelper do
4
+ let(:dummy) { Class.new { include PgEngine::ErrorHelper }.new }
5
+
6
+ describe '#error_message_for' do
7
+ subject do
8
+ categoria.validate
9
+ dummy.error_message_for(categoria, associations: [:cosas])
10
+ end
11
+
12
+ let!(:categoria) { create :categoria_de_cosa }
13
+
14
+ before { create_list :cosa, 2, categoria_de_cosa: categoria }
15
+
16
+ it do
17
+ expect(subject).to be_nil
18
+ end
19
+
20
+ context 'cuando solo tiene errores de presencia' do
21
+ before do
22
+ categoria.nombre = nil
23
+ end
24
+
25
+ it do
26
+ expect(subject).to eq :only_presence_errors
27
+ end
28
+ end
29
+
30
+ context 'cuando solo tiene otros errores' do
31
+ before do
32
+ categoria.validate_aux = true
33
+ end
34
+
35
+ it do
36
+ expect(subject).to eq :not_presence_errors
37
+ end
38
+ end
39
+
40
+ context 'cuando tiene multiples errores' do
41
+ before do
42
+ categoria.validate_aux = true
43
+ categoria.nombre = nil
44
+ end
45
+
46
+ it do
47
+ expect(subject).to eq :multiple_error_types
48
+ end
49
+ end
50
+
51
+ context 'cuando tiene errores anidados' do
52
+ before do
53
+ categoria.reload
54
+ categoria.cosas[0].nombre = nil
55
+ end
56
+
57
+ it do
58
+ expect(subject).to eq :only_presence_errors
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails_helper'
2
+
3
+ describe PgFormBuilder do
4
+ let(:categoria) { create :categoria_de_cosa }
5
+ let(:template) { double }
6
+ let(:instancia) { described_class.new('bla', categoria, template, {}) }
7
+
8
+ before { create_list :cosa, 2, categoria_de_cosa: categoria }
9
+
10
+ describe '#mensaje' do
11
+ subject { instancia.mensaje }
12
+
13
+ context 'cuando solo tiene errores de presencia' do
14
+ before do
15
+ categoria.nombre = nil
16
+ categoria.validate
17
+ end
18
+
19
+ it { expect(subject).to eq 'Por favor, revisá los campos obligatorios:' }
20
+ end
21
+
22
+ context 'cuando solo tiene errores de presencia en nested' do
23
+ before do
24
+ categoria.reload
25
+ categoria.cosas[0].nombre = nil
26
+ categoria.validate
27
+ end
28
+
29
+ it { expect(subject).to eq 'Por favor, revisá los campos obligatorios:' }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+
3
+ describe PgEngine::BaseRecord do
4
+ describe '#human_attribute_name' do
5
+ subject do
6
+ described_class.human_attribute_name('bla_text')
7
+ end
8
+
9
+ it do
10
+ expect(subject).to eq described_class.human_attribute_name('bla')
11
+ end
12
+ end
13
+ end
@@ -2,33 +2,15 @@ export function round (value) {
2
2
  return Math.round(value * 100) / 100
3
3
  }
4
4
 
5
- export function printCurrency (value, moneda) {
6
- return monedaSimbolo(moneda) + numberWithDots(round(value).toFixed(2).replace('.', ','))
5
+ export function printCurrency (value, simboloMoneda = '$') {
6
+ const decimals = (value % 1 > 0) ? 2 : 0
7
+ return simboloMoneda + ' ' + numberWithDots(value.toFixed(decimals).replace('.', ','))
7
8
  }
8
9
 
9
10
  export function showPercentage (value) {
10
11
  return '% ' + value
11
12
  }
12
13
 
13
- export function monedaSimbolo (moneda) {
14
- switch (moneda) {
15
- case 'pesos':
16
- return '$ '
17
- case 'dolares':
18
- return 'U$S '
19
- case 'euros':
20
- return '€ '
21
- case 'reales':
22
- return 'R$ '
23
- case 'pesos_chilenos':
24
- return 'CLP '
25
- case 'pesos_mexicanos':
26
- return 'MXN '
27
- default:
28
- return '$ '
29
- }
30
- }
31
-
32
14
  export function numberWithDots (x) {
33
15
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
34
16
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRails
4
- VERSION = '7.0.8-alpha.8'
4
+ VERSION = '7.0.8-alpha.9'
5
5
  end
@@ -1,7 +1,7 @@
1
1
  / # locals: (object: nil, asociable: false)
2
2
 
3
3
  div style="max-width: 22em" data-controller="pg_form"
4
- = pg_form_for(@<%= singular_name %> || object, asociable:)) do |f|
4
+ = pg_form_for(@<%= singular_name %> || object, asociable:) do |f|
5
5
  = f.mensajes_de_error
6
6
 
7
7
  = hidden_field_tag :asociable, true if asociable
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.8.pre.alpha.8
4
+ version: 7.0.8.pre.alpha.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martín Rosso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-28 00:00:00.000000000 Z
11
+ date: 2024-03-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Rails goodies.
14
14
  email:
@@ -42,10 +42,10 @@ files:
42
42
  - pg_engine/app/controllers/admin/accounts_controller.rb
43
43
  - pg_engine/app/controllers/admin/user_accounts_controller.rb
44
44
  - pg_engine/app/controllers/admin/users_controller.rb
45
+ - pg_engine/app/controllers/concerns/pg_engine/resource.rb
45
46
  - pg_engine/app/controllers/pg_engine/base_controller.rb
46
47
  - pg_engine/app/controllers/pg_engine/devise_controller.rb
47
48
  - pg_engine/app/controllers/pg_engine/require_sign_in.rb
48
- - pg_engine/app/controllers/pg_engine/resource_helper.rb
49
49
  - pg_engine/app/decorators/account_decorator.rb
50
50
  - pg_engine/app/decorators/pg_engine/base_decorator.rb
51
51
  - pg_engine/app/decorators/user_account_decorator.rb
@@ -57,6 +57,7 @@ files:
57
57
  - pg_engine/app/helpers/pg_engine/print_helper.rb
58
58
  - pg_engine/app/helpers/pg_engine/route_helper.rb
59
59
  - pg_engine/app/inputs/pg_engine/fecha_input.rb
60
+ - pg_engine/app/lib/pg_engine/error_helper.rb
60
61
  - pg_engine/app/lib/pg_engine/filtros_builder.rb
61
62
  - pg_engine/app/lib/pg_form_builder.rb
62
63
  - pg_engine/app/models/account.rb
@@ -110,11 +111,14 @@ files:
110
111
  - pg_engine/spec/controllers/admin/accounts_controller_spec.rb
111
112
  - pg_engine/spec/controllers/admin/user_accounts_controller_spec.rb
112
113
  - pg_engine/spec/controllers/admin/users_controller_spec.rb
114
+ - pg_engine/spec/controllers/concerns/pg_engine/error_helper_spec.rb
113
115
  - pg_engine/spec/factories/accounts.rb
114
116
  - pg_engine/spec/factories/user_accounts.rb
115
117
  - pg_engine/spec/factories/users.rb
116
118
  - pg_engine/spec/fixtures/test.pdf
119
+ - pg_engine/spec/lib/pg_form_builder_spec.rb
117
120
  - pg_engine/spec/models/account_spec.rb
121
+ - pg_engine/spec/models/pg_engine/base_record_spec.rb
118
122
  - pg_engine/spec/models/user_account_spec.rb
119
123
  - pg_engine/spec/models/user_spec.rb
120
124
  - pg_engine/spec/pg_engine/pdf_preview_generator_spec.rb