activerecord-virtual_attributes 1.1.0 → 1.2.0

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: 6f8bd112296732146d0d98ed5f2f490b6d72361bc965d16d092f0712d6061767
4
- data.tar.gz: 5dbbc67687d1143f69f18292f2cbdefe5166b69c8e034d5050cd2d203730ac5d
3
+ metadata.gz: 9a6c500c61ceb22d7703070013800f894b62a5efd8bf14e3e5aec0e24afdb5e1
4
+ data.tar.gz: 1e8c99b6823e09e10e39958c3f50651e5f1b558a76ef15377f23cd13571fd2cc
5
5
  SHA512:
6
- metadata.gz: fd7ab60cefbb59e9c77f197f755822e530d95e52fc67fb0f9eea261473abfae8a34713957e756fa328c2128982a3af344eae629d5d3b9b8bdaec14f671985a07
7
- data.tar.gz: a08eecd9ed665f9276fe098a95108df2d7e566751b6a8e0ed5ca39d34738a3dd5638df1e33517f41b6016ae446c4cb470dac1ed49c2a48613c7f515c8c72ec7b
6
+ metadata.gz: 0d1b3dc8b531417c714f180c74946f7f42fe6470b7a4912567be43f79529cdaad399c4316af05e0ca63b51e37d022f2887e316752d69371afed7c7ca92f0c6d0
7
+ data.tar.gz: fb251d9ca04da11c0042f0ec93c908e1ded759faf3c0935a94a7809219cd3ba73d7d4ef7451d6d7293efded29da176af921bcc92607170b5d2219c546559d317
data/.codeclimate.yml CHANGED
@@ -27,4 +27,6 @@ checks:
27
27
  plugins:
28
28
  rubocop:
29
29
  enabled: true
30
- config: ".rubocop_cc.yml"
30
+ channel: "rubocop-0-67"
31
+ config:
32
+ file: ".rubocop_cc.yml"
data/.yamllint ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ extends: relaxed
3
+
4
+ rules:
5
+ indentation:
6
+ indent-sequences: false
7
+ line-length:
8
+ max: 1000
data/CHANGELOG.md CHANGED
@@ -5,24 +5,32 @@ a nice looking [Changelog](http://keepachangelog.com).
5
5
 
6
6
  ## Version [Unreleased]
7
7
 
8
+ ## Version [1.2.0] <small>2019-04-23</small>
9
+
10
+ * Virtual_delegate :type now specified to avoid rare race conditions with attribute discovery
11
+ * Delays interpreting delegate until column is used
12
+ * Postgres now supports order by virtual_aggregate
13
+ * More flexible includes. e.g.: Arrays of symbols now work
14
+ * Raises errors for invalid `includes()` and `:uses`
15
+
8
16
  ## Version [1.1.0] <small>2019-04-23</small>
9
17
 
10
- * add legacy types for VirtualAttribute::Types
11
- * fix rails 5.1 bug with includes
12
- * remove reference to MiqPreloader
13
- * climate code fixes
18
+ * Add legacy types for `VirtualAttribute::Types`
19
+ * Fix rails 5.1 bug with `includes()`
20
+ * Remove reference to `MiqPreloader`
14
21
 
15
22
  ## Version [1.0.0] <small>2019-03-05</small>
16
23
 
17
- * renamed to activerecord-virtual_attributes
18
- * moved to ManageIQ
19
- * added support for Rails 5.1
24
+ * Renamed to activerecord-virtual_attributes
25
+ * Moved from ManageIQ to own repo
26
+ * Added support for Rails 5.1
20
27
 
21
28
  ## Version 0.1.0 <small>2019-01-17</small>
22
29
 
23
30
  * Initial Release
24
31
  * Extracted from ManageIQ/manageiq
25
32
 
26
- [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v1.1.0...HEAD
33
+ [Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v1.2.0...HEAD
34
+ [1.2.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v1.1.0...v1.2.0
27
35
  [1.1.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v1.0.0...v1.1.0
28
36
  [1.0.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v0.1.0...v1.0.0
data/Gemfile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.0.0"
5
+ gem "activerecord", "~> 5.0.7"
6
6
  gem "sqlite3", "~> 1.3.6"
7
7
  gem "pg"
8
8
  gem "mysql2", '~> 0.4.0'
@@ -15,15 +15,21 @@ module ActiveRecord
15
15
  module Type
16
16
  # TODO: do we actually need symbol types?
17
17
  class Symbol < ActiveRecord::Type::String
18
- def type; :symbol; end
18
+ def type
19
+ :symbol
20
+ end
19
21
  end
20
22
 
21
23
  class StringSet < ActiveRecord::Type::Value
22
- def type; :string_set; end
24
+ def type
25
+ :string_set
26
+ end
23
27
  end
24
28
 
25
29
  class NumericSet < ActiveRecord::Type::Value
26
- def type; :numeric_set; end
30
+ def type
31
+ :numeric_set
32
+ end
27
33
  end
28
34
  end
29
35
 
@@ -37,7 +43,6 @@ module ActiveRecord
37
43
  end
38
44
 
39
45
  module ClassMethods
40
-
41
46
  #
42
47
  # Definition
43
48
  #
@@ -127,17 +132,17 @@ require "active_record/virtual_attributes/virtual_fields"
127
132
 
128
133
  # this patch is no longer necessary for 5.2
129
134
  if ActiveRecord.version.to_s < "5.2"
130
- require "active_record/attribute"
131
- module ActiveRecord
132
- # This is a bug in rails 5.0 and 5.1, but it is made much worse by virtual attributes
133
- class Attribute
134
- def with_value_from_database(value)
135
- # self.class.from_database(name, value, type)
136
- initialized? ? self.class.from_database(name, value, type) : self
135
+ require "active_record/attribute"
136
+ module ActiveRecord
137
+ # This is a bug in rails 5.0 and 5.1, but it is made much worse by virtual attributes
138
+ class Attribute
139
+ def with_value_from_database(value)
140
+ # self.class.from_database(name, value, type)
141
+ initialized? ? self.class.from_database(name, value, type) : self
142
+ end
137
143
  end
138
144
  end
139
145
  end
140
- end
141
146
 
142
147
  require "active_record/virtual_attributes/virtual_total"
143
148
  require "active_record/virtual_attributes/arel_groups"
@@ -1,27 +1,3 @@
1
- # TODO: expose this to classes that include this gem
2
-
3
- # legacy matcher
4
- RSpec::Matchers.define :have_virtual_column do |name, type|
5
- match do |klass|
6
- expect(klass.has_attribute?(name)).to be_truthy
7
- expect(klass.virtual_attribute?(name)).to be_truthy
8
- expect(klass.type_for_attribute(name).type).to eq(type)
9
- klass.instance_methods.include?(name.to_sym)
10
- end
11
-
12
- failure_message do |klass|
13
- "expected #{klass.name} to have virtual column #{name.inspect} with type #{type.inspect}"
14
- end
15
-
16
- failure_message_when_negated do |klass|
17
- "expected #{klass.name} to not have virtual column #{name.inspect} with type #{type.inspect}"
18
- end
19
-
20
- description do
21
- "expect the object to have the virtual column"
22
- end
23
- end
24
-
25
1
  RSpec::Matchers.define :have_virtual_attribute do |name, type|
26
2
  match do |klass|
27
3
  expect(klass.has_attribute?(name)).to be_truthy
@@ -42,3 +18,5 @@ RSpec::Matchers.define :have_virtual_attribute do |name, type|
42
18
  "expect the object to have the virtual column"
43
19
  end
44
20
  end
21
+
22
+ RSpec::Matchers.alias_matcher(:have_virtual_column, :have_virtual_attribute)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module VirtualAttributes
3
- VERSION = "1.1.0"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
@@ -37,6 +37,7 @@ module ActiveRecord
37
37
  try(:attribute_alias?, name) ||
38
38
  (has_attribute?(name) && (!virtual_attribute?(name) || !!_virtual_arel[name.to_s]))
39
39
  end
40
+
40
41
  private
41
42
 
42
43
  def define_virtual_arel(name, arel)
@@ -15,7 +15,6 @@ module ActiveRecord
15
15
  end
16
16
 
17
17
  module ClassMethods
18
-
19
18
  #
20
19
  # Definition
21
20
  #
@@ -72,7 +71,8 @@ module ActiveRecord
72
71
  end
73
72
 
74
73
  col = col.to_s
75
- type = to_ref.klass.type_for_attribute(col)
74
+ type = options[:type] || to_ref.klass.type_for_attribute(col)
75
+ type = ActiveRecord::Type.lookup(type) if type.kind_of?(Symbol)
76
76
  raise "unknown attribute #{to}##{col} referenced in #{name}" unless type
77
77
  arel = virtual_delegate_arel(col, to_ref)
78
78
  define_virtual_attribute(method_name, type, :uses => (options[:uses] || to), :arel => arel)
@@ -142,20 +142,23 @@ module ActiveRecord
142
142
  # See select_from_alias for examples
143
143
 
144
144
  def virtual_delegate_arel(col, to_ref)
145
- # ensure the column has sql and the association is reachable via sql
145
+ # Ensure the association is reachable via sql
146
+ #
147
+ # But NOT ensuring the target column has sql
148
+ # to_ref.klass.arel_attribute(col) loads the target classes' schema.
149
+ # This cascades and causing a race condition
150
+ #
146
151
  # There is currently no way to propagate sql over a virtual association
147
- if to_ref.klass.arel_attribute(col) && reflect_on_association(to_ref.name)
148
- if to_ref.macro == :has_one || to_ref.macro == :belongs_to
152
+ if reflect_on_association(to_ref.name) && (to_ref.macro == :has_one || to_ref.macro == :belongs_to)
153
+ lambda do |t|
154
+ join_keys = if ActiveRecord.version.to_s >= "5.1"
155
+ to_ref.join_keys
156
+ else
157
+ to_ref.join_keys(to_ref.klass)
158
+ end
159
+ src_model_id = arel_attribute(join_keys.foreign_key, t)
149
160
  blk = ->(arel) { arel.limit = 1 } if to_ref.macro == :has_one
150
- lambda do |t|
151
- if ActiveRecord.version.to_s >= "5.1"
152
- join_keys = to_ref.join_keys
153
- else
154
- join_keys = to_ref.join_keys(to_ref.klass)
155
- end
156
- src_model_id = arel_attribute(join_keys.foreign_key, t)
157
- VirtualDelegates.select_from_alias(to_ref, col, join_keys.key, src_model_id, &blk)
158
- end
161
+ VirtualDelegates.select_from_alias(to_ref, col, join_keys.key, src_model_id, &blk)
159
162
  end
160
163
  end
161
164
  end
@@ -40,7 +40,7 @@ module ActiveRecord
40
40
  associations.each_with_object({}) do |(parent, child), h|
41
41
  next if virtual_field?(parent)
42
42
  reflection = reflect_on_association(parent.to_sym)
43
- h[parent] = reflection.options[:polymorphic] ? nil : reflection.klass.remove_virtual_fields(child) if reflection
43
+ h[parent] = reflection.nil? || reflection.options[:polymorphic] ? {} : reflection.klass.remove_virtual_fields(child) || {}
44
44
  end
45
45
  else
46
46
  associations
@@ -100,14 +100,6 @@ module VirtualAttributes
100
100
  reflection.klass.all
101
101
  end
102
102
 
103
- # ordering will probably screw up aggregations, so clear this out from
104
- # any calls
105
- #
106
- # only clear this out if this isn't a `:size` call as well, since doing
107
- # a COUNT(*) will allow any ORDER BY to still work properly. This is
108
- # to avoid any possible edge cases by clearing out the order clause.
109
- query.order_values = [] if method_name != :size
110
-
111
103
  foreign_table = reflection.klass.arel_table
112
104
  # need db access for the keys, so delaying all this lookup until call time
113
105
  if ActiveRecord.version.to_s >= "5.1"
@@ -115,7 +107,7 @@ module VirtualAttributes
115
107
  else
116
108
  join_keys = reflection.join_keys(reflection.klass)
117
109
  end
118
- query = query.where(t[join_keys.foreign_key].eq(foreign_table[join_keys.key]))
110
+ query = query.except(:order).where(t[join_keys.foreign_key].eq(foreign_table[join_keys.key]))
119
111
 
120
112
  arel_column = if method_name == :size
121
113
  Arel.star.count
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: 1.1.0
4
+ version: 1.2.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: 2019-04-23 00:00:00.000000000 Z
11
+ date: 2019-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -94,6 +94,7 @@ files:
94
94
  - ".rubocop_cc.yml"
95
95
  - ".rubocop_local.yml"
96
96
  - ".travis.yml"
97
+ - ".yamllint"
97
98
  - Appraisals
98
99
  - CHANGELOG.md
99
100
  - Gemfile
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
144
  version: '0'
144
145
  requirements: []
145
146
  rubyforge_project:
146
- rubygems_version: 2.7.6
147
+ rubygems_version: 2.7.6.2
147
148
  signing_key:
148
149
  specification_version: 4
149
150
  summary: Access non-sql attributes from sql