activerecord-virtual_attributes 6.1.1 → 6.1.2

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: 8a35d6c7665469341b76ae68e1a84180ccef887c47f56b7000efc2f0024d0a93
4
- data.tar.gz: ba57d73a5034e461d3c6bd77a50eb5475d1fe0dab8c3332f91142c893fa736a2
3
+ metadata.gz: e19652ea9b4883f4acae661b63dcc69b4fa4b0bdd0b286a0eb8b17a87892278e
4
+ data.tar.gz: 2ba220783720e1bc5b67e0af787c1396127b3fef2f2e0813deb7e34b766579b8
5
5
  SHA512:
6
- metadata.gz: 55f8e509034dc3a13dae283733da176c308d25538c38595cebfd98008d07be372c0843752e547be6afb5af361fd1610900f05fc52cdcdfbd8bff834b66233060
7
- data.tar.gz: c4511ba748f911a985f308bdfa61f9d6c64c57ec1245003dea509205f3aa4e0085f20f7e9adb5674d3657d750c34e6c4d64d41ee5faebb18846c9e8ef3ea4c32
6
+ metadata.gz: 2b60fcf44b86210f1dbe3ab9be5585158c027bc9c81c0f2ccad77a0079a77b0918bc58feca99f550f4ad2aff4e61defaf7229b125f6091dd770df0242af645fe
7
+ data.tar.gz: e691ea9679a9fda6db72fc3f057dd7d5aee163e9fd1d4aff035292da1e03c23eb711920bad8cf79ad41054bb28c2ba29ac9a03a4a38d7b44a52b9ca322245e16
@@ -13,6 +13,8 @@ jobs:
13
13
  matrix:
14
14
  ruby-version:
15
15
  - '2.7'
16
+ - '3.0'
17
+ - '3.1'
16
18
  services:
17
19
  postgres:
18
20
  image: postgres:13
@@ -41,7 +43,7 @@ jobs:
41
43
  MYSQL_HOST: 127.0.0.1
42
44
  MYSQL_PWD: password
43
45
  steps:
44
- - uses: actions/checkout@v2
46
+ - uses: actions/checkout@v4
45
47
  - name: Set up Ruby
46
48
  uses: ruby/setup-ruby@v1
47
49
  with:
@@ -54,7 +56,7 @@ jobs:
54
56
  run: bundle exec rake
55
57
  - name: Run PostgreSQL tests
56
58
  env:
57
- DB: pg
59
+ DB: postgresql
58
60
  COLLATE_SYMBOLS: false
59
61
  run: bundle exec rake
60
62
  - name: Run MySQL tests
@@ -62,8 +64,8 @@ jobs:
62
64
  DB: mysql2
63
65
  run: bundle exec rake
64
66
  - name: Report code coverage
65
- if: ${{ github.ref == 'refs/heads/master' && matrix.ruby-version == '2.7' }}
67
+ if: ${{ github.ref == 'refs/heads/master' && matrix.ruby-version == '3.1' }}
66
68
  continue-on-error: true
67
- uses: paambaati/codeclimate-action@v3.0.0
69
+ uses: paambaati/codeclimate-action@v5
68
70
  env:
69
71
  CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
data/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [6.1.2] - 2023-10-26
8
+
9
+ * Fix bind variables for joins with static strings [#124](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/124)
10
+ * Add `virtual_total` for `habtm` [#123](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/123)
11
+ * Fix: `:uses` clause now works with an array and nested hashes. [#120](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/120)
12
+ * Uses symbols in the `includes()` clause. defined by `virtual_attribute :uses` and virtual_delegate. [#128](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/128)
13
+
7
14
  ## [6.1.1] - 2022-08-09
8
15
 
9
16
  * fix HomogeneousIn clauses [#111](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/111)
@@ -86,7 +93,8 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
86
93
  * Initial Release
87
94
  * Extracted from ManageIQ/manageiq
88
95
 
89
- [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v6.1.1...HEAD
96
+ [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v6.1.2...HEAD
97
+ [6.1.2]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v6.1.1...v6.1.2
90
98
  [6.1.1]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v6.1.0...v6.1.1
91
99
  [6.1.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v3.0.0...v6.1.0
92
100
  [3.0.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v2.0.0...v3.0.0
data/README.md CHANGED
@@ -1,8 +1,8 @@
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://api.codeclimate.com/v1/badges/e1a0c26941c00f4edb55/maintainability)](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes/maintainability)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/e1a0c26941c00f4edb55/test_coverage)](https://codeclimate.com/github/ManageIQ/activerecord-virtual_attributes/test_coverage)
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
6
 
7
7
  VirtualAttributes allows you to define a ruby method that acts like an attribute or relation.
8
8
 
@@ -40,14 +40,15 @@ TODO: Write usage instructions here
40
40
 
41
41
  ## Development
42
42
 
43
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
44
 
45
45
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
46
 
47
+ To test with different database adapters, set the DB environment variable:
47
48
 
48
- To test with different versions of ruby, use `wwtd` gem or
49
-
50
- DB=pg BUNDLE_GEMFILE=gemfiles/gemfile_${version-52}.gemfile bundle exec rake
49
+ DB=postgresql bundle exec rake
50
+ DB=mysql bundle exec rake
51
+ DB=sqlite3 bundle exec rake
51
52
 
52
53
  ## Contributing
53
54
 
@@ -56,4 +57,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Manage
56
57
  ## License
57
58
 
58
59
  This project is available as open source under the terms of the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
59
-
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_runtime_dependency "activerecord", "~> 6.1.0"
29
29
 
30
30
  spec.add_development_dependency "byebug"
31
+ spec.add_development_dependency "database_cleaner-active_record", "~> 2.1"
31
32
  spec.add_development_dependency "db-query-matchers"
32
33
  spec.add_development_dependency "manageiq-style"
33
34
  spec.add_development_dependency "rake", "~> 13.0"
data/bin/setup CHANGED
@@ -1,8 +1,13 @@
1
1
  #!/usr/bin/env bash
2
2
  set -euo pipefail
3
3
  IFS=$'\n\t'
4
- set -vx
4
+ set -e
5
5
 
6
6
  bundle install
7
+ echo
7
8
 
8
- # Do any other automated setup that you need to do here
9
+ echo "Setting up the postgres database for specs..."
10
+ echo "SELECT 'CREATE DATABASE virtual_attributes' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'virtual_attributes')\gexec" | psql -U postgres
11
+
12
+ echo "Setting up the mysql database for specs..."
13
+ mysql -u root -e 'CREATE SCHEMA IF NOT EXISTS 'virtual_attributes';'
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module VirtualAttributes
3
- VERSION = "6.1.1".freeze
3
+ VERSION = "6.1.2".freeze
4
4
  end
5
5
  end
@@ -36,10 +36,8 @@ module ActiveRecord
36
36
  end
37
37
 
38
38
  module VirtualArel
39
- # This arel table proxy is our shim to get our functionality into rails
39
+ # This arel table proxy. This allows WHERE clauses to use virtual attributes
40
40
  class ArelTableProxy < Arel::Table
41
- attr_accessor :klass
42
-
43
41
  # overrides Arel::Table#[]
44
42
  # adds aliases and virtual attribute arel (aka sql)
45
43
  #
@@ -70,16 +68,9 @@ module ActiveRecord
70
68
  end
71
69
 
72
70
  module ClassMethods
73
- if ActiveRecord.version.to_s < "6.1"
74
- # ActiveRecord::Core 6.0 (every version of active record seems to do this differently)
75
- def arel_table
76
- @arel_table ||= ArelTableProxy.new(table_name, :type_caster => type_caster).tap { |t| t.klass = self }
77
- end
78
- else
79
- # ActiveRecord::Core 6.1
80
- def arel_table
81
- @arel_table ||= ArelTableProxy.new(table_name, :klass => self)
82
- end
71
+ # ActiveRecord::Core 6.1
72
+ def arel_table
73
+ @arel_table ||= ArelTableProxy.new(table_name, :klass => self)
83
74
  end
84
75
 
85
76
  # supported by sql if any are true:
@@ -43,7 +43,7 @@ module ActiveRecord
43
43
  method_prefix = virtual_delegate_name_prefix(options[:prefix], to)
44
44
  method_name = "#{method_prefix}#{method}"
45
45
  if to.include?(".") # to => "target.method"
46
- to, method = to.split(".")
46
+ to, method = to.split(".").map(&:to_sym)
47
47
  options[:to] = to
48
48
  end
49
49
 
@@ -260,8 +260,7 @@ module ActiveRecord
260
260
 
261
261
  yield arel if block_given?
262
262
 
263
- # convert arel to sql to populate with bind variables
264
- ::Arel::Nodes::Grouping.new(Arel.sql(arel.to_sql))
263
+ ::Arel::Nodes::Grouping.new(arel)
265
264
  end
266
265
 
267
266
  # determine table reference to use for a sub query
@@ -35,7 +35,7 @@ module ActiveRecord
35
35
 
36
36
  case associations
37
37
  when String, Symbol
38
- virtual_field?(associations) ? replace_virtual_fields(virtual_includes(associations)) : associations
38
+ virtual_field?(associations) ? replace_virtual_fields(virtual_includes(associations)) : associations.to_sym
39
39
  when Array
40
40
  associations.collect { |association| replace_virtual_fields(association) }.compact
41
41
  when Hash
@@ -67,9 +67,11 @@ module ActiveRecord
67
67
  def include_to_hash(value)
68
68
  case value
69
69
  when String, Symbol
70
- {value => {}}
70
+ {value.to_sym => {}}
71
71
  when Array
72
- value.flatten.each_with_object({}) { |k, h| h[k] = {} }
72
+ value.flatten.each_with_object({}) do |k, h|
73
+ merge_includes(h, k)
74
+ end
73
75
  when nil
74
76
  {}
75
77
  else
@@ -77,15 +79,21 @@ module ActiveRecord
77
79
  end
78
80
  end
79
81
 
80
- # @param [Hash] hash1
81
- # @param [Hash] hash2
82
+ # @param [Hash] hash1 (incoming hash is modified and returned)
83
+ # @param [Hash|Symbol|nil] hash2 (this hash will not be modified)
82
84
  def merge_includes(hash1, hash2)
83
85
  return hash1 if hash2.blank?
84
86
 
85
- hash1 = include_to_hash(hash1)
86
- hash2 = include_to_hash(hash2)
87
- hash1.deep_merge!(hash2) do |_k, v1, v2|
88
- merge_includes(v1, v2)
87
+ # very common case.
88
+ # optimization to skip deep_merge and hash creation
89
+ if hash2.kind_of?(Symbol)
90
+ hash1[hash2] ||= {}
91
+ return hash1
92
+ end
93
+
94
+ hash1.deep_merge!(include_to_hash(hash2)) do |_k, v1, v2|
95
+ # this block is conflict resolution when a key has 2 values
96
+ merge_includes(include_to_hash(v1), v2)
89
97
  end
90
98
  end
91
99
  end
@@ -114,7 +114,7 @@ module VirtualAttributes
114
114
  end
115
115
 
116
116
  def virtual_aggregate_arel(reflection, method_name, column)
117
- return unless reflection && reflection.macro == :has_many
117
+ return unless reflection && [:has_many, :has_and_belongs_to_many].include?(reflection.macro)
118
118
 
119
119
  # need db access for the reflection join_keys, so delaying all this key lookup until call time
120
120
  lambda do |t|
@@ -137,14 +137,8 @@ module VirtualAttributes
137
137
  # query: SELECT COUNT(*) FROM foreign_table JOIN ... [WHERE main_table.id = foreign_table.id]
138
138
  query.where(join.right.expr)
139
139
 
140
- # convert bind variables from ? to actual values. otherwise, sql is incomplete
141
- conn = connection
142
- sql = conn.unprepared_statement { conn.to_sql(query) }
143
-
144
- # add () around query
145
- query = t.grouping(Arel::Nodes::SqlLiteral.new(sql))
146
140
  # add coalesce to ensure correct value comes out
147
- t.grouping(Arel::Nodes::NamedFunction.new('COALESCE', [query, Arel::Nodes::SqlLiteral.new("0")]))
141
+ t.grouping(Arel::Nodes::NamedFunction.new('COALESCE', [t.grouping(query), Arel::Nodes::SqlLiteral.new("0")]))
148
142
  end
149
143
  end
150
144
  end
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: 6.1.1
4
+ version: 6.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keenan Brock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-09 00:00:00.000000000 Z
11
+ date: 2023-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: database_cleaner-active_record
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: db-query-matchers
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -167,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
181
  - !ruby/object:Gem::Version
168
182
  version: '0'
169
183
  requirements: []
170
- rubygems_version: 3.1.6
184
+ rubygems_version: 3.2.33
171
185
  signing_key:
172
186
  specification_version: 4
173
187
  summary: Access non-sql attributes from sql