active_model-relation 0.2.0 → 0.2.2

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: f0321b5b92acdc5dd26dae064782f042c36201d3f5f3a956497d0719e774aaee
4
- data.tar.gz: f46553ea8591a73af03326c41c3ada13454f7ae9f508382bfeef1788bdbb178c
3
+ metadata.gz: 1b5bc9df824181a3efcd1ee2fc98409ae717a7fd13913e303fad62ddf0a173d1
4
+ data.tar.gz: d068bea29b6292db57ddd65f5144e62b9cba71f4b4d092cbe53989aada2dea2f
5
5
  SHA512:
6
- metadata.gz: ab15a40f58b9a295841a8740a9b4465c4a872db3d64a1d9e03b69ff6c018b5716af356cf65d64ed8b2e8d8037ee601f8eac4e5d9d33a911257a1f2db7e447152
7
- data.tar.gz: 4f2fad7e6ed5b5f365aec277d438fb9d22c6bdce031adf3b42d22e36ee5c297eaee14d655f1c8c6fcdfda5746655838f51d4454261d146d654bc73e874e52db1
6
+ metadata.gz: 838e952ce7433951db4fe5b530c3e6686be5a89c27e39872ec5d37e74ea79596a9da433df75238b4d82e1a1346b0b31e9d5571e393b7c6ad4fb6164571439d63
7
+ data.tar.gz: 4ffcdb4c54fd3cf481c8a11f29a2676aefeac00e2b208f8baab2c67bcd3a471b6b9e2e84b873a2f9d5478f7d490cc5edc415ecd642dabf22dd38098376ec893f
data/CHANGELOG.md CHANGED
@@ -1,8 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ - Treat `ActiveModel::RecordNotFound` like `ActiveRecord::RecordNotFound` in `ActionDispatch`
4
+ - Properly type cast values when they are defined as attribute
5
+ - Ensure that there is always at least an empty array of records
6
+
3
7
  ## [0.2.0] - 2024-09-16
4
8
 
5
- - Rename `ActiveModel::Relation::ModelNotFound` to `ActiveModel::Relation::RecordNotFound`
9
+ - Rename `ActiveModel::ModelNotFound` to `ActiveModel::RecordNotFound`
6
10
  - Allow creating a `ActiveModel::Relation` without passing a collection
7
11
  - Don't require a `.records` class method on model classes
8
12
  - Allow passing a block to `ActiveModel::Relation#find`
data/README.md CHANGED
@@ -51,7 +51,7 @@ An `ActiveModel::Relation` can be queried almost exactly like an `ActiveRecord::
51
51
 
52
52
  #### `#find`
53
53
 
54
- You can look up a record by it's primary key, using the `find` method. If no record is found, it will raise a `ActiveModel::Relation::RecordNotFound` error.
54
+ You can look up a record by it's primary key, using the `find` method. If no record is found, it will raise a `ActiveModel::RecordNotFound` error.
55
55
 
56
56
  ```ruby
57
57
  project = relation.find(1)
@@ -268,3 +268,11 @@ The gem is available as open source under the terms of the [MIT License](https:/
268
268
  ## Code of Conduct
269
269
 
270
270
  Everyone interacting in the ActiveModel::Relation project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/userlist/active_model-relation/blob/main/CODE_OF_CONDUCT.md).
271
+
272
+ ## What is Userlist?
273
+
274
+ [![Userlist](https://userlist.com/images/external/userlist-logo-github.svg)](https://userlist.com/)
275
+
276
+ [Userlist](https://userlist.com/) allows you to onboard and engage your SaaS users with targeted behavior-based campaigns using email or in-app messages.
277
+
278
+ Userlist was started in 2017 as an alternative to bulky enterprise messaging tools. We believe that running SaaS products should be more enjoyable. Learn more [about us](https://userlist.com/about-us/).
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ module ActiveModel
6
+ class Relation
7
+ class Railtie < Rails::Railtie # :nodoc:
8
+ config.action_dispatch.rescue_responses.merge!(
9
+ 'ActiveModel::RecordNotFound' => :not_found
10
+ )
11
+ end
12
+ end
13
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveModel
4
4
  class Relation
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_model'
4
+ require_relative 'relation/railtie' if defined?(Rails::Railtie)
4
5
 
5
6
  module ActiveModel
6
7
  class RecordNotFound < StandardError
@@ -29,9 +30,9 @@ module ActiveModel
29
30
 
30
31
  delegate :each, :size, :last, to: :records
31
32
 
32
- def initialize(model, records = model.try(:records) || [])
33
+ def initialize(model, records = model.try(:records))
33
34
  @model = model
34
- @records = records
35
+ @records = records || []
35
36
  @where_clause = WhereClause.new
36
37
  @order_clause = OrderClause.new
37
38
  @offset_value = nil
@@ -49,7 +50,7 @@ module ActiveModel
49
50
  end
50
51
 
51
52
  def find_by(attributes = {})
52
- where_clause = self.where_clause + WhereClause.from_hash(attributes)
53
+ where_clause = self.where_clause + WhereClause.from_hash(type_cast_values(attributes))
53
54
 
54
55
  records.find(&where_clause)
55
56
  end
@@ -61,7 +62,7 @@ module ActiveModel
61
62
  def where!(attributes = {}, &)
62
63
  return WhereChain.new(spawn) unless attributes.any? || block_given?
63
64
 
64
- self.where_clause += WhereClause.build(attributes, &)
65
+ self.where_clause += WhereClause.build(type_cast_values(attributes), &)
65
66
  self
66
67
  end
67
68
 
@@ -180,5 +181,14 @@ module ActiveModel
180
181
  relation.limit_value = values[:limit]
181
182
  end
182
183
  end
184
+
185
+ def type_cast_values(attributes)
186
+ attributes.to_h do |key, value|
187
+ type = model.try(:type_for_attribute, key)
188
+ value = type.cast(value) if type
189
+
190
+ [key, value]
191
+ end
192
+ end
183
193
  end
184
194
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model-relation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-16 00:00:00.000000000 Z
11
+ date: 2024-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '7.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '7.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.1'
27
33
  description: This library allows querying of collections of Ruby objects, with a similar
28
34
  interface to ActiveRecord::Relation.
29
35
  email:
@@ -43,6 +49,7 @@ files:
43
49
  - lib/active_model/relation/model.rb
44
50
  - lib/active_model/relation/order_clause.rb
45
51
  - lib/active_model/relation/querying.rb
52
+ - lib/active_model/relation/railtie.rb
46
53
  - lib/active_model/relation/scoping.rb
47
54
  - lib/active_model/relation/version.rb
48
55
  - lib/active_model/relation/where_chain.rb