active_hash 3.2.0 → 3.3.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: 11f2e4807b50cf0708d0a9ca9bea56e9a26bc9b7ca7eb2e81e0a93f88c333c7e
4
- data.tar.gz: ba9dee0fea4fa0e02b8ebc2eae74839e72f1e2ea1fd262606561e42aac10ecf1
3
+ metadata.gz: f255dca493bb6e5cb46acba485f7df5af6ebfef6a708abf30757a3e39db1bb62
4
+ data.tar.gz: a5c7bcc8a8f85ab74395b7554c39cb33023a30dd592d68d9a9b5664d02d9e233
5
5
  SHA512:
6
- metadata.gz: 1f7d4c59c9087aaddd26c05295507850f9720ba14b60cdfeb4c77bfbcefd43d982fb052212dc89e6fd5c0d753de047d512d1ce7d5ce3644dc9dd89efa166066d
7
- data.tar.gz: ec31d53f5622c6b2a8958656b9ac035d00c2484b7d7ee8cd6b486ad6a9c1351ad1d55fbda487025b268ee708688ba832b0513189b91db773956eaea5ba2c2737
6
+ metadata.gz: bebafa5e913c561fbe97a1ba997df579072db9153be012773c2fcfdd33062cdd033b417b47ea5a6de5c234736b575b2c1d307185b2b7570c31ce825da107cd61
7
+ data.tar.gz: 439923f589dc14098c4b278ccee6ee0f66de3c17e67f5f41c8d0f2f6ccf706d93311582d3daadcb296c0de9afe97044b22f2f6136be1dd3bba6c93f03fdecd7a
data/CHANGELOG.md CHANGED
@@ -1,6 +1,36 @@
1
1
  # active_hash Changelog
2
2
 
3
- ## Version [3.2.0] - <sub><sup>2022-07-14</sup></sub>
3
+ ## Version [3.3.0] - <sub><sup>2024-04-30</sup></sub>
4
+
5
+ ### Added
6
+
7
+ - Ruby 3.3 support [#298](https://github.com/active-hash/active_hash/pull/298) @m-nakamura145
8
+ - Support `has_many :through` associations [#296](https://github.com/active-hash/active_hash/pull/296) @flavorjones
9
+ - Rails 7.1 support [#291](https://github.com/active-hash/active_hash/pull/281) @y-yagi
10
+
11
+ ### Fixed
12
+
13
+ - Rails 7.1: fix sqlite3 issue [#303](https://github.com/active-hash/active_hash/pull/303) @flavorjones
14
+ - Rails 7.1.3: add missing `has_query_constraints?` [#300](https://github.com/active-hash/active_hash/pull/300) @flavorjones
15
+ - `Array#pluck` supports methods [#299](https://github.com/active-hash/active_hash/pull/299) @iberianpig
16
+ - Prefer `safe_constantize` over `constantize` [#297](https://github.com/active-hash/active_hash/pull/297) @flavorjones
17
+ - Treat `nil` and `blank?` as different values [#295](https://github.com/active-hash/active_hash/pull/295) @kbrock
18
+ - Fix `#where` for string keys [#292](https://github.com/active-hash/active_hash/pull/292) @usernam3
19
+
20
+ ## Version [3.2.1] - <sub><sup>2023-08-31</sup></sub>
21
+
22
+ ### Added
23
+
24
+ - Improve `pp` output for `ActiveHash::Relation`. [#288](https://github.com/active-hash/active_hash/pull/288) @flavorjones
25
+
26
+ ### Fixed
27
+
28
+ - Fix relation matching when attribute name collides with a method. [#281](https://github.com/active-hash/active_hash/pull/281) @flavorjones
29
+ - Fix association reflection in applications that don't use ActiveHash::Associations. [#286](https://github.com/active-hash/active_hash/pull/286) @iberianpig
30
+ - Fix `ActiveHash::Relation#method_missing` and `#respond_to_missing?` without scopes. [#278](https://github.com/active-hash/active_hash/pull/278) @julianrubisch
31
+
32
+
33
+ ## Version [3.2.0] - <sub><sup>2023-05-06</sup></sub>
4
34
 
5
35
  - Add Ruby 3.2 to the CI matrix [#275](https://github.com/active-hash/active_hash/pull/275) @petergoldstein
6
36
  - Handle default value of `false` [#274](https://github.com/active-hash/active_hash/pull/274) @ihollander
@@ -292,9 +322,10 @@
292
322
  - Setting data to nil correctly causes .all to return an empty array
293
323
  - Added reload(force) method, so that you can force a reload from files in ActiveFile, useful for tests
294
324
 
295
- [HEAD]: https://github.com/active-hash/active_hash/compare/v4.3.0...HEAD
296
- [4.3.0]: https://github.com/active-hash/active_hash/compare/v3.2.0...v4.3.0
297
- [4.2.0]: https://github.com/active-hash/active_hash/compare/v3.1.1...v4.2.0
325
+ [HEAD]: https://github.com/active-hash/active_hash/compare/v3.3.0...HEAD
326
+ [3.3.0]: https://github.com/active-hash/active_hash/compare/v3.2.1...v3.3.0
327
+ [3.2.1]: https://github.com/active-hash/active_hash/compare/v3.2.0...v3.2.1
328
+ [3.2.0]: https://github.com/active-hash/active_hash/compare/v3.1.1...v3.2.0
298
329
  [3.1.1]: https://github.com/active-hash/active_hash/compare/v3.1.0...v3.1.1
299
330
  [3.1.0]: https://github.com/active-hash/active_hash/compare/v3.0.0...v3.1.0
300
331
  [3.0.0]: https://github.com/active-hash/active_hash/compare/v2.3.0...v3.0.0
@@ -127,6 +127,10 @@ module ActiveHash
127
127
  @record_index ||= {}
128
128
  end
129
129
 
130
+ def has_query_constraints?
131
+ false
132
+ end
133
+
130
134
  private :record_index
131
135
 
132
136
  def reset_record_index
@@ -349,6 +353,11 @@ module ActiveHash
349
353
  base_class.name
350
354
  end
351
355
 
356
+ # Needed for ActiveRecord since rails/rails#47664
357
+ def composite_primary_key?
358
+ false
359
+ end
360
+
352
361
  def reload
353
362
  reset_record_index
354
363
  self.data = _data
@@ -406,7 +415,7 @@ module ActiveHash
406
415
  end
407
416
 
408
417
  def _read_attribute(key)
409
- attributes[key]
418
+ attributes[key.to_sym]
410
419
  end
411
420
  alias_method :read_attribute, :_read_attribute
412
421
 
@@ -19,7 +19,7 @@ class ActiveHash::Relation::Condition
19
19
  expectation_method = inverted ? :any? : :all?
20
20
 
21
21
  constraints.send(expectation_method) do |attribute, expected|
22
- value = record.public_send(attribute)
22
+ value = record.read_attribute(attribute)
23
23
 
24
24
  matches_value?(value, expected)
25
25
  end
@@ -39,6 +39,6 @@ class ActiveHash::Relation::Condition
39
39
  end
40
40
 
41
41
  def normalize(value)
42
- value.respond_to?(:to_s) ? value.to_s : value
42
+ value.respond_to?(:to_s) ? value&.to_s : value
43
43
  end
44
- end
44
+ end
@@ -22,6 +22,10 @@ module ActiveHash
22
22
  spawn.where!(conditions_hash)
23
23
  end
24
24
 
25
+ def pretty_print(pp)
26
+ pp.pp(entries.to_ary)
27
+ end
28
+
25
29
  class WhereChain
26
30
  attr_reader :relation
27
31
 
@@ -139,15 +143,11 @@ module ActiveHash
139
143
  end
140
144
 
141
145
  def pluck(*column_names)
142
- symbolized_column_names = column_names.map(&:to_sym)
143
-
144
- if symbolized_column_names.length == 1
145
- column_name = symbolized_column_names.first
146
- all.map { |record| record[column_name] }
146
+ if column_names.length == 1
147
+ column_name = column_names.first
148
+ all.map { |record| record.public_send(column_name) }
147
149
  else
148
- all.map do |record|
149
- symbolized_column_names.map { |column_name| record[column_name] }
150
- end
150
+ all.map { |record| column_names.map { |column_name| record.public_send(column_name) } }
151
151
  end
152
152
  end
153
153
 
@@ -164,13 +164,13 @@ module ActiveHash
164
164
  end
165
165
 
166
166
  def method_missing(method_name, *args)
167
- return super unless klass.scopes.key?(method_name)
167
+ return super unless klass.scopes&.key?(method_name)
168
168
 
169
169
  instance_exec(*args, &klass.scopes[method_name])
170
170
  end
171
171
 
172
172
  def respond_to_missing?(method_name, include_private = false)
173
- klass.scopes.key?(method_name) || super
173
+ klass.scopes&.key?(method_name) || super
174
174
  end
175
175
 
176
176
  private
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "3.2.0"
3
+ VERSION = "3.3.0"
4
4
  end
5
5
  end
@@ -2,12 +2,40 @@ module ActiveHash
2
2
  module Associations
3
3
 
4
4
  module ActiveRecordExtensions
5
+ def self.extended(base)
6
+ require_relative 'reflection_extensions'
7
+ end
8
+
9
+ def has_many(association_id, **options)
10
+ if options[:through]
11
+ klass_name = association_id.to_s.classify
12
+ klass =
13
+ begin
14
+ klass_name.safe_constantize
15
+ rescue StandardError, LoadError
16
+ nil
17
+ end
18
+
19
+ if klass && klass < ActiveHash::Base
20
+ define_method(association_id) do
21
+ join_models = send(options[:through])
22
+ join_models.flat_map do |join_model|
23
+ join_model.send(association_id.to_s.singularize)
24
+ end.uniq
25
+ end
26
+
27
+ return
28
+ end
29
+ end
30
+
31
+ super
32
+ end
5
33
 
6
34
  def belongs_to(name, scope = nil, **options)
7
35
  klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize
8
36
  klass =
9
37
  begin
10
- klass_name.constantize
38
+ klass_name.safe_constantize
11
39
  rescue StandardError, LoadError
12
40
  nil
13
41
  end
@@ -27,11 +55,11 @@ module ActiveHash
27
55
  :shortcuts => []
28
56
  }.merge(options)
29
57
  # Define default primary_key with provided class_name if any
30
- options[:primary_key] ||= options[:class_name].constantize.primary_key
58
+ options[:primary_key] ||= options[:class_name].safe_constantize.primary_key
31
59
  options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array)
32
60
 
33
61
  define_method(association_id) do
34
- options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
62
+ options[:class_name].safe_constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
35
63
  end
36
64
 
37
65
  define_method("#{association_id}=") do |new_value|
@@ -44,16 +72,13 @@ module ActiveHash
44
72
  end
45
73
 
46
74
  define_method("#{association_id}_#{shortcut}=") do |new_value|
47
- send "#{association_id}=", new_value ? options[:class_name].constantize.send("find_by_#{shortcut}", new_value) : nil
75
+ send "#{association_id}=", new_value ? options[:class_name].safe_constantize.send("find_by_#{shortcut}", new_value) : nil
48
76
  end
49
77
  end
50
78
 
51
79
  if ActiveRecord::Reflection.respond_to?(:create)
52
80
  if defined?(ActiveHash::Reflection::BelongsToReflection)
53
81
  reflection = ActiveHash::Reflection::BelongsToReflection.new(association_id.to_sym, nil, options, self)
54
- if options[:through]
55
- reflection = ActiveRecord::ThroughReflection.new(reflection)
56
- end
57
82
  else
58
83
  reflection = ActiveRecord::Reflection.create(
59
84
  :belongs_to,
@@ -84,7 +109,7 @@ module ActiveHash
84
109
  :belongs_to,
85
110
  association_id.to_sym,
86
111
  options,
87
- options[:class_name].constantize
112
+ options[:class_name].safe_constantize
88
113
  )
89
114
  end
90
115
  end
@@ -92,7 +117,6 @@ module ActiveHash
92
117
  end
93
118
 
94
119
  def self.included(base)
95
- require_relative "reflection_extensions"
96
120
  base.extend Methods
97
121
  end
98
122
 
@@ -105,7 +129,7 @@ module ActiveHash
105
129
  :primary_key => self.class.primary_key
106
130
  }.merge(options)
107
131
 
108
- klass = options[:class_name].constantize
132
+ klass = options[:class_name].safe_constantize
109
133
  primary_key_value = send(options[:primary_key])
110
134
  foreign_key = options[:foreign_key].to_sym
111
135
 
@@ -117,6 +141,7 @@ module ActiveHash
117
141
  klass.where(foreign_key => primary_key_value)
118
142
  end
119
143
  end
144
+
120
145
  define_method("#{association_id.to_s.underscore.singularize}_ids") do
121
146
  public_send(association_id).map(&:id)
122
147
  end
@@ -130,7 +155,7 @@ module ActiveHash
130
155
  :primary_key => self.class.primary_key
131
156
  }.merge(options)
132
157
 
133
- scope = options[:class_name].constantize
158
+ scope = options[:class_name].safe_constantize
134
159
 
135
160
  if scope.respond_to?(:scoped) && options[:conditions]
136
161
  scope = scope.scoped(:conditions => options[:conditions])
@@ -140,7 +165,6 @@ module ActiveHash
140
165
  end
141
166
 
142
167
  def belongs_to(association_id, options = {})
143
-
144
168
  options = {
145
169
  :class_name => association_id.to_s.classify,
146
170
  :foreign_key => association_id.to_s.foreign_key,
@@ -150,13 +174,12 @@ module ActiveHash
150
174
  field options[:foreign_key].to_sym
151
175
 
152
176
  define_method(association_id) do
153
- options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
177
+ options[:class_name].safe_constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key]))
154
178
  end
155
179
 
156
180
  define_method("#{association_id}=") do |new_value|
157
181
  attributes[options[:foreign_key].to_sym] = new_value ? new_value.send(options[:primary_key]) : nil
158
182
  end
159
-
160
183
  end
161
184
  end
162
185
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -29,7 +29,7 @@ authors:
29
29
  autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
- date: 2023-05-06 00:00:00.000000000 Z
32
+ date: 2024-04-29 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: activesupport
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.2.33
111
+ rubygems_version: 3.5.9
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: An ActiveRecord-like model that uses a hash or file as a datasource