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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d70491a91afe401a5ff75d1ecf1d1820e9164bb725d33ecbf5a233508e70faa
4
- data.tar.gz: 59c3693a4563491e0da78bf4496d620c75025b7b70fb4cda760405ac04c3f436
3
+ metadata.gz: 8a88b255d31abb1059b16e75e33cb24b9ba2ad64a3d35afd63e27bc1d01dbbbe
4
+ data.tar.gz: b50b1587013cb361fe0eb2d7d56ee46dd5bdd87ed6aacf836aee7867ec7c152e
5
5
  SHA512:
6
- metadata.gz: ed28ed741a6dc380f97f1b3a1af9afaba32baebeee1c06ccf81790eab178ac8eadf022051a3dd5dffd98c01bf421fc749ce69d4db8b4a078c8d347795a286949
7
- data.tar.gz: d5b26dc72e4a8d36e9b609b606a95264432c81e612a08fa705c40da2db94fe3e6ba23108121f170dcd1f395cea969133c6799ab15ccb6520e0f3a8a22e2ed025
6
+ metadata.gz: 1249f9ba2a9598117e86de27b315fdd2f6f0f55330ee39916ea9e03f25c97b9c966a99a476628a1dfda677ea14b518cdf7478ad21eafc41868b4bcf4222fe65e
7
+ data.tar.gz: a7def8aa5c32a906ecae3e29b5c42ba669f962c368ad3ae01153de142ce74cc3f3da22f9090cc4d9d97fa359080ed414985be9879b8d3f42ec476e7b6f0dcae9
@@ -1,36 +1,42 @@
1
- module EacRailsUtils::Models::TablelessAssociations
2
- module ActiveRecordReflection
3
- extend ActiveSupport::Concern
1
+ # frozen_string_literal: true
4
2
 
5
- included do
6
- if ActiveRecord.version >= Gem::Version.new("4.1.2")
7
- class_attribute :_reflections
8
- self._reflections = ActiveSupport::HashWithIndifferentAccess.new
9
- else
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
- module ClassMethods
16
- if ActiveRecord.version < Gem::Version.new("4.1")
17
- def create_reflection(macro, name, scope, options, active_record)
18
- case macro
19
- when :has_many, :belongs_to
20
- klass = ActiveRecord::Reflection::AssociationReflection
21
- reflection = klass.new(macro, name, scope, options, active_record)
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
- def reflect_on_association(association)
30
- if ActiveRecord.version >= Gem::Version.new("4.1.2")
31
- _reflections[association.to_s]
32
- else
33
- reflections[association]
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
- module EacRailsUtils::Models::TablelessAssociations
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
- super
11
- end
12
- else
13
- def add_constraints(scope, owner, assoc_klass, refl, tracker)
14
- if refl.options[:active_model]
15
- target_ids = refl.options[:target_ids]
16
- return scope.where(id: owner[target_ids])
17
- end
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
- super
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
- module EacRailsUtils::Models::TablelessAssociations
2
- module AutosaveAssociation
3
- extend ActiveSupport::Concern
1
+ # frozen_string_literal: true
4
2
 
5
- include ActiveRecord::AutosaveAssociation
3
+ module EacRailsUtils
4
+ module Models
5
+ module TablelessAssociations
6
+ module AutosaveAssociation
7
+ extend ActiveSupport::Concern
6
8
 
7
- included do
8
- extend ActiveModel::Callbacks
9
- define_model_callbacks :save, :create, :update
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
- module EacRailsUtils::Models::TablelessAssociations
2
- module Hooks
3
- def self.init
4
- ActiveSupport.on_load(:active_record) do
5
- require 'eac_rails_utils/models/tableless_associations/association_scope_extension'
6
- ActiveRecord::Associations::AssociationScope.send(:prepend, EacRailsUtils::Models::TablelessAssociations::AssociationScopeExtension)
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
- module EacRailsUtils::Models::TablelessAssociations
2
- module InitializeExtension
3
- extend ActiveSupport::Concern
1
+ # frozen_string_literal: true
4
2
 
5
- included do
6
- prepend WithAssociationCache
7
- end
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
- module WithAssociationCache
10
- def initialize(*args)
11
- @association_cache = {}
12
- super
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
- module EacRailsUtils::Models::TablelessAssociations
2
- module OverrideMethods
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def generated_association_methods
7
- @generated_association_methods ||= begin
8
- mod = const_set(:GeneratedAssociationMethods, Module.new)
9
- include mod
10
- mod
11
- end
12
- end
13
- alias :generated_feature_methods :generated_association_methods \
14
- if ActiveRecord.version < Gem::Version.new("4.1")
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
- # override
17
- def dangerous_attribute_method?(name)
18
- false
19
- end
20
+ # override
21
+ def dangerous_attribute_method?(_name)
22
+ false
23
+ end
20
24
 
21
- # dummy table name
22
- def pluralize_table_names
23
- self.to_s.pluralize
24
- end
25
+ # dummy table name
26
+ def pluralize_table_names
27
+ to_s.pluralize
28
+ end
25
29
 
26
- def clear_reflections_cache
27
- @__reflections = nil
28
- end
30
+ def clear_reflections_cache
31
+ @__reflections = nil
32
+ end
29
33
 
30
- def default_scopes
31
- []
32
- end
34
+ def default_scopes
35
+ []
36
+ end
37
+
38
+ protected
33
39
 
34
- protected
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
- candidates = []
44
- name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
45
- candidates << type_name
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
- candidates.each do |candidate|
48
- begin
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
- raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
67
+ raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
68
+ end
59
69
  end
60
- end
61
- end
62
70
 
63
- # use by association accessor
64
- def association(name) #:nodoc:
65
- association = association_instance_get(name)
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
- if association.nil?
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
- association
78
- end
83
+ def read_attribute(name)
84
+ send(name)
85
+ end
86
+ alias _read_attribute read_attribute
79
87
 
80
- def read_attribute(name)
81
- send(name)
82
- end
83
- alias :_read_attribute :read_attribute
88
+ # dummy
89
+ def new_record?
90
+ false
91
+ end
84
92
 
85
- # dummy
86
- def new_record?
87
- false
88
- end
93
+ private
89
94
 
90
- private
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
- # override
93
- def validate_collection_association(reflection)
94
- if association = association_instance_get(reflection.name)
95
- if records = associated_records_to_validate_or_save(association, false, reflection.options[:autosave])
96
- records.each { |record| association_valid?(reflection, record) }
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
- # use in Rails internal
102
- def association_instance_get(name)
103
- @association_cache[name]
104
- end
116
+ # use in Rails internal
117
+ def association_instance_get(name)
118
+ @association_cache[name]
119
+ end
105
120
 
106
- # use in Rails internal
107
- def association_instance_set(name, association)
108
- @association_cache[name] = association
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
- module EacRailsUtils::Models::TablelessAssociations
2
- class Railtie < ::Rails::Railtie #:nodoc:
3
- initializer 'activemodel-associations' do |_|
4
- EacRailsUtils::Models::TablelessAssociations::Hooks.init
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.build(self, name, scope, options)
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.build(self, name, scope, options, &extension)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsUtils
4
- VERSION = '0.22.0'
4
+ VERSION = '0.22.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.22.0
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-18 00:00:00.000000000 Z
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
@@ -1,5 +0,0 @@
1
- module ActiveModel
2
- module Associations
3
- VERSION = "0.2.0"
4
- end
5
- end