airctiverecord 0.2.1 → 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 +4 -4
- data/README.md +22 -8
- data/lib/airctiverecord/base.rb +15 -17
- data/lib/airctiverecord/scoping.rb +8 -0
- data/lib/airctiverecord/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ae3765d9f17173d90f9e6bbbce91fca4adaf5dbcf2543acacab671cb9ecb577c
|
|
4
|
+
data.tar.gz: ef36c911e89d741eb5f33d9a0bba89b2b866283e4f3d89cc447b089247ce36d4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c87e579fc0a84a2acfe7f6b9649606190672efec26d80c769432d5e135e80881cb3141410d8a0a3fabad55f48843883a345f667c08673889cae96fdcace44bc8
|
|
7
|
+
data.tar.gz: a110512bd802d6e5181bc982874ffce213a7c01f4055211a795deacaf0f888797cd68663fc7bdb3585318c80d2c182b93b125ba60f5b9a1f7be54ed754911004
|
data/README.md
CHANGED
|
@@ -106,6 +106,16 @@ user = User.first
|
|
|
106
106
|
user = User.find_by(email: "alice@example.com")
|
|
107
107
|
user = User.find_by!(email: "alice@example.com") # raises if not found
|
|
108
108
|
|
|
109
|
+
# find or create
|
|
110
|
+
user = User.find_or_create_by(email: "alice@example.com")
|
|
111
|
+
user = User.find_or_create_by!(email: "alice@example.com") # raises on validation error
|
|
112
|
+
|
|
113
|
+
# block is yielded to the new record only (not if found)
|
|
114
|
+
user = User.find_or_create_by(email: "alice@example.com") do |u|
|
|
115
|
+
u.first_name = "Alice"
|
|
116
|
+
u.role = "admin"
|
|
117
|
+
end
|
|
118
|
+
|
|
109
119
|
# update
|
|
110
120
|
user.update(first_name: "Alicia")
|
|
111
121
|
user.first_name = "Alicia"
|
|
@@ -323,17 +333,21 @@ User.first(10) # limit(10)
|
|
|
323
333
|
|
|
324
334
|
**large tables**
|
|
325
335
|
|
|
326
|
-
for tables with
|
|
336
|
+
for tables with many records, use `find_each` or `find_in_batches`:
|
|
327
337
|
|
|
328
338
|
```ruby
|
|
329
|
-
#
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
batch = User.limit(100).offset(offset).to_a
|
|
333
|
-
break if batch.empty?
|
|
334
|
-
batch.each { |user| process(user) }
|
|
335
|
-
offset += 100
|
|
339
|
+
# process records one at a time (fetches in batches of 100 internally)
|
|
340
|
+
User.where(active: true).find_each do |user|
|
|
341
|
+
process(user)
|
|
336
342
|
end
|
|
343
|
+
|
|
344
|
+
# process records in batches
|
|
345
|
+
User.find_in_batches(batch_size: 50) do |batch|
|
|
346
|
+
bulk_import(batch)
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# works with scopes and conditions
|
|
350
|
+
User.active.admins.find_each(batch_size: 50) { |user| sync(user) }
|
|
337
351
|
```
|
|
338
352
|
|
|
339
353
|
## license
|
data/lib/airctiverecord/base.rb
CHANGED
|
@@ -35,8 +35,10 @@ module AirctiveRecord
|
|
|
35
35
|
id = kwargs.delete(:id)
|
|
36
36
|
created_at = kwargs.delete(:created_at)
|
|
37
37
|
|
|
38
|
-
# Merge positional hash and kwargs to handle both styles
|
|
39
|
-
|
|
38
|
+
# Merge positional hash and kwargs to handle both styles.
|
|
39
|
+
# Call to_h to handle ActionController::Parameters, which is not a Hash subclass.
|
|
40
|
+
attr_hash = attributes.respond_to?(:to_h) ? attributes.to_h : {}
|
|
41
|
+
all_attrs = attr_hash.merge(kwargs)
|
|
40
42
|
|
|
41
43
|
# Norairrecord::Table expects field names as STRING keys
|
|
42
44
|
# We need to convert Ruby attribute names to Airtable field names
|
|
@@ -72,29 +74,25 @@ module AirctiveRecord
|
|
|
72
74
|
def save(**options)
|
|
73
75
|
return false unless valid?
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
super(**options)
|
|
84
|
-
end
|
|
77
|
+
run_callbacks :save do
|
|
78
|
+
if new_record?
|
|
79
|
+
run_callbacks :create do
|
|
80
|
+
super(**options)
|
|
81
|
+
end
|
|
82
|
+
else
|
|
83
|
+
run_callbacks :update do
|
|
84
|
+
super(**options)
|
|
85
85
|
end
|
|
86
86
|
end
|
|
87
|
-
changes_applied
|
|
88
|
-
true
|
|
89
|
-
rescue StandardError
|
|
90
|
-
false
|
|
91
87
|
end
|
|
88
|
+
changes_applied
|
|
89
|
+
true
|
|
92
90
|
end
|
|
93
91
|
|
|
94
92
|
def save!(**options)
|
|
95
93
|
raise RecordInvalid, errors.full_messages.join(", ") unless valid?
|
|
96
94
|
|
|
97
|
-
save(**options)
|
|
95
|
+
save(**options)
|
|
98
96
|
end
|
|
99
97
|
|
|
100
98
|
def update(attributes)
|
|
@@ -36,6 +36,14 @@ module AirctiveRecord
|
|
|
36
36
|
|
|
37
37
|
def find_by!(conditions) = all.find_by!(conditions)
|
|
38
38
|
|
|
39
|
+
def find_or_create_by(conditions, &block)
|
|
40
|
+
find_by(conditions) || new(conditions).tap { |r| block&.call(r) }.tap(&:save)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def find_or_create_by!(conditions, &block)
|
|
44
|
+
find_by(conditions) || new(conditions).tap { |r| block&.call(r) }.tap(&:save!)
|
|
45
|
+
end
|
|
46
|
+
|
|
39
47
|
def first(limit = nil) = all.first(limit)
|
|
40
48
|
|
|
41
49
|
def last(limit = nil) = all.last(limit)
|