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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b646edd0e5f82349a4aeb249a12c8b56ee34b6fb1f090351341c1bb429a9edb
4
- data.tar.gz: b5a8aaffeb5eb8fcc403f37dccb08f0fcbd4e51bc20c3e5a6ab63c170cf98b50
3
+ metadata.gz: ae3765d9f17173d90f9e6bbbce91fca4adaf5dbcf2543acacab671cb9ecb577c
4
+ data.tar.gz: ef36c911e89d741eb5f33d9a0bba89b2b866283e4f3d89cc447b089247ce36d4
5
5
  SHA512:
6
- metadata.gz: 40749055a208071322ceaae739c4b93a2df42d1da1a19711fdc42db4dba1724fcb976b9ee876dcce8c4e2c4a72920187c7319e9c0aa6e04c54651767eef8293c
7
- data.tar.gz: 41b2f812a0ba8b6a42e1c2b0bca4d8bc889d119668ef094348216e961c9bf67d8830a8bfffb620f69617e1eced2456af517005ea88908a1ad0eae4fe37ea8cf2
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 25k+ records, process in batches:
336
+ for tables with many records, use `find_each` or `find_in_batches`:
327
337
 
328
338
  ```ruby
329
- # batch processing
330
- offset = 0
331
- loop do
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
@@ -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
- all_attrs = attributes.is_a?(Hash) ? attributes.merge(kwargs) : kwargs
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
- begin
76
- run_callbacks :save do
77
- if new_record?
78
- run_callbacks :create do
79
- super(**options)
80
- end
81
- else
82
- run_callbacks :update do
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) || raise(RecordNotSaved, "Failed to save record")
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AirctiveRecord
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: airctiverecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - 24c02