eac_rails_utils 0.22.0 → 0.22.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_rails_utils/models/tableless_associations/active_record_reflection.rb +34 -28
- data/lib/eac_rails_utils/models/tableless_associations/association_scope_extension.rb +23 -17
- data/lib/eac_rails_utils/models/tableless_associations/autosave_association.rb +13 -7
- data/lib/eac_rails_utils/models/tableless_associations/hooks.rb +14 -6
- data/lib/eac_rails_utils/models/tableless_associations/initialize_extension.rb +16 -10
- data/lib/eac_rails_utils/models/tableless_associations/override_methods.rb +98 -81
- data/lib/eac_rails_utils/models/tableless_associations/railtie.rb +10 -4
- data/lib/eac_rails_utils/models/tableless_associations.rb +7 -3
- data/lib/eac_rails_utils/version.rb +1 -1
- metadata +2 -3
- data/lib/eac_rails_utils/models/tableless_associations/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a88b255d31abb1059b16e75e33cb24b9ba2ad64a3d35afd63e27bc1d01dbbbe
|
4
|
+
data.tar.gz: b50b1587013cb361fe0eb2d7d56ee46dd5bdd87ed6aacf836aee7867ec7c152e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1249f9ba2a9598117e86de27b315fdd2f6f0f55330ee39916ea9e03f25c97b9c966a99a476628a1dfda677ea14b518cdf7478ad21eafc41868b4bcf4222fe65e
|
7
|
+
data.tar.gz: a7def8aa5c32a906ecae3e29b5c42ba669f962c368ad3ae01153de142ce74cc3f3da22f9090cc4d9d97fa359080ed414985be9879b8d3f42ec476e7b6f0dcae9
|
@@ -1,36 +1,42 @@
|
|
1
|
-
|
2
|
-
module ActiveRecordReflection
|
3
|
-
extend ActiveSupport::Concern
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
class_attribute :reflections
|
11
|
-
self.reflections = {}
|
12
|
-
end
|
13
|
-
end
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module ActiveRecordReflection
|
7
|
+
extend ActiveSupport::Concern
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
9
|
+
included do
|
10
|
+
if ActiveRecord.version >= Gem::Version.new('4.1.2')
|
11
|
+
class_attribute :_reflections
|
12
|
+
self._reflections = ActiveSupport::HashWithIndifferentAccess.new
|
13
|
+
else
|
14
|
+
class_attribute :reflections
|
15
|
+
self.reflections = {}
|
22
16
|
end
|
23
|
-
|
24
|
-
self.reflections = self.reflections.merge(name => reflection)
|
25
|
-
reflection
|
26
17
|
end
|
27
|
-
end
|
28
18
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
19
|
+
module ClassMethods
|
20
|
+
if ActiveRecord.version < Gem::Version.new('4.1')
|
21
|
+
def create_reflection(macro, name, scope, options, active_record)
|
22
|
+
case macro
|
23
|
+
when :has_many, :belongs_to
|
24
|
+
klass = ActiveRecord::Reflection::AssociationReflection
|
25
|
+
reflection = klass.new(macro, name, scope, options, active_record)
|
26
|
+
end
|
27
|
+
|
28
|
+
self.reflections = reflections.merge(name => reflection)
|
29
|
+
reflection
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def reflect_on_association(association)
|
34
|
+
if ActiveRecord.version >= Gem::Version.new('4.1.2')
|
35
|
+
_reflections[association.to_s]
|
36
|
+
else
|
37
|
+
reflections[association]
|
38
|
+
end
|
39
|
+
end
|
34
40
|
end
|
35
41
|
end
|
36
42
|
end
|
@@ -1,22 +1,28 @@
|
|
1
|
-
|
2
|
-
module AssociationScopeExtension
|
3
|
-
if ActiveRecord.version >= Gem::Version.new("5.0.0.beta")
|
4
|
-
def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tail)
|
5
|
-
if refl.options[:active_model]
|
6
|
-
target_ids = refl.options[:target_ids]
|
7
|
-
return scope.where(id: owner[target_ids])
|
8
|
-
end
|
1
|
+
# frozen_string_literal: true
|
9
2
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
if
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module AssociationScopeExtension
|
7
|
+
if ActiveRecord.version >= Gem::Version.new('5.0.0.beta')
|
8
|
+
def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tail) # rubocop:disable Metrics/ParameterLists
|
9
|
+
if refl.options[:active_model]
|
10
|
+
target_ids = refl.options[:target_ids]
|
11
|
+
return scope.where(id: owner[target_ids])
|
12
|
+
end
|
18
13
|
|
19
|
-
|
14
|
+
super
|
15
|
+
end
|
16
|
+
else
|
17
|
+
def add_constraints(scope, owner, assoc_klass, refl, tracker)
|
18
|
+
if refl.options[:active_model]
|
19
|
+
target_ids = refl.options[:target_ids]
|
20
|
+
return scope.where(id: owner[target_ids])
|
21
|
+
end
|
22
|
+
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -1,12 +1,18 @@
|
|
1
|
-
|
2
|
-
module AutosaveAssociation
|
3
|
-
extend ActiveSupport::Concern
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module AutosaveAssociation
|
7
|
+
extend ActiveSupport::Concern
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
include ActiveRecord::AutosaveAssociation
|
10
|
+
|
11
|
+
included do
|
12
|
+
extend ActiveModel::Callbacks
|
13
|
+
define_model_callbacks :save, :create, :update
|
14
|
+
end
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
12
18
|
end
|
@@ -1,9 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module Hooks
|
7
|
+
def self.init
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
require 'eac_rails_utils/models/tableless_associations/association_scope_extension'
|
10
|
+
ActiveRecord::Associations::AssociationScope.prepend(
|
11
|
+
EacRailsUtils::Models::TablelessAssociations::AssociationScopeExtension
|
12
|
+
)
|
13
|
+
end
|
14
|
+
end
|
7
15
|
end
|
8
16
|
end
|
9
17
|
end
|
@@ -1,15 +1,21 @@
|
|
1
|
-
|
2
|
-
module InitializeExtension
|
3
|
-
extend ActiveSupport::Concern
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module InitializeExtension
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
prepend WithAssociationCache
|
11
|
+
end
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
module WithAssociationCache
|
14
|
+
def initialize(*args)
|
15
|
+
@association_cache = {}
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
@@ -1,111 +1,128 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
module OverrideMethods
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def generated_association_methods
|
11
|
+
@generated_association_methods ||= begin
|
12
|
+
mod = const_set(:GeneratedAssociationMethods, Module.new)
|
13
|
+
include mod
|
14
|
+
mod
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias generated_feature_methods generated_association_methods \
|
18
|
+
if ActiveRecord.version < Gem::Version.new('4.1')
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
+
# override
|
21
|
+
def dangerous_attribute_method?(_name)
|
22
|
+
false
|
23
|
+
end
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
# dummy table name
|
26
|
+
def pluralize_table_names
|
27
|
+
to_s.pluralize
|
28
|
+
end
|
25
29
|
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
def clear_reflections_cache
|
31
|
+
@__reflections = nil
|
32
|
+
end
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
def default_scopes
|
35
|
+
[]
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
33
39
|
|
34
|
-
|
40
|
+
def compute_type(type_name)
|
41
|
+
if type_name.match(/^::/)
|
42
|
+
# If the type is prefixed with a scope operator then we assume that
|
43
|
+
# the type_name is an absolute reference.
|
44
|
+
ActiveSupport::Dependencies.constantize(type_name)
|
45
|
+
else
|
46
|
+
compute_type_from_candidates(type_name)
|
47
|
+
end
|
48
|
+
end
|
35
49
|
|
36
|
-
def compute_type(type_name)
|
37
|
-
if type_name.match(/^::/)
|
38
|
-
# If the type is prefixed with a scope operator then we assume that
|
39
|
-
# the type_name is an absolute reference.
|
40
|
-
ActiveSupport::Dependencies.constantize(type_name)
|
41
|
-
else
|
42
50
|
# Build a list of candidates to search for
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
def compute_type_candidates(type_name)
|
52
|
+
candidates = []
|
53
|
+
name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
|
54
|
+
candidates << type_name
|
55
|
+
end
|
46
56
|
|
47
|
-
|
48
|
-
|
57
|
+
def compute_type_from_candidates(type_name)
|
58
|
+
compute_type_candidates(type_name).each do |candidate|
|
49
59
|
constant = ActiveSupport::Dependencies.constantize(candidate)
|
50
60
|
return constant if candidate == constant.to_s
|
51
61
|
# We don't want to swallow NoMethodError < NameError errors
|
52
62
|
rescue NoMethodError
|
53
63
|
raise
|
54
|
-
rescue NameError
|
64
|
+
rescue NameError # rubocop:disable Lint/SuppressedException
|
55
65
|
end
|
56
|
-
end
|
57
66
|
|
58
|
-
|
67
|
+
raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
|
68
|
+
end
|
59
69
|
end
|
60
|
-
end
|
61
|
-
end
|
62
70
|
|
63
|
-
|
64
|
-
|
65
|
-
|
71
|
+
# use by association accessor
|
72
|
+
def association(name) #:nodoc:
|
73
|
+
association = association_instance_get(name)
|
74
|
+
|
75
|
+
if association.nil?
|
76
|
+
association = association_by_reflection(name)
|
77
|
+
association_instance_set(name, association)
|
78
|
+
end
|
66
79
|
|
67
|
-
|
68
|
-
reflection = self.class.reflect_on_association(name)
|
69
|
-
if reflection.options[:active_model]
|
70
|
-
association = ActiveRecord::Associations::HasManyForActiveModelAssociation.new(self, reflection)
|
71
|
-
else
|
72
|
-
association = reflection.association_class.new(self, reflection)
|
80
|
+
association
|
73
81
|
end
|
74
|
-
association_instance_set(name, association)
|
75
|
-
end
|
76
82
|
|
77
|
-
|
78
|
-
|
83
|
+
def read_attribute(name)
|
84
|
+
send(name)
|
85
|
+
end
|
86
|
+
alias _read_attribute read_attribute
|
79
87
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
88
|
+
# dummy
|
89
|
+
def new_record?
|
90
|
+
false
|
91
|
+
end
|
84
92
|
|
85
|
-
|
86
|
-
def new_record?
|
87
|
-
false
|
88
|
-
end
|
93
|
+
private
|
89
94
|
|
90
|
-
|
95
|
+
def association_by_reflection(name)
|
96
|
+
reflection = self.class.reflect_on_association(name)
|
97
|
+
if reflection.options[:active_model]
|
98
|
+
::ActiveRecord::Associations::HasManyForActiveModelAssociation.new(self, reflection)
|
99
|
+
else
|
100
|
+
reflection.association_class.new(self, reflection)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# override
|
105
|
+
def validate_collection_association(reflection)
|
106
|
+
return unless (association = association_instance_get(reflection.name))
|
91
107
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
108
|
+
if (records = associated_records_to_validate_or_save(
|
109
|
+
association,
|
110
|
+
false, reflection.options[:autosave]
|
111
|
+
))
|
112
|
+
records.each { |record| association_valid?(reflection, record) }
|
113
|
+
end
|
97
114
|
end
|
98
|
-
end
|
99
|
-
end
|
100
115
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
116
|
+
# use in Rails internal
|
117
|
+
def association_instance_get(name)
|
118
|
+
@association_cache[name]
|
119
|
+
end
|
105
120
|
|
106
|
-
|
107
|
-
|
108
|
-
|
121
|
+
# use in Rails internal
|
122
|
+
def association_instance_set(name, association)
|
123
|
+
@association_cache[name] = association
|
124
|
+
end
|
125
|
+
end
|
109
126
|
end
|
110
127
|
end
|
111
128
|
end
|
@@ -1,7 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EacRailsUtils
|
4
|
+
module Models
|
5
|
+
module TablelessAssociations
|
6
|
+
class Railtie < ::Rails::Railtie #:nodoc:
|
7
|
+
initializer 'activemodel-associations' do |_|
|
8
|
+
EacRailsUtils::Models::TablelessAssociations::Hooks.init
|
9
|
+
end
|
10
|
+
end
|
5
11
|
end
|
6
12
|
end
|
7
13
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'eac_rails_utils/models/tableless_associations/initialize_extension'
|
2
4
|
require 'eac_rails_utils/models/tableless_associations/active_record_reflection'
|
3
5
|
require 'eac_rails_utils/models/tableless_associations/autosave_association'
|
@@ -23,19 +25,21 @@ module EacRailsUtils
|
|
23
25
|
module ClassMethods
|
24
26
|
# define association like ActiveRecord
|
25
27
|
def belongs_to(name, scope = nil, options = {})
|
26
|
-
reflection = ActiveRecord::Associations::Builder::BelongsTo
|
28
|
+
reflection = ActiveRecord::Associations::Builder::BelongsTo
|
29
|
+
.build(self, name, scope, options)
|
27
30
|
ActiveRecord::Reflection.add_reflection self, name, reflection
|
28
31
|
end
|
29
32
|
|
30
33
|
# define association like ActiveRecord
|
31
|
-
def has_many(name, scope = nil, options = {}, &extension)
|
34
|
+
def has_many(name, scope = nil, options = {}, &extension) # rubocop:disable Naming/PredicateName
|
32
35
|
options.reverse_merge!(active_model: true, target_ids: "#{name.to_s.singularize}_ids")
|
33
36
|
if scope.is_a?(Hash)
|
34
37
|
options.merge!(scope)
|
35
38
|
scope = nil
|
36
39
|
end
|
37
40
|
|
38
|
-
reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel
|
41
|
+
reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel
|
42
|
+
.build(self, name, scope, options, &extension)
|
39
43
|
ActiveRecord::Reflection.add_reflection self, name, reflection
|
40
44
|
|
41
45
|
mixin = generated_association_methods
|
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.22.
|
4
|
+
version: 0.22.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: 2023-09
|
11
|
+
date: 2023-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bootstrap-sass
|
@@ -225,7 +225,6 @@ files:
|
|
225
225
|
- lib/eac_rails_utils/models/tableless_associations/initialize_extension.rb
|
226
226
|
- lib/eac_rails_utils/models/tableless_associations/override_methods.rb
|
227
227
|
- lib/eac_rails_utils/models/tableless_associations/railtie.rb
|
228
|
-
- lib/eac_rails_utils/models/tableless_associations/version.rb
|
229
228
|
- lib/eac_rails_utils/models/test_utils.rb
|
230
229
|
- lib/eac_rails_utils/models/validations.rb
|
231
230
|
- lib/eac_rails_utils/patches.rb
|