activerecord-virtual_attributes 7.2.0.0 → 8.0.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: ef69f0683d1cf4b95da626b2407fe42fcd2e6564053bcb424ea8c867365e6c94
4
- data.tar.gz: 28d0d1506872044eae2cb1d9e84cd0044e9dae43f849facabd44024c9f1ed676
3
+ metadata.gz: d6a140b11204b4edf29cfe6e13fdfe53226b56e73279c63e9878ccfe5ead543b
4
+ data.tar.gz: 1496844f5c1ec0870fa66fc8905c6f34d0d0e8064cf71b26cb1f0b87db15917f
5
5
  SHA512:
6
- metadata.gz: 2c26a6ea644c42aa0317bcf3848f31ae79d49a86f80fa2c0832642652c9195171f52fe23b52789544fd7045efbb51a398d911e39fc2f4645e2e88a2945fa1555
7
- data.tar.gz: 3d59e61f9b22e9037237c1e1f8e8be91ee36dc165c54a101bf742f4a5b88cc9da6b0a13fef44dbe18dda4f0af299028179e5e37c001e7f3419f6cd988679e044
6
+ metadata.gz: db6751f0bad4abf5a669f035172ad6e338df09b073594e505360a16f11f5d24188c102d0cfdc2da40921eb1e67939a62715b248436bf3d4b31a07c7a6dd84994
7
+ data.tar.gz: 8da21356c323be6cdd90134b3f7125589b3d337ad9be2081fd43b9072fb7866e55cbafa67e53335b83b0c6265caebe92656b59562e1984b398977720fd349124
data/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [8.0.0] - 2026-01-22
8
+
9
+ * Drop unnecessary load_schema statements [#197](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/197)
10
+ * Attribute delegation [#192](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/192)
11
+ * Spec wording, better error message for bogus va :through [#202](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/202)
12
+ * Support rails 8, drop support for 7.2 [#216](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/216)
13
+ * Make the virtual_delegate deprecation warning less noisy [#217](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/217)
14
+
15
+ ## [7.2.0.1] - 2025-07-18
16
+
17
+ * Fix includes and associations with empty uses [#195](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/195)
18
+
7
19
  ## [7.2.0.0] - 2025-06-25
8
20
 
9
21
  * virtual_delegate requires type [#185](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/185)
@@ -125,7 +137,9 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
125
137
  * Initial Release
126
138
  * Extracted from ManageIQ/manageiq
127
139
 
128
- [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.0...HEAD
140
+ [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v8.0.0...HEAD
141
+ [8.0.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.1...v8.0.0
142
+ [7.2.0.1]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.0...v7.2.0.1
129
143
  [7.2.0.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.2...v7.2.0.0
130
144
  [7.1.2]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.1...v7.1.2
131
145
  [7.1.1]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.0...v7.1.1
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # VirtualAttributes
2
2
 
3
3
  [![CI](https://github.com/ManageIQ/activerecord-virtual_attributes/actions/workflows/ci.yaml/badge.svg)](https://github.com/ManageIQ/activerecord-virtual_attributes/actions/workflows/ci.yaml)
4
- [![Maintainability](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes.svg)](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes/maintainability)
5
- [![Test Coverage](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes/coverage.svg)](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes/test_coverage)
6
4
 
7
5
  VirtualAttributes allows you to define a ruby method that acts like an attribute or relation.
8
6
 
@@ -16,6 +14,17 @@ This also allows you to calculate counts and treat those as a field accessible w
16
14
 
17
15
  As of v6.1.0, the versioning of this gem follows ActiveRecord versioning, and does not follow SemVer (e.g. virtual attributes v6.1.x supports all versions of Rails 6.1). Version v3.0.0 supports Rails 6.0 and lower.
18
16
 
17
+ Refer to the table below:
18
+
19
+ Version | Rails
20
+ --------|-------
21
+ v3.0.x | 6.0.x and lower
22
+ v6.1.x | 6.1.x
23
+ v7.0.x | 7.0.x
24
+ v7.1.x | 7.1.x
25
+ v7.2.x | 7.2.x
26
+ v8.0.x | 8.0.x
27
+
19
28
  Use the latest version of both this gem and Rails where the first 2 digits match.
20
29
 
21
30
  ## Installation
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.push(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  require "active_record/virtual_attributes/version"
@@ -22,13 +22,13 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  # Specify which files should be added to the gem when it is released.
24
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|bin)/}) || f.match(/^(\.)|renovate.json/) }
27
27
  end
28
28
 
29
29
  spec.require_paths = ["lib"]
30
30
 
31
- spec.add_runtime_dependency "activerecord", "~> 7.2.2", ">=7.2.2.1"
31
+ spec.add_runtime_dependency "activerecord", "~> 8.0.4"
32
32
 
33
33
  spec.add_development_dependency "byebug"
34
34
  spec.add_development_dependency "database_cleaner-active_record", "~> 2.1"
@@ -40,5 +40,5 @@ Gem::Specification.new do |spec|
40
40
  spec.add_development_dependency "rake", "~> 13.0"
41
41
  spec.add_development_dependency "rspec", "~> 3.0"
42
42
  spec.add_development_dependency "simplecov", ">= 0.21.2"
43
- spec.add_development_dependency "sqlite3", "< 2"
43
+ spec.add_development_dependency "sqlite3"
44
44
  end
@@ -1,7 +1,7 @@
1
1
  RSpec::Matchers.define(:have_virtual_attribute) do |name, type|
2
2
  match do |klass|
3
- expect(klass.has_attribute?(name)).to be_truthy
4
- expect(klass.virtual_attribute?(name)).to be_truthy
3
+ expect(klass.has_attribute?(name)).to(be_truthy)
4
+ expect(klass.virtual_attribute?(name)).to(be_truthy)
5
5
  expect(klass.type_for_attribute(name.to_s).type).to(eq(type)) if type
6
6
  klass.instance_methods.include?(name.to_sym)
7
7
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module VirtualAttributes
3
- VERSION = "7.2.0.0".freeze
3
+ VERSION = "8.0.0".freeze
4
4
  end
5
5
  end
@@ -83,7 +83,6 @@ module ActiveRecord
83
83
  # - it is an attribute that is non virtual
84
84
  # - it is an attribute that is virtual and has arel defined
85
85
  def attribute_supported_by_sql?(name)
86
- load_schema
87
86
  try(:attribute_alias?, name) ||
88
87
  (has_attribute?(name) && (!virtual_attribute?(name) || !!_virtual_arel[name.to_s]))
89
88
  end
@@ -15,6 +15,7 @@ module ActiveRecord
15
15
  #
16
16
 
17
17
  def virtual_delegate(*methods, to:, type:, prefix: nil, allow_nil: nil, default: nil, uses: nil, **options) # rubocop:disable Naming/MethodParameterName
18
+ src_loc = caller_locations
18
19
  to = to.to_s
19
20
  if to.include?(".") && (methods.size > 1 || prefix)
20
21
  raise ArgumentError, 'Delegation only supports specifying a target method name when defining a single virtual method with no prefix'
@@ -28,12 +29,12 @@ module ActiveRecord
28
29
  # This better supports reloading of the class and changing the definitions
29
30
  methods.each do |method|
30
31
  method_name, to, method = determine_method_names(method, to, prefix)
31
- unless (to_ref = reflection_with_virtual(to))
32
- raise ArgumentError, "Delegation needs an association. Association #{to} does not exist"
33
- end
34
32
 
35
- define_delegate(method_name, method, :to => to, :allow_nil => allow_nil, :default => default)
36
- virtual_attribute(method_name, type, :uses => (uses || to), :arel => virtual_delegate_arel(method, to_ref), **options)
33
+ # NOTE: delete_blank will remove a default of []. we only want to remove nils
34
+ va_params = options.merge(:uses => uses, :through => to, :source => method, :default => default).delete_if { |_n, v| v.nil? }
35
+
36
+ ActiveRecord::VirtualAttributes.deprecator.warn("virtual_delegate is deprecated in favor of virtual_attribute. Change to: #{name}.virtual_attribute #{method_name.inspect}, #{type.inspect}, #{va_params.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(", ")}", src_loc)
37
+ virtual_attribute(method_name, type, **va_params)
37
38
  end
38
39
  end
39
40
 
@@ -105,7 +106,7 @@ module ActiveRecord
105
106
  end
106
107
 
107
108
  method_prefix = "#{prefix == true ? to : prefix}_" if prefix
108
- method_name = "#{method_prefix}#{method_name}".to_sym
109
+ method_name = :"#{method_prefix}#{method_name}"
109
110
 
110
111
  [method_name, to.to_sym, column]
111
112
  end
@@ -6,6 +6,7 @@ module ActiveRecord
6
6
  include ActiveRecord::VirtualAttributes::VirtualReflections
7
7
 
8
8
  # rubocop:disable Style/SingleLineMethods
9
+ # rubocop:disable Naming/PredicateMethod
9
10
  module NonARModels
10
11
  def dangerous_attribute_method?(_); false; end
11
12
 
@@ -15,6 +16,7 @@ module ActiveRecord
15
16
 
16
17
  def belongs_to_required_by_default; false; end
17
18
  end
19
+ # rubocop:enable Naming/PredicateMethod
18
20
  # rubocop:enable Style/SingleLineMethods
19
21
 
20
22
  included do
@@ -183,6 +185,7 @@ module ActiveRecord
183
185
  source_records.each do |record|
184
186
  # begin virtual_attributes changes
185
187
  association = record.class.replace_virtual_fields(self.association)
188
+ next if association.nil?
186
189
  # end virtual_attributes changes
187
190
 
188
191
  reflection = record.class._reflect_on_association(association)
@@ -18,7 +18,6 @@ module ActiveRecord
18
18
 
19
19
  module ClassMethods
20
20
  def virtual_includes(name)
21
- load_schema
22
21
  _virtual_includes[name.to_s]
23
22
  end
24
23
 
@@ -56,13 +56,29 @@ module ActiveRecord
56
56
  virtual_attribute(name, type, **options)
57
57
  end
58
58
 
59
- def virtual_attribute(name, type, uses: nil, arel: nil, **options)
59
+ def virtual_attribute(name, type, through: nil, uses: nil, arel: nil, source: name, default: nil, **options)
60
60
  name = name.to_s
61
61
  reload_schema_from_cache
62
62
 
63
63
  self.virtual_attributes_to_define =
64
64
  virtual_attributes_to_define.merge(name => [type, options])
65
65
 
66
+ if through
67
+ define_delegate(name, source, :to => through, :allow_nil => true, :default => default)
68
+
69
+ unless (to_ref = reflection_with_virtual(through))
70
+ raise ArgumentError, "#{self.name}.virtual_attribute #{name.inspect} references unknown :through association #{through.inspect}"
71
+ end
72
+
73
+ # ensure that the through table is in the uses clause
74
+ uses = merge_includes({through => {}}, uses)
75
+
76
+ # We can not validate target#source exists
77
+ # Because we may not have loaded the class yet
78
+ # And we definitely have not loaded the database yet
79
+ arel ||= virtual_delegate_arel(source, to_ref)
80
+ end
81
+
66
82
  define_virtual_include(name, uses)
67
83
  define_virtual_arel(name, arel)
68
84
  end
@@ -72,7 +88,6 @@ module ActiveRecord
72
88
  #
73
89
 
74
90
  def virtual_attribute?(name)
75
- load_schema
76
91
  has_attribute?(name) && (
77
92
  !respond_to?(:column_for_attribute) ||
78
93
  column_for_attribute(name).kind_of?(ActiveRecord::ConnectionAdapters::NullColumn)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-virtual_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.2.0.0
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keenan Brock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-26 00:00:00.000000000 Z
11
+ date: 2026-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 7.2.2
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 7.2.2.1
19
+ version: 8.0.4
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 7.2.2
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 7.2.2.1
26
+ version: 8.0.4
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: byebug
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -160,16 +154,16 @@ dependencies:
160
154
  name: sqlite3
161
155
  requirement: !ruby/object:Gem::Requirement
162
156
  requirements:
163
- - - "<"
157
+ - - ">="
164
158
  - !ruby/object:Gem::Version
165
- version: '2'
159
+ version: '0'
166
160
  type: :development
167
161
  prerelease: false
168
162
  version_requirements: !ruby/object:Gem::Requirement
169
163
  requirements:
170
- - - "<"
164
+ - - ">="
171
165
  - !ruby/object:Gem::Version
172
- version: '2'
166
+ version: '0'
173
167
  description: Define attributes in arel
174
168
  email:
175
169
  - keenan@thebrocks.net