active_hash 3.2.1 → 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: 993ef228f8c89c89dc8f18087b7ed609dc575e3e6031abd82d18673322d42d0d
4
- data.tar.gz: 8871a88db27cc363de8d813d98a1d3f354d83c0cc76c0f0a03fb093e5d8a5226
3
+ metadata.gz: f255dca493bb6e5cb46acba485f7df5af6ebfef6a708abf30757a3e39db1bb62
4
+ data.tar.gz: a5c7bcc8a8f85ab74395b7554c39cb33023a30dd592d68d9a9b5664d02d9e233
5
5
  SHA512:
6
- metadata.gz: e4c19f95913358f7a7c46e76871bd3845e7e61d82515e725933f162d5735b5fae5f52aeb376b7a61ae9459640692b335f11312b2ae00d43ba353b55cd3358638
7
- data.tar.gz: 07a92392da1c142801aa2f5ed60ffa0b1e7ee8a8638096b7f4dd707d4d98c767082e95cfbc3cebce0da7e189a71246bb7d1a349a730d11cf8e146171cea060e2
6
+ metadata.gz: bebafa5e913c561fbe97a1ba997df579072db9153be012773c2fcfdd33062cdd033b417b47ea5a6de5c234736b575b2c1d307185b2b7570c31ce825da107cd61
7
+ data.tar.gz: 439923f589dc14098c4b278ccee6ee0f66de3c17e67f5f41c8d0f2f6ccf706d93311582d3daadcb296c0de9afe97044b22f2f6136be1dd3bba6c93f03fdecd7a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # active_hash Changelog
2
2
 
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
+
3
20
  ## Version [3.2.1] - <sub><sup>2023-08-31</sup></sub>
4
21
 
5
22
  ### Added
@@ -305,9 +322,10 @@
305
322
  - Setting data to nil correctly causes .all to return an empty array
306
323
  - Added reload(force) method, so that you can force a reload from files in ActiveFile, useful for tests
307
324
 
308
- [HEAD]: https://github.com/active-hash/active_hash/compare/v4.3.0...HEAD
309
- [4.3.0]: https://github.com/active-hash/active_hash/compare/v3.2.0...v4.3.0
310
- [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
311
329
  [3.1.1]: https://github.com/active-hash/active_hash/compare/v3.1.0...v3.1.1
312
330
  [3.1.0]: https://github.com/active-hash/active_hash/compare/v3.0.0...v3.1.0
313
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
 
@@ -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
44
  end
@@ -143,15 +143,11 @@ module ActiveHash
143
143
  end
144
144
 
145
145
  def pluck(*column_names)
146
- symbolized_column_names = column_names.map(&:to_sym)
147
-
148
- if symbolized_column_names.length == 1
149
- column_name = symbolized_column_names.first
150
- 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) }
151
149
  else
152
- all.map do |record|
153
- symbolized_column_names.map { |column_name| record[column_name] }
154
- end
150
+ all.map { |record| column_names.map { |column_name| record.public_send(column_name) } }
155
151
  end
156
152
  end
157
153
 
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "3.2.1"
3
+ VERSION = "3.3.0"
4
4
  end
5
5
  end
@@ -2,16 +2,40 @@ module ActiveHash
2
2
  module Associations
3
3
 
4
4
  module ActiveRecordExtensions
5
-
6
5
  def self.extended(base)
7
6
  require_relative 'reflection_extensions'
8
7
  end
9
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
33
+
10
34
  def belongs_to(name, scope = nil, **options)
11
35
  klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize
12
36
  klass =
13
37
  begin
14
- klass_name.constantize
38
+ klass_name.safe_constantize
15
39
  rescue StandardError, LoadError
16
40
  nil
17
41
  end
@@ -31,11 +55,11 @@ module ActiveHash
31
55
  :shortcuts => []
32
56
  }.merge(options)
33
57
  # Define default primary_key with provided class_name if any
34
- options[:primary_key] ||= options[:class_name].constantize.primary_key
58
+ options[:primary_key] ||= options[:class_name].safe_constantize.primary_key
35
59
  options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array)
36
60
 
37
61
  define_method(association_id) do
38
- 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]))
39
63
  end
40
64
 
41
65
  define_method("#{association_id}=") do |new_value|
@@ -48,16 +72,13 @@ module ActiveHash
48
72
  end
49
73
 
50
74
  define_method("#{association_id}_#{shortcut}=") do |new_value|
51
- 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
52
76
  end
53
77
  end
54
78
 
55
79
  if ActiveRecord::Reflection.respond_to?(:create)
56
80
  if defined?(ActiveHash::Reflection::BelongsToReflection)
57
81
  reflection = ActiveHash::Reflection::BelongsToReflection.new(association_id.to_sym, nil, options, self)
58
- if options[:through]
59
- reflection = ActiveRecord::ThroughReflection.new(reflection)
60
- end
61
82
  else
62
83
  reflection = ActiveRecord::Reflection.create(
63
84
  :belongs_to,
@@ -88,7 +109,7 @@ module ActiveHash
88
109
  :belongs_to,
89
110
  association_id.to_sym,
90
111
  options,
91
- options[:class_name].constantize
112
+ options[:class_name].safe_constantize
92
113
  )
93
114
  end
94
115
  end
@@ -108,7 +129,7 @@ module ActiveHash
108
129
  :primary_key => self.class.primary_key
109
130
  }.merge(options)
110
131
 
111
- klass = options[:class_name].constantize
132
+ klass = options[:class_name].safe_constantize
112
133
  primary_key_value = send(options[:primary_key])
113
134
  foreign_key = options[:foreign_key].to_sym
114
135
 
@@ -120,6 +141,7 @@ module ActiveHash
120
141
  klass.where(foreign_key => primary_key_value)
121
142
  end
122
143
  end
144
+
123
145
  define_method("#{association_id.to_s.underscore.singularize}_ids") do
124
146
  public_send(association_id).map(&:id)
125
147
  end
@@ -133,7 +155,7 @@ module ActiveHash
133
155
  :primary_key => self.class.primary_key
134
156
  }.merge(options)
135
157
 
136
- scope = options[:class_name].constantize
158
+ scope = options[:class_name].safe_constantize
137
159
 
138
160
  if scope.respond_to?(:scoped) && options[:conditions]
139
161
  scope = scope.scoped(:conditions => options[:conditions])
@@ -143,7 +165,6 @@ module ActiveHash
143
165
  end
144
166
 
145
167
  def belongs_to(association_id, options = {})
146
-
147
168
  options = {
148
169
  :class_name => association_id.to_s.classify,
149
170
  :foreign_key => association_id.to_s.foreign_key,
@@ -153,13 +174,12 @@ module ActiveHash
153
174
  field options[:foreign_key].to_sym
154
175
 
155
176
  define_method(association_id) do
156
- 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]))
157
178
  end
158
179
 
159
180
  define_method("#{association_id}=") do |new_value|
160
181
  attributes[options[:foreign_key].to_sym] = new_value ? new_value.send(options[:primary_key]) : nil
161
182
  end
162
-
163
183
  end
164
184
  end
165
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.1
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -26,10 +26,10 @@ authors:
26
26
  - Brett Richardson
27
27
  - Rachel Heaton
28
28
  - Keisuke Izumiya
29
- autorequire:
29
+ autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
- date: 2023-10-12 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
@@ -93,7 +93,7 @@ metadata:
93
93
  changelog_uri: https://github.com/active-hash/active_hash/blob/master/CHANGELOG.md
94
94
  source_code_uri: http://github.com/active-hash/active_hash
95
95
  bug_tracker_uri: https://github.com/active-hash/active_hash/issues
96
- post_install_message:
96
+ post_install_message:
97
97
  rdoc_options: []
98
98
  require_paths:
99
99
  - lib
@@ -108,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.4.19
112
- signing_key:
111
+ rubygems_version: 3.5.9
112
+ signing_key:
113
113
  specification_version: 4
114
114
  summary: An ActiveRecord-like model that uses a hash or file as a datasource
115
115
  test_files: []