eac_rails_utils 0.11.6 → 0.12.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: 4d59ee76de9d16dac158bf6ffd8f4a1e1c4efa20dd1142ed228af225b39551ab
4
- data.tar.gz: 1c085391870e824b1f0ba969b0edc199a5020d32ba7bc101fe41dc9370780876
3
+ metadata.gz: 00fd5aed91f9c11d3b6ab34cd532aff0c5ed10da72ba420488a060f93bbd8540
4
+ data.tar.gz: f66267fb4600635f05bcb15475fe5186c31663e9db4a00494add6e59dc603578
5
5
  SHA512:
6
- metadata.gz: c3336f477cde2ed2d42fd8fdf07166626479279719d3e17e205bff26631c0c457d2e36b802554954bf66268a9d27c455ab7c53fd144933585fbea63c3100700c
7
- data.tar.gz: 3a7cd6b2a2ea0e1ed4814f5fc920d34d234aa56cfeb86efecc09482fa63532627ff6197025aead13eedc5ba7e87cb0e8fbe28a64a6a29894c63947bc5bed269b
6
+ metadata.gz: 9b6aa1e4febf6826c760daf62ad9c280b3b708beb00800644cffdd88021acdd69ef41262f5ff395616eefa63c240e214a0ef3eb1f4028a1fab5f5f5cfa382acd
7
+ data.tar.gz: 5f2a4f27e090663895222931a57bdb63eae96a4b679a95a544a47699e7c0a5e733c47968cf13fa3f628c3c90381d3796a8d54989b3f7f38a674cd94454f7936a
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/require_sub'
4
- require 'eac_rails_utils/models/attribute_required'
4
+ require 'eac_rails_utils/models/validations'
5
5
 
6
6
  module EacRailsUtils
7
7
  module CommonFormHelper
@@ -93,8 +93,8 @@ module EacRailsUtils
93
93
 
94
94
  def field_label(field_name, label, required)
95
95
  if required.nil?
96
- required = ::EacRailsUtils::Models::AttributeRequired
97
- .required?(model_instance, field_name)
96
+ required = ::EacRailsUtils::Models::Validations
97
+ .column_required?(model_instance, field_name)
98
98
  end
99
99
  @form.label(field_name, label, class: required ? 'required' : 'optional')
100
100
  end
@@ -10,6 +10,16 @@ module EacRailsUtils
10
10
  include Virtus.model
11
11
  include ActiveModel::Associations
12
12
 
13
+ class << self
14
+ def columns
15
+ attribute_set.each.to_a
16
+ end
17
+
18
+ def columns_names
19
+ columns.map(&:name)
20
+ end
21
+ end
22
+
13
23
  def initialize(values = {})
14
24
  super(build_attributes(values))
15
25
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsUtils
6
+ module Models
7
+ module Validations
8
+ common_concern
9
+
10
+ class << self
11
+ def column_errors(model_or_record, column, value)
12
+ model = model_class_or_record_to_model(model_or_record)
13
+ record = model.new
14
+ model.validators_on(column).each do |validator|
15
+ next if validator.options[:allow_blank] && value.blank?
16
+ next if validator.options[:allow_nil] && value.nil?
17
+
18
+ validator.validate_each(record, column, value)
19
+ end
20
+ record.errors
21
+ end
22
+
23
+ def column_required?(model_or_record, column)
24
+ !column_valid?(model_or_record, column, nil)
25
+ end
26
+
27
+ def column_valid?(model_or_record, column, value)
28
+ column_errors(model_or_record, column, value).empty?
29
+ end
30
+
31
+ private
32
+
33
+ def model_class_or_record_to_model(model_class_or_record)
34
+ model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
35
+ end
36
+ end
37
+
38
+ module ClassMethods
39
+ def column_errors(column, value)
40
+ ::EacRailsUtils::Models::Validations.column_errors(self, column, value)
41
+ end
42
+
43
+ def column_required?(column)
44
+ ::EacRailsUtils::Models::Validations.column_required?(self, column)
45
+ end
46
+
47
+ def column_valid?(attribute, value)
48
+ column_errors(attribute, value).empty?
49
+ end
50
+
51
+ private
52
+
53
+ def model_class_or_record_to_model(model_class_or_record)
54
+ model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
55
+ end
56
+ end
57
+
58
+ def attribute_errors(attribute)
59
+ self.class.column_errors(attribute, send(attribute))
60
+ end
61
+
62
+ def attribute_required?(attribute)
63
+ self.class.column_required?(attribute)
64
+ end
65
+
66
+ def attribute_valid?(attribute)
67
+ attribute_errors(attribute).empty?
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsUtils
4
- VERSION = '0.11.6'
4
+ VERSION = '0.12.0'
5
5
  end
@@ -1,10 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'eac_rails_utils/models/attribute_required'
4
-
5
3
  class User < ActiveRecord::Base
6
- include ::EacRailsUtils::Models::AttributeRequired
7
-
8
4
  belongs_to :job
9
5
 
10
6
  validates :job, presence: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rails_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.6
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - E.A.C.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-14 00:00:00.000000000 Z
11
+ date: 2020-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -44,20 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.33'
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: 0.33.1
47
+ version: '0.49'
51
48
  type: :runtime
52
49
  prerelease: false
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - "~>"
56
53
  - !ruby/object:Gem::Version
57
- version: '0.33'
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 0.33.1
54
+ version: '0.49'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: htmlbeautifier
63
57
  requirement: !ruby/object:Gem::Requirement
@@ -206,11 +200,11 @@ files:
206
200
  - lib/eac_rails_utils/engine.rb
207
201
  - lib/eac_rails_utils/htmlbeautifier.rb
208
202
  - lib/eac_rails_utils/models.rb
209
- - lib/eac_rails_utils/models/attribute_required.rb
210
203
  - lib/eac_rails_utils/models/fetch_errors.rb
211
204
  - lib/eac_rails_utils/models/inequality_queries.rb
212
205
  - lib/eac_rails_utils/models/tableless.rb
213
206
  - lib/eac_rails_utils/models/test_utils.rb
207
+ - lib/eac_rails_utils/models/validations.rb
214
208
  - lib/eac_rails_utils/patches.rb
215
209
  - lib/eac_rails_utils/patches/action_controller_base.rb
216
210
  - lib/eac_rails_utils/patches/active_model_associations.rb
@@ -235,9 +229,7 @@ files:
235
229
  - test/dummy/db/schema.rb
236
230
  - test/helpers/eac_rails_utils/data_table_test_helper_test.rb
237
231
  - test/helpers/eac_rails_utils/formatter_helper_test.rb
238
- - test/lib/eac_rails_utils/models/attribute_required_test.rb
239
232
  - test/lib/eac_rails_utils/models/fetch_errors_test.rb
240
- - test/lib/eac_rails_utils/models/tableless_test.rb
241
233
  - test/test_helper.rb
242
234
  - test/validators/cpf_validator_test.rb
243
235
  homepage:
@@ -285,6 +277,4 @@ test_files:
285
277
  - test/validators/cpf_validator_test.rb
286
278
  - test/helpers/eac_rails_utils/data_table_test_helper_test.rb
287
279
  - test/helpers/eac_rails_utils/formatter_helper_test.rb
288
- - test/lib/eac_rails_utils/models/attribute_required_test.rb
289
- - test/lib/eac_rails_utils/models/tableless_test.rb
290
280
  - test/lib/eac_rails_utils/models/fetch_errors_test.rb
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/concern'
4
-
5
- module EacRailsUtils
6
- module Models
7
- module AttributeRequired
8
- extend ::ActiveSupport::Concern
9
-
10
- class << self
11
- def required?(model_class_or_record, attribute)
12
- model = model_class_or_record_to_model(model_class_or_record)
13
- record = model.new
14
- record.validate
15
- record.errors.key?(attribute.to_sym)
16
- end
17
-
18
- private
19
-
20
- def model_class_or_record_to_model(model_class_or_record)
21
- model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
22
- end
23
- end
24
-
25
- included do
26
- extend ClassMethods
27
- include InstanceMethods
28
- end
29
-
30
- module ClassMethods
31
- def column_required?(column)
32
- ::EacRailsUtils::Models::AttributeRequired.required?(self, column)
33
- end
34
- end
35
-
36
- module InstanceMethods
37
- def attribute_required?(column)
38
- self.class.column_required?(column)
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module EacRailsUtils
6
- module Patches
7
- class ModelAttributeRequiredTest < ActiveSupport::TestCase
8
- setup do
9
- reset_test_database
10
- end
11
-
12
- test 'column with presence validator should be required' do
13
- assert User.column_required?(:password)
14
- end
15
-
16
- test 'column with format validator should be required' do
17
- assert User.column_required?(:email)
18
- end
19
-
20
- test 'column without validators should be optional' do
21
- assert_not User.column_required?(:name)
22
- end
23
-
24
- test 'association with presence validator should be required' do
25
- assert User.column_required?(:job)
26
- end
27
-
28
- test 'required column in active model' do
29
- assert ActiveModelStub.column_required?(:name), 'name is required'
30
- assert_not ActiveModelStub.column_required?(:age), 'age is optional'
31
- end
32
-
33
- class ActiveModelStub
34
- include ActiveModel::Model
35
- include ::EacRailsUtils::Models::AttributeRequired
36
-
37
- attr_accessor :name, :age
38
- validates :name, presence: true
39
- end
40
- end
41
- end
42
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module EacRailsUtils
6
- module Models
7
- class TablelessTest < ActiveSupport::TestCase
8
- class Stub < ::EacRailsUtils::Models::Tableless
9
- attribute :tempo, DateTime
10
- end
11
-
12
- test 'date time array values' do
13
- stub = Stub.new('tempo(1i)' => '9', 'tempo(2i)' => '10', 'tempo(3i)' => '11',
14
- 'tempo(4i)' => '12', 'tempo(5i)' => '13', 'tempo(6i)' => '14')
15
- assert stub.tempo.is_a?(DateTime), "Class: |#{stub.tempo.class}|, Value: |#{stub.tempo}|"
16
- assert_equal 9, stub.tempo.year, 'Year'
17
- assert_equal 10, stub.tempo.month, 'Month'
18
- assert_equal 11, stub.tempo.day, 'Day'
19
- assert_equal 12, stub.tempo.hour, 'Hour'
20
- assert_equal 13, stub.tempo.minute, 'Minute'
21
- assert_equal 14, stub.tempo.second, 'Second'
22
- end
23
- end
24
- end
25
- end