familia 2.0.0.pre22 → 2.0.0.pre23
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/.github/workflows/claude-code-review.yml +8 -5
- data/CHANGELOG.rst +33 -0
- data/Gemfile.lock +8 -8
- data/docs/1106-participates_in-bidirectional-solution.md +201 -58
- data/examples/through_relationships.rb +275 -0
- data/lib/familia/features/relationships/README.md +1 -1
- data/lib/familia/features/relationships/participation/participant_methods.rb +59 -10
- data/lib/familia/features/relationships/participation/target_methods.rb +51 -7
- data/lib/familia/features/relationships/participation/through_model_operations.rb +150 -0
- data/lib/familia/features/relationships/participation.rb +39 -15
- data/lib/familia/features/relationships/participation_relationship.rb +19 -1
- data/lib/familia/features/relationships.rb +1 -1
- data/lib/familia/version.rb +1 -1
- data/pr_agent.toml +6 -1
- data/try/features/relationships/participation_commands_verification_spec.rb +1 -1
- data/try/features/relationships/participation_commands_verification_try.rb +1 -1
- data/try/features/relationships/participation_method_prefix_try.rb +133 -0
- data/try/features/relationships/participation_reverse_index_try.rb +1 -1
- data/try/features/relationships/{participation_bidirectional_try.rb → participation_reverse_methods_try.rb} +6 -6
- data/try/features/relationships/participation_through_try.rb +173 -0
- metadata +6 -2
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# lib/familia/features/relationships/participation/through_model_operations.rb
|
|
2
|
+
#
|
|
3
|
+
# frozen_string_literal: true
|
|
4
|
+
|
|
5
|
+
module Familia
|
|
6
|
+
module Features
|
|
7
|
+
module Relationships
|
|
8
|
+
module Participation
|
|
9
|
+
# ThroughModelOperations provides lifecycle management for through models
|
|
10
|
+
# in participation relationships.
|
|
11
|
+
#
|
|
12
|
+
# Through models implement the join table pattern, creating an intermediate
|
|
13
|
+
# object between target and participant that can carry additional attributes
|
|
14
|
+
# (e.g., role, permissions, metadata).
|
|
15
|
+
#
|
|
16
|
+
# Key characteristics:
|
|
17
|
+
# - Deterministic identifier: Built from target, participant, and through class
|
|
18
|
+
# - Auto-lifecycle: Created on add, destroyed on remove
|
|
19
|
+
# - Idempotent: Re-adding updates existing model
|
|
20
|
+
# - Atomic: All operations use transactions
|
|
21
|
+
# - Cache-friendly: Auto-updates updated_at for invalidation
|
|
22
|
+
#
|
|
23
|
+
# Example:
|
|
24
|
+
# class Membership < Familia::Horreum
|
|
25
|
+
# feature :object_identifier
|
|
26
|
+
# field :customer_objid
|
|
27
|
+
# field :domain_objid
|
|
28
|
+
# field :role
|
|
29
|
+
# field :updated_at
|
|
30
|
+
# end
|
|
31
|
+
#
|
|
32
|
+
# class Domain < Familia::Horreum
|
|
33
|
+
# participates_in Customer, :domains, through: :Membership
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# # Through model auto-created with deterministic key
|
|
37
|
+
# customer.add_domains_instance(domain, through_attrs: { role: 'admin' })
|
|
38
|
+
# # => #<Membership objid="customer:123:domain:456:membership">
|
|
39
|
+
#
|
|
40
|
+
module ThroughModelOperations
|
|
41
|
+
module_function
|
|
42
|
+
|
|
43
|
+
# Build a deterministic key for the through model
|
|
44
|
+
#
|
|
45
|
+
# The key format ensures uniqueness and allows direct lookup:
|
|
46
|
+
# {target.prefix}:{target.objid}:{participant.prefix}:{participant.objid}:{through.prefix}
|
|
47
|
+
#
|
|
48
|
+
# @param target [Object] The target instance (e.g., customer)
|
|
49
|
+
# @param participant [Object] The participant instance (e.g., domain)
|
|
50
|
+
# @param through_class [Class] The through model class
|
|
51
|
+
# @return [String] Deterministic key for the through model
|
|
52
|
+
#
|
|
53
|
+
def build_key(target:, participant:, through_class:)
|
|
54
|
+
"#{target.class.config_name}:#{target.objid}:" \
|
|
55
|
+
"#{participant.class.config_name}:#{participant.objid}:" \
|
|
56
|
+
"#{through_class.config_name}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Find or create a through model instance
|
|
60
|
+
#
|
|
61
|
+
# This method is idempotent - calling it multiple times with the same
|
|
62
|
+
# target/participant pair will update the existing through model rather
|
|
63
|
+
# than creating duplicates.
|
|
64
|
+
#
|
|
65
|
+
# The through model's updated_at is set on both create and update for
|
|
66
|
+
# cache invalidation.
|
|
67
|
+
#
|
|
68
|
+
# @param through_class [Class] The through model class
|
|
69
|
+
# @param target [Object] The target instance
|
|
70
|
+
# @param participant [Object] The participant instance
|
|
71
|
+
# @param attrs [Hash] Additional attributes to set on through model
|
|
72
|
+
# @return [Object] The created or updated through model instance
|
|
73
|
+
#
|
|
74
|
+
def find_or_create(through_class:, target:, participant:, attrs: {})
|
|
75
|
+
key = build_key(target: target, participant: participant, through_class: through_class)
|
|
76
|
+
|
|
77
|
+
# Try to load existing model - load returns nil if key doesn't exist
|
|
78
|
+
existing = through_class.load(key)
|
|
79
|
+
|
|
80
|
+
# Check if we got a valid loaded object using the public API
|
|
81
|
+
# This is called outside transaction boundaries (see participant_methods.rb
|
|
82
|
+
# and target_methods.rb for the transaction boundary documentation)
|
|
83
|
+
if existing&.exists?
|
|
84
|
+
# Update existing through model with validated attributes
|
|
85
|
+
safe_attrs = validated_attrs(through_class, attrs)
|
|
86
|
+
safe_attrs.each { |k, v| existing.send("#{k}=", v) }
|
|
87
|
+
existing.updated_at = Familia.now.to_f if existing.respond_to?(:updated_at=)
|
|
88
|
+
# Save returns boolean, but we want to return the model instance
|
|
89
|
+
existing.save if safe_attrs.any? || existing.respond_to?(:updated_at=)
|
|
90
|
+
existing # Return the model, not the save result
|
|
91
|
+
else
|
|
92
|
+
# Create new through model with our deterministic key as objid
|
|
93
|
+
# Pass objid during initialization to prevent auto-generation
|
|
94
|
+
inst = through_class.new(objid: key)
|
|
95
|
+
|
|
96
|
+
# Set foreign key fields if they exist (validated via respond_to?)
|
|
97
|
+
target_field = "#{target.class.config_name}_objid"
|
|
98
|
+
participant_field = "#{participant.class.config_name}_objid"
|
|
99
|
+
inst.send("#{target_field}=", target.objid) if inst.respond_to?("#{target_field}=")
|
|
100
|
+
inst.send("#{participant_field}=", participant.objid) if inst.respond_to?("#{participant_field}=")
|
|
101
|
+
|
|
102
|
+
# Set updated_at for cache invalidation
|
|
103
|
+
inst.updated_at = Familia.now.to_f if inst.respond_to?(:updated_at=)
|
|
104
|
+
|
|
105
|
+
# Set custom attributes (validated against field schema)
|
|
106
|
+
safe_attrs = validated_attrs(through_class, attrs)
|
|
107
|
+
safe_attrs.each { |k, v| inst.send("#{k}=", v) }
|
|
108
|
+
|
|
109
|
+
# Save returns boolean, but we want to return the model instance
|
|
110
|
+
inst.save
|
|
111
|
+
inst # Return the model, not the save result
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Find and destroy a through model instance
|
|
116
|
+
#
|
|
117
|
+
# Used during remove operations to clean up the join table entry.
|
|
118
|
+
#
|
|
119
|
+
# @param through_class [Class] The through model class
|
|
120
|
+
# @param target [Object] The target instance
|
|
121
|
+
# @param participant [Object] The participant instance
|
|
122
|
+
# @return [void]
|
|
123
|
+
#
|
|
124
|
+
def find_and_destroy(through_class:, target:, participant:)
|
|
125
|
+
key = build_key(target: target, participant: participant, through_class: through_class)
|
|
126
|
+
existing = through_class.load(key)
|
|
127
|
+
# Use the public exists? method for a more robust check
|
|
128
|
+
existing&.destroy! if existing&.exists?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Validate attribute keys against the through model's field schema
|
|
132
|
+
#
|
|
133
|
+
# This prevents arbitrary method invocation by ensuring only defined
|
|
134
|
+
# fields can be set via the attrs hash.
|
|
135
|
+
#
|
|
136
|
+
# @param through_class [Class] The through model class
|
|
137
|
+
# @param attrs [Hash] Attributes to validate
|
|
138
|
+
# @return [Hash] Only attributes whose keys match defined fields
|
|
139
|
+
#
|
|
140
|
+
def validated_attrs(through_class, attrs)
|
|
141
|
+
return {} if attrs.nil? || attrs.empty?
|
|
142
|
+
|
|
143
|
+
valid_fields = through_class.fields.map(&:to_sym)
|
|
144
|
+
attrs.select { |k, _v| valid_fields.include?(k.to_sym) }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -7,6 +7,7 @@ require_relative 'participation_membership'
|
|
|
7
7
|
require_relative 'collection_operations'
|
|
8
8
|
require_relative 'participation/participant_methods'
|
|
9
9
|
require_relative 'participation/target_methods'
|
|
10
|
+
require_relative 'participation/through_model_operations'
|
|
10
11
|
|
|
11
12
|
module Familia
|
|
12
13
|
module Features
|
|
@@ -117,7 +118,7 @@ module Familia
|
|
|
117
118
|
# - +ClassName.add_to_collection_name(instance)+ - Add instance to collection
|
|
118
119
|
# - +ClassName.remove_from_collection_name(instance)+ - Remove instance from collection
|
|
119
120
|
#
|
|
120
|
-
# ==== On Instances (Participant Methods, if
|
|
121
|
+
# ==== On Instances (Participant Methods, if generate_participant_methods)
|
|
121
122
|
# - +instance.in_class_collection_name?+ - Check membership in class collection
|
|
122
123
|
# - +instance.add_to_class_collection_name+ - Add self to class collection
|
|
123
124
|
# - +instance.remove_from_class_collection_name+ - Remove self from class collection
|
|
@@ -134,7 +135,9 @@ module Familia
|
|
|
134
135
|
# - +:sorted_set+: Ordered by score (default)
|
|
135
136
|
# - +:set+: Unordered unique membership
|
|
136
137
|
# - +:list+: Ordered sequence allowing duplicates
|
|
137
|
-
# @param
|
|
138
|
+
# @param generate_participant_methods [Boolean] Whether to generate convenience methods on instances (default: +true+)
|
|
139
|
+
# @param through [Class, Symbol, String, nil] Optional join model class for
|
|
140
|
+
# storing additional attributes. See +participates_in+ for details.
|
|
138
141
|
#
|
|
139
142
|
# @example Simple priority-based global collection
|
|
140
143
|
# class User < Familia::Horreum
|
|
@@ -160,7 +163,7 @@ module Familia
|
|
|
160
163
|
# @see #participates_in for instance-level participation relationships
|
|
161
164
|
# @since 1.0.0
|
|
162
165
|
def class_participates_in(collection_name, score: nil,
|
|
163
|
-
type: :sorted_set,
|
|
166
|
+
type: :sorted_set, generate_participant_methods: true, through: nil)
|
|
164
167
|
# Store metadata for this participation relationship
|
|
165
168
|
participation_relationships << ParticipationRelationship.new(
|
|
166
169
|
_original_target: self, # For class-level, original and resolved are the same
|
|
@@ -168,21 +171,23 @@ module Familia
|
|
|
168
171
|
collection_name: collection_name,
|
|
169
172
|
score: score,
|
|
170
173
|
type: type,
|
|
171
|
-
|
|
174
|
+
generate_participant_methods: generate_participant_methods,
|
|
175
|
+
through: through,
|
|
176
|
+
method_prefix: nil, # Not applicable for class-level participation
|
|
172
177
|
)
|
|
173
178
|
|
|
174
179
|
# STEP 1: Add collection management methods to the class itself
|
|
175
180
|
# e.g., User.all_users, User.add_to_all_users(user)
|
|
176
181
|
TargetMethods::Builder.build_class_level(self, collection_name, type)
|
|
177
182
|
|
|
178
|
-
# STEP 2: Add participation methods to instances (if
|
|
183
|
+
# STEP 2: Add participation methods to instances (if generate_participant_methods)
|
|
179
184
|
# e.g., user.in_class_all_users?, user.add_to_class_all_users
|
|
180
|
-
return unless
|
|
185
|
+
return unless generate_participant_methods
|
|
181
186
|
|
|
182
187
|
# Pass the string 'class' as target to distinguish class-level from instance-level
|
|
183
188
|
# This prevents generating reverse collection methods (user can't have "all_users")
|
|
184
189
|
# See ParticipantMethods::Builder.build for handling of this special case
|
|
185
|
-
ParticipantMethods::Builder.build(self, 'class', collection_name, type, nil)
|
|
190
|
+
ParticipantMethods::Builder.build(self, 'class', collection_name, type, nil, through, nil)
|
|
186
191
|
end
|
|
187
192
|
|
|
188
193
|
# Define an instance-level participation relationship between two classes.
|
|
@@ -203,7 +208,7 @@ module Familia
|
|
|
203
208
|
# - +target.remove_participant_class_name(participant)+ - Remove participant from collection
|
|
204
209
|
# - +target.add_participant_class_names([participants])+ - Bulk add multiple participants
|
|
205
210
|
#
|
|
206
|
-
# ==== On Participant Class (if
|
|
211
|
+
# ==== On Participant Class (if generate_participant_methods)
|
|
207
212
|
# - +participant.in_target_collection_name?(target)+ - Check membership in target's collection
|
|
208
213
|
# - +participant.add_to_target_collection_name(target)+ - Add self to target's collection
|
|
209
214
|
# - +participant.remove_from_target_collection_name(target)+ - Remove self from target's collection
|
|
@@ -233,12 +238,18 @@ module Familia
|
|
|
233
238
|
# different scores (default)
|
|
234
239
|
# - +:set+: Unordered unique membership
|
|
235
240
|
# - +:list+: Ordered sequence, allows duplicates
|
|
236
|
-
# @param
|
|
241
|
+
# @param generate_participant_methods [Boolean] Whether to generate reverse collection
|
|
237
242
|
# methods on participant class. If true, methods are generated using the
|
|
238
243
|
# name of the target class. (default: +true+)
|
|
239
244
|
# @param as [Symbol, nil] Custom name for reverse collection methods
|
|
240
245
|
# (e.g., +as: :contracting_orgs+). When provided, overrides the default
|
|
241
246
|
# method name derived from the target class.
|
|
247
|
+
# @param through [Class, Symbol, String, nil] Optional join model class for
|
|
248
|
+
# storing additional attributes on the relationship. The through model:
|
|
249
|
+
# - Must use +feature :object_identifier+
|
|
250
|
+
# - Gets auto-created when adding to collection (via +through_attrs:+ param)
|
|
251
|
+
# - Gets auto-destroyed when removing from collection
|
|
252
|
+
# - Uses deterministic keys: +{target}:{id}:{participant}:{id}:{through}+
|
|
242
253
|
#
|
|
243
254
|
# @example Basic domain-employee relationship
|
|
244
255
|
#
|
|
@@ -284,7 +295,7 @@ module Familia
|
|
|
284
295
|
# @see ModelInstanceMethods#current_participations for membership queries
|
|
285
296
|
# @see ModelInstanceMethods#calculate_participation_score for scoring details
|
|
286
297
|
#
|
|
287
|
-
def participates_in(target, collection_name, score: nil, type: :sorted_set,
|
|
298
|
+
def participates_in(target, collection_name, score: nil, type: :sorted_set, generate_participant_methods: true, as: nil, through: nil, method_prefix: nil)
|
|
288
299
|
|
|
289
300
|
# Normalize the target class parameter
|
|
290
301
|
target_class = Familia.resolve_class(target)
|
|
@@ -306,6 +317,17 @@ module Familia
|
|
|
306
317
|
ERROR
|
|
307
318
|
end
|
|
308
319
|
|
|
320
|
+
# Validate through class if provided
|
|
321
|
+
if through
|
|
322
|
+
through_class = Familia.resolve_class(through)
|
|
323
|
+
raise ArgumentError, "Cannot resolve through class: #{through.inspect}" unless through_class
|
|
324
|
+
|
|
325
|
+
unless through_class.respond_to?(:features_enabled) &&
|
|
326
|
+
through_class.features_enabled.include?(:object_identifier)
|
|
327
|
+
raise ArgumentError, "Through model #{through_class} must use `feature :object_identifier`"
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
309
331
|
# Store metadata for this participation relationship
|
|
310
332
|
participation_relationships << ParticipationRelationship.new(
|
|
311
333
|
_original_target: target, # Original value as passed (Symbol/String/Class)
|
|
@@ -313,7 +335,9 @@ module Familia
|
|
|
313
335
|
collection_name: collection_name,
|
|
314
336
|
score: score,
|
|
315
337
|
type: type,
|
|
316
|
-
|
|
338
|
+
generate_participant_methods: generate_participant_methods,
|
|
339
|
+
through: through,
|
|
340
|
+
method_prefix: method_prefix,
|
|
317
341
|
)
|
|
318
342
|
|
|
319
343
|
# STEP 0: Add participations tracking field to PARTICIPANT class (Domain)
|
|
@@ -322,14 +346,14 @@ module Familia
|
|
|
322
346
|
|
|
323
347
|
# STEP 1: Add collection management methods to TARGET class (Employee)
|
|
324
348
|
# Employee gets: domains, add_domain, remove_domain, etc.
|
|
325
|
-
TargetMethods::Builder.build(target_class, collection_name, type)
|
|
349
|
+
TargetMethods::Builder.build(target_class, collection_name, type, through)
|
|
326
350
|
|
|
327
351
|
# STEP 2: Add participation methods to PARTICIPANT class (Domain) - only if
|
|
328
|
-
#
|
|
329
|
-
if
|
|
352
|
+
# generate_participant_methods. e.g. in_employee_domains?, add_to_employee_domains, etc.
|
|
353
|
+
if generate_participant_methods
|
|
330
354
|
# `as` parameter allows custom naming for reverse collections
|
|
331
355
|
# If not provided, we'll let the builder use the pluralized target class name
|
|
332
|
-
ParticipantMethods::Builder.build(self, target_class, collection_name, type, as)
|
|
356
|
+
ParticipantMethods::Builder.build(self, target_class, collection_name, type, as, through, method_prefix)
|
|
333
357
|
end
|
|
334
358
|
end
|
|
335
359
|
|
|
@@ -21,7 +21,9 @@ module Familia
|
|
|
21
21
|
:collection_name, # Symbol name of the collection (e.g., :members, :domains)
|
|
22
22
|
:score, # Proc/Symbol/nil - score calculator for sorted sets
|
|
23
23
|
:type, # Symbol - collection type (:sorted_set, :set, :list)
|
|
24
|
-
:
|
|
24
|
+
:generate_participant_methods, # Boolean - whether to generate participant methods
|
|
25
|
+
:through, # Symbol/Class/nil - through model class for join table pattern
|
|
26
|
+
:method_prefix, # Symbol/nil - custom prefix for reverse method names (e.g., :team)
|
|
25
27
|
) do
|
|
26
28
|
# Get a unique key for this participation relationship
|
|
27
29
|
# Useful for comparisons and hash keys
|
|
@@ -53,6 +55,22 @@ module Familia
|
|
|
53
55
|
target_class_base == comparison_target_base &&
|
|
54
56
|
collection_name == comparison_collection.to_sym
|
|
55
57
|
end
|
|
58
|
+
|
|
59
|
+
# Check if this relationship uses a through model
|
|
60
|
+
#
|
|
61
|
+
# @return [Boolean] true if through model is configured
|
|
62
|
+
def through_model?
|
|
63
|
+
!through.nil?
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Resolve the through class to an actual Class object
|
|
67
|
+
#
|
|
68
|
+
# @return [Class, nil] The resolved through class or nil
|
|
69
|
+
def resolved_through_class
|
|
70
|
+
return nil unless through
|
|
71
|
+
|
|
72
|
+
through.is_a?(Class) ? through : Familia.resolve_class(through)
|
|
73
|
+
end
|
|
56
74
|
end
|
|
57
75
|
end
|
|
58
76
|
end
|
|
@@ -38,7 +38,7 @@ module Familia
|
|
|
38
38
|
#
|
|
39
39
|
# # Participation with bidirectional control (no method collisions)
|
|
40
40
|
# participates_in Customer, :domains
|
|
41
|
-
# participates_in Team, :domains,
|
|
41
|
+
# participates_in Team, :domains, generate_participant_methods: false
|
|
42
42
|
# participates_in Organization, :domains, type: :set
|
|
43
43
|
# end
|
|
44
44
|
#
|
data/lib/familia/version.rb
CHANGED
data/pr_agent.toml
CHANGED
|
@@ -9,12 +9,17 @@ response_language = "en"
|
|
|
9
9
|
# Enable RAG context enrichment for codebase duplication compliance checks
|
|
10
10
|
enable_rag = true
|
|
11
11
|
# Include related repositories for comprehensive context
|
|
12
|
-
rag_repo_list = ['
|
|
12
|
+
rag_repo_list = ['onetimesecret/onetimesecret', 'delano/tryouts']
|
|
13
13
|
|
|
14
14
|
[compliance]
|
|
15
15
|
# Reference custom compliance checklist for project-specific rules
|
|
16
16
|
custom_compliance_path = "pr_compliance_checklist.yaml"
|
|
17
17
|
|
|
18
|
+
[pr_reviewer]
|
|
19
|
+
# Disable automatic label additions (triggers Claude review workflow noise)
|
|
20
|
+
enable_review_labels_security = false
|
|
21
|
+
enable_review_labels_effort = false
|
|
22
|
+
|
|
18
23
|
[ignore]
|
|
19
24
|
# Reduce noise by excluding generated files and build artifacts
|
|
20
25
|
glob = [
|
|
@@ -30,7 +30,7 @@ RSpec.describe 'participation_commands_verification_try' do
|
|
|
30
30
|
field :display_domain
|
|
31
31
|
field :created_at
|
|
32
32
|
participates_in ReverseIndexCustomer, :domains, score: :created_at
|
|
33
|
-
participates_in ReverseIndexCustomer, :preferred_domains,
|
|
33
|
+
participates_in ReverseIndexCustomer, :preferred_domains, generate_participant_methods: true
|
|
34
34
|
class_participates_in :all_domains, score: :created_at
|
|
35
35
|
end
|
|
36
36
|
end
|
|
@@ -49,7 +49,7 @@ class ReverseIndexDomain < Familia::Horreum
|
|
|
49
49
|
field :created_at
|
|
50
50
|
|
|
51
51
|
participates_in ReverseIndexCustomer, :domains, score: :created_at
|
|
52
|
-
participates_in ReverseIndexCustomer, :preferred_domains,
|
|
52
|
+
participates_in ReverseIndexCustomer, :preferred_domains, generate_participant_methods: true
|
|
53
53
|
class_participates_in :all_domains, score: :created_at
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# try/features/relationships/participation_method_prefix_try.rb
|
|
2
|
+
#
|
|
3
|
+
# frozen_string_literal: true
|
|
4
|
+
|
|
5
|
+
require_relative '../../../lib/familia'
|
|
6
|
+
|
|
7
|
+
# Test the method_prefix: option for participates_in
|
|
8
|
+
# This allows shorter reverse method names for namespaced classes
|
|
9
|
+
|
|
10
|
+
# Simulate a namespaced target class
|
|
11
|
+
module ::Admin
|
|
12
|
+
class MethodPrefixTeam < Familia::Horreum
|
|
13
|
+
feature :relationships
|
|
14
|
+
|
|
15
|
+
identifier_field :team_id
|
|
16
|
+
field :team_id
|
|
17
|
+
field :name
|
|
18
|
+
|
|
19
|
+
sorted_set :members
|
|
20
|
+
sorted_set :admins
|
|
21
|
+
|
|
22
|
+
def init
|
|
23
|
+
@team_id ||= "team_#{SecureRandom.hex(4)}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Test class using method_prefix: option
|
|
29
|
+
class ::MethodPrefixUser < Familia::Horreum
|
|
30
|
+
feature :relationships
|
|
31
|
+
|
|
32
|
+
identifier_field :user_id
|
|
33
|
+
field :user_id
|
|
34
|
+
field :email
|
|
35
|
+
|
|
36
|
+
# Use method_prefix to get shorter method names
|
|
37
|
+
# Instead of admin_method_prefix_team_instances, we get team_instances
|
|
38
|
+
participates_in Admin::MethodPrefixTeam, :members, method_prefix: :team
|
|
39
|
+
|
|
40
|
+
def init
|
|
41
|
+
@user_id ||= "user_#{SecureRandom.hex(4)}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Test class using as: which should take precedence over method_prefix:
|
|
46
|
+
class ::MethodPrefixPriorityUser < Familia::Horreum
|
|
47
|
+
feature :relationships
|
|
48
|
+
|
|
49
|
+
identifier_field :user_id
|
|
50
|
+
field :user_id
|
|
51
|
+
field :email
|
|
52
|
+
|
|
53
|
+
# as: takes precedence over method_prefix:
|
|
54
|
+
participates_in Admin::MethodPrefixTeam, :admins, method_prefix: :team, as: :my_teams
|
|
55
|
+
|
|
56
|
+
def init
|
|
57
|
+
@user_id ||= "user_#{SecureRandom.hex(4)}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Test class without method_prefix (default behavior)
|
|
62
|
+
class ::MethodPrefixDefaultUser < Familia::Horreum
|
|
63
|
+
feature :relationships
|
|
64
|
+
|
|
65
|
+
identifier_field :user_id
|
|
66
|
+
field :user_id
|
|
67
|
+
field :email
|
|
68
|
+
|
|
69
|
+
# Default: uses config_name (method_prefix_team - demodularized, no namespace)
|
|
70
|
+
participates_in Admin::MethodPrefixTeam, :members
|
|
71
|
+
|
|
72
|
+
def init
|
|
73
|
+
@user_id ||= "user_#{SecureRandom.hex(4)}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
@user = MethodPrefixUser.new(email: 'alice@example.com')
|
|
78
|
+
@priority_user = MethodPrefixPriorityUser.new(email: 'bob@example.com')
|
|
79
|
+
@default_user = MethodPrefixDefaultUser.new(email: 'charlie@example.com')
|
|
80
|
+
@team = Admin::MethodPrefixTeam.new(name: 'Engineering')
|
|
81
|
+
|
|
82
|
+
## method_prefix: generates shortened method names
|
|
83
|
+
# The method_prefix: :team option should generate team_* methods
|
|
84
|
+
@user.respond_to?(:team_instances)
|
|
85
|
+
#=> true
|
|
86
|
+
|
|
87
|
+
## method_prefix: generates team_ids method
|
|
88
|
+
@user.respond_to?(:team_ids)
|
|
89
|
+
#=> true
|
|
90
|
+
|
|
91
|
+
## method_prefix: generates team? method
|
|
92
|
+
@user.respond_to?(:team?)
|
|
93
|
+
#=> true
|
|
94
|
+
|
|
95
|
+
## method_prefix: generates team_count method
|
|
96
|
+
@user.respond_to?(:team_count)
|
|
97
|
+
#=> true
|
|
98
|
+
|
|
99
|
+
## method_prefix: does NOT generate verbose config_name methods
|
|
100
|
+
# The verbose admin_method_prefix_team_* methods should not be created
|
|
101
|
+
@user.respond_to?(:admin_method_prefix_team_instances)
|
|
102
|
+
#=> false
|
|
103
|
+
|
|
104
|
+
## as: takes precedence over method_prefix:
|
|
105
|
+
# When both as: and method_prefix: are provided, as: wins
|
|
106
|
+
@priority_user.respond_to?(:my_teams_instances)
|
|
107
|
+
#=> true
|
|
108
|
+
|
|
109
|
+
## as: takes precedence - method_prefix method NOT generated
|
|
110
|
+
@priority_user.respond_to?(:team_instances)
|
|
111
|
+
#=> false
|
|
112
|
+
|
|
113
|
+
## default behavior preserved when no method_prefix
|
|
114
|
+
# Without method_prefix:, the config_name is used (method_prefix_team)
|
|
115
|
+
# Note: config_name uses the class name without namespace
|
|
116
|
+
@default_user.respond_to?(:method_prefix_team_instances)
|
|
117
|
+
#=> true
|
|
118
|
+
|
|
119
|
+
## default behavior - no custom shortened method names
|
|
120
|
+
# The team_instances method is not generated unless method_prefix: :team is used
|
|
121
|
+
@default_user.respond_to?(:team_instances)
|
|
122
|
+
#=> false
|
|
123
|
+
|
|
124
|
+
## ParticipationRelationship stores method_prefix
|
|
125
|
+
# The relationship metadata should include the method_prefix
|
|
126
|
+
rel = MethodPrefixUser.participation_relationships.first
|
|
127
|
+
rel.method_prefix
|
|
128
|
+
#=> :team
|
|
129
|
+
|
|
130
|
+
## ParticipationRelationship method_prefix is nil for default
|
|
131
|
+
rel_default = MethodPrefixDefaultUser.participation_relationships.first
|
|
132
|
+
rel_default.method_prefix
|
|
133
|
+
#=> nil
|
|
@@ -28,7 +28,7 @@ class ReverseIndexDomain < Familia::Horreum
|
|
|
28
28
|
field :created_at
|
|
29
29
|
|
|
30
30
|
participates_in ReverseIndexCustomer, :domains, score: :created_at
|
|
31
|
-
participates_in ReverseIndexCustomer, :preferred_domains,
|
|
31
|
+
participates_in ReverseIndexCustomer, :preferred_domains, generate_participant_methods: true
|
|
32
32
|
class_participates_in :all_domains, score: :created_at
|
|
33
33
|
end
|
|
34
34
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# try/features/relationships/
|
|
1
|
+
# try/features/relationships/participation_reverse_methods_try.rb
|
|
2
2
|
#
|
|
3
3
|
# frozen_string_literal: true
|
|
4
4
|
|
|
5
5
|
require_relative '../../../lib/familia'
|
|
6
6
|
|
|
7
|
-
# Test demonstrating
|
|
7
|
+
# Test demonstrating reverse collection methods (generate_participant_methods: true)
|
|
8
8
|
# This shows the improvement from asymmetric to symmetric relationship access
|
|
9
9
|
|
|
10
10
|
# Setup test classes
|
|
@@ -51,8 +51,8 @@ class ::ProjectUser < Familia::Horreum
|
|
|
51
51
|
field :name
|
|
52
52
|
field :role
|
|
53
53
|
|
|
54
|
-
# Define
|
|
55
|
-
# These
|
|
54
|
+
# Define participation relationships with reverse collection methods
|
|
55
|
+
# These auto-generate methods like user.project_team_instances (when generate_participant_methods: true)
|
|
56
56
|
participates_in ProjectTeam, :members # Generates: user.project_team_instances
|
|
57
57
|
participates_in ProjectTeam, :admins # Also adds to user.project_team_instances (union)
|
|
58
58
|
participates_in ProjectOrganization, :employees # Generates: user.project_organization_instances
|
|
@@ -197,13 +197,13 @@ contracting_orgs_instances.map(&:name)
|
|
|
197
197
|
@team1.admins.to_a
|
|
198
198
|
#=> [@user1.identifier]
|
|
199
199
|
|
|
200
|
-
## Test
|
|
200
|
+
## Test symmetric consistency - forward direction
|
|
201
201
|
team1_member_ids = @team1.members.to_a
|
|
202
202
|
team1_members = ProjectUser.load_multi(team1_member_ids).compact
|
|
203
203
|
team1_members.map(&:name)
|
|
204
204
|
#=> ["Alice"]
|
|
205
205
|
|
|
206
|
-
## Test
|
|
206
|
+
## Test symmetric consistency - reverse direction
|
|
207
207
|
user1_teams = @user1.project_team_instances
|
|
208
208
|
user1_team_ids = user1_teams.map(&:identifier)
|
|
209
209
|
user1_team_ids.include?(@team1.identifier)
|