eac_rails_utils 0.13.4 → 0.15.0

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: 843754871487598961a71d16871da8c3556d9d0097f0869838311d56c4e82df8
4
- data.tar.gz: e5a72c9640c9c6aba5f720bbb1204371c0f63ffdf5309f497e537b0a74406721
3
+ metadata.gz: 89426131c7ab950c53778513d6096be4495f7d6a5846fd42db9b8f31a0d69266
4
+ data.tar.gz: cbd2f6f94b7b83be5609e4e755250b64390897fb042d148efb5081d410eef41e
5
5
  SHA512:
6
- metadata.gz: eb890220eb07ca6a06e54a31d877779c043070b502d1a724236e5ea82c07839508487fe6effa5197d48928d1103eff5fd56d81dd8d54a3138a7c5404bf52057d
7
- data.tar.gz: 94d56786e02f5fd2c3d3d4a7b42cb36ea9d601bf1e89828a1670477f36ce7d36e8d164b3028c709bbdca8f7123aa8971cadd2d03861179e7b0cdde4cc70fe679
6
+ metadata.gz: da342b910c131c7c020ae55d89347f897378424a1aa1c6e82ca66ef5c98ba275261307c06ad4fb6aca08c5d467fde6d629f56400cb2ce79ba72e21bc006d33d3
7
+ data.tar.gz: dc082d3c3eafdd8fb0d040d703b0e4692ef110621777caae15150d5c2b6398538bfb7237c166b157ae38cbe9f5717a9aa942d9d942e824a704b38330f91e6d80
@@ -3,22 +3,51 @@
3
3
  module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
- module AssociationSelectField
7
- def association_select_field(field_name, options = {})
8
- options = options.dup
9
- methods = extract_methods(options)
10
- select_options = extract_select_options(options)
11
- collection = extract_association_key(field_name, options, :collection, :all)
12
- foreign_key = extract_association_key(field_name, options, :foreign_key,
13
- :association_foreign_key)
14
- field(field_name, options) do
15
- form.collection_select(foreign_key, collection, methods[:value], methods[:text],
16
- select_options, class: 'form-control')
6
+ class AssociationSelectField
7
+ common_constructor :builder, :field_name, :options, default: [{}] do
8
+ self.options = options.dup
9
+ end
10
+
11
+ delegate :model_instance, to: :builder
12
+
13
+ def collection
14
+ extract_association_key(:collection, *(
15
+ ::Rails.version < '5' ? [:all] : %i[klass all]
16
+ ))
17
+ end
18
+
19
+ def foreign_key
20
+ extract_association_key(:foreign_key, (
21
+ ::Rails.version < '5' ? :association_foreign_key : :join_foreign_key
22
+ ))
23
+ end
24
+
25
+ def methods
26
+ extract_methods(options)
27
+ end
28
+
29
+ def output
30
+ builder.send(:field, field_name, options) do
31
+ builder.form.collection_select(foreign_key, collection, methods[:value], methods[:text],
32
+ select_options, class: 'form-control')
17
33
  end
18
34
  end
19
35
 
36
+ def select_options
37
+ extract_select_options(options)
38
+ end
39
+
20
40
  private
21
41
 
42
+ def association_reflection
43
+ if model_instance.class.respond_to?(:reflect_on_association)
44
+ model_instance.class.reflect_on_association(field_name)
45
+ else
46
+ raise "#{model_instance.class} não possui um método \"reflect_on_association\". " \
47
+ "Defina explicitamente a opção :#{key}"
48
+ end
49
+ end
50
+
22
51
  def extract_methods(options)
23
52
  { value: options.delete(:value_method) || :id, text:
24
53
  options.delete(:text_method) || :to_s }
@@ -28,14 +57,12 @@ module EacRailsUtils
28
57
  options.extract!(:prompt, :include_blank)
29
58
  end
30
59
 
31
- def extract_association_key(field_name, options, key, method)
32
- return options.delete(key) if options.key?(key)
33
- if model_instance.class.respond_to?(:reflect_on_association)
34
- return model_instance.class.reflect_on_association(field_name).send(method)
60
+ def extract_association_key(key, *methods)
61
+ if options.key?(key)
62
+ options.fetch(key)
63
+ else
64
+ methods.inject(association_reflection) { |a, e| a.send(e) }
35
65
  end
36
-
37
- raise "#{model_instance.class} não possui um método \"reflect_on_association\". " \
38
- "Defina explicitamente a opção :#{key}"
39
66
  end
40
67
  end
41
68
  end
@@ -8,7 +8,6 @@ module EacRailsUtils
8
8
  class FormBuilder
9
9
  ::EacRubyUtils.require_sub __FILE__
10
10
 
11
- include AssociationSelectField
12
11
  include CommonTextFields
13
12
  include CurrencyField
14
13
  include DateField
@@ -27,6 +26,11 @@ module EacRailsUtils
27
26
  @field_errors_showed = Set.new
28
27
  end
29
28
 
29
+ def association_select_field(field_name, options = {})
30
+ ::EacRailsUtils::CommonFormHelper::FormBuilder::AssociationSelectField
31
+ .new(self, field_name, options).output
32
+ end
33
+
30
34
  def model_instance
31
35
  form.object
32
36
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ def patch_json?
4
+ require 'json'
5
+
6
+ ::Gem::Version.new(RUBY_VERSION) >= ::Gem::Version.new('2.7') &&
7
+ ::Gem::Version.new(JSON::VERSION) < ::Gem::Version.new('2')
8
+ rescue ::LoadError
9
+ false
10
+ end
11
+
12
+ if patch_json?
13
+ module JSON
14
+ module_function
15
+
16
+ def parse(source, opts = {})
17
+ Parser.new(source, **opts).parse
18
+ end
19
+ end
20
+ end
@@ -16,7 +16,7 @@ module ActiveModel
16
16
  def init_rails_4
17
17
  ActiveSupport.on_load(:active_record) do
18
18
  ActiveRecord::Associations::AssociationScope.prepend(
19
- ::EacRailsUtils::Patches::Rails4.ActiveRecordAssociationsAssociationScope
19
+ ::EacRailsUtils::Patches::Rails4::ActiveRecordAssociationsAssociationScope
20
20
  )
21
21
  end
22
22
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view/helpers/number_helper'
4
+
5
+ class Numeric
6
+ DEFAULT_PRECISION = 2
7
+
8
+ class << self
9
+ # @return [Object] A object that extends [ActionView::Helpers::NumberHelper]
10
+ def number_helper
11
+ @number_helper ||= begin
12
+ r = ::Object.new
13
+ r.extend(::ActionView::Helpers::NumberHelper)
14
+ r
15
+ end
16
+ end
17
+ end
18
+
19
+ def default_precision_options(options = {})
20
+ r = options.dup
21
+ r[:precision] = DEFAULT_PRECISION
22
+ r
23
+ end
24
+
25
+ # @return [Object] A object that extends [ActionView::Helpers::NumberHelper]
26
+ def number_helper
27
+ self.class.number_helper
28
+ end
29
+
30
+ # @return [String]
31
+ def to_currency(options = {})
32
+ number_helper.number_to_currency(self, default_precision_options(options))
33
+ end
34
+
35
+ # @return [String]
36
+ def to_human(options = {})
37
+ number_helper.number_to_human(self, default_precision_options(options))
38
+ end
39
+
40
+ # @return [String]
41
+ def to_human_size(options = {})
42
+ number_helper.number_to_human_size(self, default_precision_options(options))
43
+ end
44
+
45
+ # @return [String]
46
+ def to_percentage(options = {})
47
+ number_helper.number_to_percentage(self, default_precision_options(options))
48
+ end
49
+
50
+ # @return [String]
51
+ def to_phone(options = {})
52
+ number_helper.number_to_phone(self, options)
53
+ end
54
+
55
+ # @return [String]
56
+ def with_delimiter(options = {})
57
+ number_helper.number_with_delimiter(self, options)
58
+ end
59
+
60
+ # @return [String]
61
+ def with_precision(options = {})
62
+ number_helper.number_with_precision(self, default_precision_options(options).print_debug)
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub __FILE__
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsUtils
6
+ module Rspec
7
+ module Setup
8
+ module ModelsUtils
9
+ def model_record_attribute_test(record_variable, attribute, valid, value)
10
+ context("when #{record_variable}.#{attribute} == #{value}") do
11
+ before do
12
+ send(record_variable).send("#{attribute}=", value)
13
+ end
14
+
15
+ it "#{record_variable} should be {valid ? '' : 'not '}valid" do
16
+ expect(send(record_variable).valid?).to send("be_#{valid ? 'truthy' : 'falsy'}"),
17
+ send(record_variable).errors.messages
18
+ end
19
+ end
20
+ end
21
+
22
+ def model_record_values_attribute_test(record_variable, attribute, valid, values)
23
+ values.each do |value|
24
+ model_record_attribute_test(record_variable, attribute, valid, value)
25
+ end
26
+ end
27
+
28
+ def model_record_valid_invalid_values_attribute_test(record_variable, attribute,
29
+ valid_values, invalid_values)
30
+ {
31
+ false => invalid_values,
32
+ true => valid_values
33
+ }.each do |valid, values|
34
+ model_record_values_attribute_test(record_variable, attribute, valid, values)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsUtils
6
+ module Rspec
7
+ module Setup
8
+ require_sub __FILE__
9
+
10
+ def self.extended(setup_obj)
11
+ setup_obj.rspec_config.extend(::EacRailsUtils::Rspec::Setup::ModelsUtils)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRailsUtils
4
+ module Rspec
5
+ require 'eac_ruby_utils/require_sub'
6
+ ::EacRubyUtils.require_sub(__FILE__)
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsUtils
4
- VERSION = '0.13.4'
4
+ VERSION = '0.15.0'
5
5
  end
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.13.4
4
+ version: 0.15.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: 2021-09-05 00:00:00.000000000 Z
11
+ date: 2022-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -162,6 +162,7 @@ files:
162
162
  - app/validators/eac_rails_utils/cpf_validator.rb
163
163
  - app/validators/eac_rails_utils/no_presence_validator.rb
164
164
  - config/initializers/assets.rb
165
+ - config/initializers/json.rb
165
166
  - config/locales/en.yml
166
167
  - config/locales/pt-BR.yml
167
168
  - lib/assets/images/sort_asc.png
@@ -195,10 +196,15 @@ files:
195
196
  - lib/eac_rails_utils/patches.rb
196
197
  - lib/eac_rails_utils/patches/action_controller_base.rb
197
198
  - lib/eac_rails_utils/patches/active_model_associations.rb
199
+ - lib/eac_rails_utils/patches/numeric.rb
200
+ - lib/eac_rails_utils/patches/numeric/number_helper.rb
198
201
  - lib/eac_rails_utils/patches/rails_4.rb
199
202
  - lib/eac_rails_utils/patches/rails_4/active_record_associations_association_scope.rb
200
203
  - lib/eac_rails_utils/patches/rails_5_2.rb
201
204
  - lib/eac_rails_utils/patches/rails_5_2/active_model_association_method_fix.rb
205
+ - lib/eac_rails_utils/rspec.rb
206
+ - lib/eac_rails_utils/rspec/setup.rb
207
+ - lib/eac_rails_utils/rspec/setup/models_utils.rb
202
208
  - lib/eac_rails_utils/version.rb
203
209
  homepage:
204
210
  licenses: []