eac_rails_utils 0.14.0 → 0.15.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: 5c4f6d085f9a7e1f6b39c4028498cac28f482540993e73bdadff0b58e6f1e214
4
- data.tar.gz: d369558ea68714a4d0dedb89b3af900945be5c57113da0241b5332e818f634d8
3
+ metadata.gz: c466724b885abae859a74f33568b35eb62d9488f55ef7e5a9f980b43117982e5
4
+ data.tar.gz: 77bc820f51b8ee863b08a7b81ca8feb78b488a3917841ee2a5582956c7c76088
5
5
  SHA512:
6
- metadata.gz: 591317f786a1edac276ac1ce0ecea5dcacbdfdbbd5635b51cd8e064c45ea921e4b8819bf78b49be0e708d13843843b896ab4e33d8f034e1146cb6584e0a9c798
7
- data.tar.gz: 6f9760a2990de21e42b779f22d38aff09fa83b722959f551aff3bace7b9eef7f0db9cb8acb617bd3a5d4f9935e95090546c64de317cd8d68d4503b2d0e11989f
6
+ metadata.gz: 4f7b4fd5913f32cad671bbfe269f4316137e15f55f349f06d88f7efb2879ff4bc03b450fec87f0dd15d396f97ef972199c3edc5666b68b4a8a49ffd8a6c9bc00
7
+ data.tar.gz: f6a3bf43950216bb98f48509f9f451d23e33545e6b7d40ca73e8481ffa8cb6ffb4fc95136ea13f0c00ab4240096b06df579adf49560ded72d033ad159d662959
@@ -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,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__
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsUtils
4
- VERSION = '0.14.0'
4
+ VERSION = '0.15.1'
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.14.0
4
+ version: 0.15.1
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-10-24 00:00:00.000000000 Z
11
+ date: 2022-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -121,6 +121,9 @@ dependencies:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0.4'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 0.4.1
124
127
  type: :development
125
128
  prerelease: false
126
129
  version_requirements: !ruby/object:Gem::Requirement
@@ -128,6 +131,9 @@ dependencies:
128
131
  - - "~>"
129
132
  - !ruby/object:Gem::Version
130
133
  version: '0.4'
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 0.4.1
131
137
  description:
132
138
  email:
133
139
  executables: []
@@ -196,6 +202,8 @@ files:
196
202
  - lib/eac_rails_utils/patches.rb
197
203
  - lib/eac_rails_utils/patches/action_controller_base.rb
198
204
  - lib/eac_rails_utils/patches/active_model_associations.rb
205
+ - lib/eac_rails_utils/patches/numeric.rb
206
+ - lib/eac_rails_utils/patches/numeric/number_helper.rb
199
207
  - lib/eac_rails_utils/patches/rails_4.rb
200
208
  - lib/eac_rails_utils/patches/rails_4/active_record_associations_association_scope.rb
201
209
  - lib/eac_rails_utils/patches/rails_5_2.rb