eac_rails_utils 0.13.3 → 0.14.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 +4 -4
- data/app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb +45 -18
- data/app/helpers/eac_rails_utils/common_form_helper/form_builder.rb +5 -1
- data/config/initializers/json.rb +20 -0
- data/lib/eac_rails_utils/patches/active_model_associations.rb +23 -27
- data/lib/eac_rails_utils/patches/rails_4/active_record_associations_association_scope.rb +23 -0
- data/lib/eac_rails_utils/patches/rails_4.rb +18 -0
- data/lib/eac_rails_utils/patches/rails_5_2/active_model_association_method_fix.rb +38 -0
- data/lib/eac_rails_utils/patches/rails_5_2.rb +18 -0
- data/lib/eac_rails_utils/rspec/setup/models_utils.rb +40 -0
- data/lib/eac_rails_utils/rspec/setup.rb +15 -0
- data/lib/eac_rails_utils/rspec.rb +8 -0
- data/lib/eac_rails_utils/version.rb +1 -1
- metadata +12 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7137f7331f9f361c5371306a1c2c607495dd3897bef11c887aad02535814c312
|
4
|
+
data.tar.gz: b226cce4f9f3dc400a97426fa13a0d00d5fa2d157fd48bbda07873218832bd45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51cab365aec88b22c1a4765e3537f8b661eff6e06290210b5cd79e4d18f2a07a8de3c7ee059eb5b0809e71359c9c855cbee406f0d94c7a8381de78f45dbdebeb
|
7
|
+
data.tar.gz: b2e42d61fbf1ed64f5297a7762e0bde11522682c12d02e4d9ca864d3000def5d037d512134bbae2ad9e998f4a3567f5326fb81db29e3fe5a9870b1f1926266ce
|
data/app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
CHANGED
@@ -3,22 +3,51 @@
|
|
3
3
|
module EacRailsUtils
|
4
4
|
module CommonFormHelper
|
5
5
|
class FormBuilder
|
6
|
-
|
7
|
-
|
8
|
-
options = options.dup
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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(
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
@@ -1,38 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'active_model/associations/hooks'
|
4
3
|
require 'activemodel/associations'
|
5
|
-
|
6
|
-
|
7
|
-
module Patches
|
8
|
-
module ActiveModelAssociations
|
9
|
-
module ScopeExtensionPatch
|
10
|
-
def add_constraints(scope, owner, association_klass, *extra_args)
|
11
|
-
if extra_args.any?
|
12
|
-
refl = extra_args.first
|
13
|
-
if refl.options[:active_model]
|
14
|
-
target_ids = refl.options[:target_ids]
|
15
|
-
return scope.where(id: owner[target_ids])
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
super
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
4
|
+
require 'eac_rails_utils/patches/rails_4'
|
5
|
+
require 'eac_rails_utils/patches/rails_5_2'
|
25
6
|
|
26
7
|
module ActiveModel
|
27
8
|
module Associations
|
28
9
|
module Hooks
|
29
|
-
|
30
|
-
|
10
|
+
class << self
|
11
|
+
def init
|
12
|
+
init_rails_4 if ::EacRailsUtils::Patches::Rails4.enabled?
|
13
|
+
init_rails_5_2 if ::EacRailsUtils::Patches::Rails52.enabled?
|
14
|
+
end
|
15
|
+
|
16
|
+
def init_rails_4
|
17
|
+
ActiveSupport.on_load(:active_record) do
|
18
|
+
ActiveRecord::Associations::AssociationScope.prepend(
|
19
|
+
::EacRailsUtils::Patches::Rails4::ActiveRecordAssociationsAssociationScope
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
31
23
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
def init_rails_5_2
|
25
|
+
rails_5_2_fix_activemodel_associations_methods
|
26
|
+
end
|
27
|
+
|
28
|
+
def rails_5_2_fix_activemodel_associations_methods
|
29
|
+
%i[belongs_to has_many].each do |method|
|
30
|
+
::EacRailsUtils::Patches::Rails52::ActiveModelAssociationMethodFix.new(method)
|
31
|
+
end
|
36
32
|
end
|
37
33
|
end
|
38
34
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'activemodel/associations'
|
4
|
+
|
5
|
+
module EacRailsUtils
|
6
|
+
module Patches
|
7
|
+
module Rails4
|
8
|
+
module ActiveRecordAssociationsAssociationScope
|
9
|
+
def add_constraints(scope, owner, association_klass, *extra_args)
|
10
|
+
if extra_args.any?
|
11
|
+
refl = extra_args.first
|
12
|
+
if refl.options[:active_model]
|
13
|
+
target_ids = refl.options[:target_ids]
|
14
|
+
return scope.where(id: owner[target_ids])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/associations/hooks'
|
4
|
+
require 'eac_ruby_utils/require_sub'
|
5
|
+
|
6
|
+
module EacRailsUtils
|
7
|
+
module Patches
|
8
|
+
module Rails4
|
9
|
+
::EacRubyUtils.require_sub __FILE__
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def enabled?
|
13
|
+
::Rails.version < '5'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'activemodel/associations'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
|
6
|
+
module EacRailsUtils
|
7
|
+
module Patches
|
8
|
+
module Rails52
|
9
|
+
class ActiveModelAssociationMethodFix
|
10
|
+
common_constructor :method_name do
|
11
|
+
perform
|
12
|
+
end
|
13
|
+
|
14
|
+
def original_method_new_name
|
15
|
+
"original_#{method_name}".to_sym
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def the_module
|
21
|
+
::ActiveModel::Associations::ClassMethods
|
22
|
+
end
|
23
|
+
|
24
|
+
def perform
|
25
|
+
patch = self
|
26
|
+
the_module.class_eval do
|
27
|
+
alias_method patch.original_method_new_name, patch.method_name
|
28
|
+
remove_method patch.method_name
|
29
|
+
|
30
|
+
define_method patch.method_name do |name, scope = nil, **options, &extension|
|
31
|
+
send(patch.original_method_new_name, name, scope, options, &extension)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/associations/hooks'
|
4
|
+
require 'eac_ruby_utils/require_sub'
|
5
|
+
|
6
|
+
module EacRailsUtils
|
7
|
+
module Patches
|
8
|
+
module Rails52
|
9
|
+
::EacRubyUtils.require_sub __FILE__
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def enabled?
|
13
|
+
::Rails.version >= '5.2'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -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
|
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.
|
4
|
+
version: 0.14.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-
|
11
|
+
date: 2021-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel-associations
|
@@ -120,14 +120,14 @@ dependencies:
|
|
120
120
|
requirements:
|
121
121
|
- - "~>"
|
122
122
|
- !ruby/object:Gem::Version
|
123
|
-
version: '0.
|
123
|
+
version: '0.4'
|
124
124
|
type: :development
|
125
125
|
prerelease: false
|
126
126
|
version_requirements: !ruby/object:Gem::Requirement
|
127
127
|
requirements:
|
128
128
|
- - "~>"
|
129
129
|
- !ruby/object:Gem::Version
|
130
|
-
version: '0.
|
130
|
+
version: '0.4'
|
131
131
|
description:
|
132
132
|
email:
|
133
133
|
executables: []
|
@@ -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,6 +196,13 @@ 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/rails_4.rb
|
200
|
+
- lib/eac_rails_utils/patches/rails_4/active_record_associations_association_scope.rb
|
201
|
+
- lib/eac_rails_utils/patches/rails_5_2.rb
|
202
|
+
- lib/eac_rails_utils/patches/rails_5_2/active_model_association_method_fix.rb
|
203
|
+
- lib/eac_rails_utils/rspec.rb
|
204
|
+
- lib/eac_rails_utils/rspec/setup.rb
|
205
|
+
- lib/eac_rails_utils/rspec/setup/models_utils.rb
|
198
206
|
- lib/eac_rails_utils/version.rb
|
199
207
|
homepage:
|
200
208
|
licenses: []
|