recordables 0.2.0 → 0.2.1

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: 7355a9bfb7d1440a8b7746b13b4fa810304eb837c97d8dc850011d12091241b6
4
- data.tar.gz: 4f13296e03d0467244273af7f96566d21129d48013ec3279d2967236806181a5
3
+ metadata.gz: 2a183a373143cdec754a8704fb66131ddc8980930ebb3a6e16d7a42309bcdd1f
4
+ data.tar.gz: eb62d04c3f09067fa95221810da3cdcb4acdf704052ddd88059893735e7b521d
5
5
  SHA512:
6
- metadata.gz: 8053edc2e2c5cfa59c71c19658f2faba977bbc29e48b933d6eccd3086a548bb5d76ebcb6a0f65e8e8468f60694587a9c47b76ce2ee8c47a286f3fb631318868b
7
- data.tar.gz: 504f8827f44890f43442e17f3af80874feb8dcdd88685f362e7e6bd78a00266e2e7710c387ad331480bc67e741facffdc897d97b9bc66eba13749f80b306fcab
6
+ metadata.gz: 33f79c5525fce7dc3cf40c345c77f38d99f7a511cafca9c7be1b5412b5177ca6dd3722346e7b3365ca7e2c71eb1fbc1d9c8c70ca03de34d6a78f0faacb3c2158
7
+ data.tar.gz: 60c51aa0167f1ac34132cab455cbace4275635f8be85157ac183f5394b26062e3e6506373c8fa7e9d59b6ec6b59bda517f5f1203fd14a8fada865b1e66a90eab
data/CHANGELOG.md CHANGED
@@ -8,6 +8,18 @@ All notable changes to this project are documented here. The format follows
8
8
 
9
9
  ### Added
10
10
 
11
+ - `nested_recordable_attributes_for` accepts `recording_attributes:`, an optional proc
12
+ called once per new child (with the parent record) to build extra attributes for that
13
+ child's Recording. Without it, `apply_#{plural}_attributes!` creating a new child had
14
+ no way to set anything beyond `actor:` and the child's own fields — any host app whose
15
+ Recording has its own required columns (an `account_id` or similar tenant column is
16
+ the common case) hit a hard `NOT NULL` failure the moment a form actually submitted a
17
+ new child row.
18
+
19
+ ## [0.2.0]
20
+
21
+ ### Added
22
+
11
23
  - `trashable` — `default_scope` + `with_trashed` for a recordable type whose Recording
12
24
  can be trashed, so a trashed row stops showing up in normal queries. Paired with
13
25
  `Recording.active` and `Recording#trash!`.
@@ -81,12 +81,17 @@ module Recordables
81
81
  # class RoutineTemplate < ApplicationRecord
82
82
  # recordable
83
83
  # has_children :task_templates, class_name: "TaskTemplate"
84
- # nested_recordable_attributes_for :task_templates, class_name: "TaskTemplate"
84
+ # nested_recordable_attributes_for :task_templates, class_name: "TaskTemplate",
85
+ # recording_attributes: ->(template) { { account: template.account } }
85
86
  # end
86
- def nested_recordable_attributes_for(plural_name, class_name: nil)
87
+ #
88
+ # recording_attributes: only needed if the host app's Recording has its
89
+ # own required columns (an account_id or similar tenant column is the
90
+ # common case) — see Recordables::NestedRecordableAttributes.
91
+ def nested_recordable_attributes_for(plural_name, class_name: nil, recording_attributes: nil)
87
92
  include Recordables::NestedRecordableAttributes
88
93
  target_class_name = class_name&.to_s || plural_name.to_s.classify
89
- __recordables_define_nested_attributes(plural_name, class_name: target_class_name)
94
+ __recordables_define_nested_attributes(plural_name, class_name: target_class_name, recording_attributes: recording_attributes)
90
95
  end
91
96
 
92
97
  # A belongs_to whose target is trashable, resolved through the append-only
@@ -7,9 +7,17 @@ module Recordables
7
7
  # class RoutineTemplate < ApplicationRecord
8
8
  # recordable
9
9
  # has_children :task_templates, class_name: "TaskTemplate"
10
- # nested_recordable_attributes_for :task_templates, class_name: "TaskTemplate"
10
+ # nested_recordable_attributes_for :task_templates, class_name: "TaskTemplate",
11
+ # recording_attributes: ->(template) { { account: template.account } }
11
12
  # end
12
13
  #
14
+ # recording_attributes: is only needed if the host app's Recording has its
15
+ # own required columns beyond what the gem's own template generates (an
16
+ # account_id or similar tenant column is the common case) — a new child's
17
+ # Recording needs those set too, and there's no attribute on the
18
+ # submitted form row itself to read them from. Called once per new child,
19
+ # with the parent record, right before creating it.
20
+ #
13
21
  # accepts_nested_attributes_for can't work here: it writes at
14
22
  # assign_attributes time, straight to a real association's rows — but
15
23
  # task_templates isn't a real has_many (see HasChildren), and a child
@@ -30,7 +38,7 @@ module Recordables
30
38
  extend ActiveSupport::Concern
31
39
 
32
40
  class_methods do
33
- def __recordables_define_nested_attributes(plural_name, class_name:)
41
+ def __recordables_define_nested_attributes(plural_name, class_name:, recording_attributes:)
34
42
  singular_name = plural_name.to_s.singularize
35
43
  target_class_name = class_name.to_s
36
44
  pending_ivar = :"@pending_#{plural_name}_attributes"
@@ -88,7 +96,8 @@ module Recordables
88
96
  child.recording.revise(actor: actor, **changes)
89
97
  end
90
98
  elsif !destroy && changes.present?
91
- public_send(:"add_#{singular_name}", actor: actor, **changes)
99
+ extra_recording_attrs = recording_attributes ? recording_attributes.call(self) : {}
100
+ public_send(:"add_#{singular_name}", actor: actor, recording: extra_recording_attrs, **changes)
92
101
  end
93
102
  end
94
103
 
@@ -1,3 +1,3 @@
1
1
  module Recordables
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recordables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Medeiros