active_model-relation 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +9 -1
- data/lib/active_model/relation/railtie.rb +13 -0
- data/lib/active_model/relation/version.rb +1 -1
- data/lib/active_model/relation.rb +14 -4
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 317f4bd9c497d254fabc65f125c743badd36fe4f91e41876bd40819f77703e01
|
4
|
+
data.tar.gz: 937df0db69de0e3983531fae564b0cefa8efbb3cc9d9aac499238a601d9e6503
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f147ee5f25e5b5cd8570dae9d0117c4cbbf015491d8820ae07cf6cca831075d3c9a70f5cc0ec0bf7a15dd522bd691103fa12ee9b6e9b3dcc9a12484ca0761df3
|
7
|
+
data.tar.gz: d61a00b7833c030be0ca14d814f8aeb8e0f23d913e071dd068a09fac050106ca1f3c46cd80b8dea597c21632397fec4d0ccd276cfb7193b29e09b73d1b2ebe68
|
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::
|
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::
|
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/).
|
@@ -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.
|
4
|
+
version: 0.2.1
|
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-
|
11
|
+
date: 2024-11-08 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.0'
|
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.0'
|
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
|