Module: Familia::Features::Relationships::Tracking::ClassMethods

Defined in:
lib/familia/features/relationships/tracking.rb

Instance Method Summary collapse

Instance Method Details

#camelize_word(word) ⇒ Object

Simple camelize method (basic implementation)



33
34
35
# File 'lib/familia/features/relationships/tracking.rb', line 33

def camelize_word(word)
  word.to_s.split('_').map(&:capitalize).join
end

#singularize_word(word) ⇒ Object

Simple singularize method (basic implementation)



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/familia/features/relationships/tracking.rb', line 18

def singularize_word(word)
  word = word.to_s
  # Basic English pluralization rules (simplified)
  if word.end_with?('ies')
    "#{word[0..-4]}y"
  elsif word.end_with?('es') && word.length > 3
    word[0..-3]
  elsif word.end_with?('s') && word.length > 1
    word[0..-2]
  else
    word
  end
end

#tracked_in(context_class, collection_name, score: nil, on_destroy: :remove) ⇒ Object

Define a tracked_in relationship

Examples:

Basic tracking

tracked_in Customer, :domains, score: :created_at

Multi-presence tracking

tracked_in Customer, :domains, score: -> { permission_encode(created_at, permission_level) }
tracked_in Team, :domains, score: :added_at
tracked_in Organization, :all_domains, score: :created_at

Parameters:

  • context_class (Class, Symbol)

    The class that owns the collection

  • collection_name (Symbol)

    Name of the collection

  • score (Symbol, Proc, nil) (defaults to: nil)

    How to calculate the score

  • on_destroy (Symbol) (defaults to: :remove)

    What to do when object is destroyed (:remove, :ignore)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/familia/features/relationships/tracking.rb', line 51

def tracked_in(context_class, collection_name, score: nil, on_destroy: :remove)
  # Handle special :global context
  if context_class == :global
    context_class_name = 'Global'
  elsif context_class.is_a?(Class)
    class_name = context_class.name
    context_class_name = if class_name.include?('::')
                           # Extract the last part after the last ::
                           class_name.split('::').last
                         else
                           class_name
                         end
  # Extract just the class name, handling anonymous classes
  else
    context_class_name = camelize_word(context_class)
  end

  # Store metadata for this tracking relationship
  tracking_relationships << {
    context_class: context_class,
    context_class_name: context_class_name,
    collection_name: collection_name,
    score: score,
    on_destroy: on_destroy
  }

  # Generate class methods on the context class (skip for global)
  if context_class == :global
    generate_global_class_methods(self, collection_name)
  else
    generate_context_class_methods(context_class, collection_name)
  end

  # Generate instance methods on this class
  generate_tracking_instance_methods(context_class_name, collection_name, score)
end

#tracking_relationshipsObject

Get all tracking relationships for this class



89
90
91
# File 'lib/familia/features/relationships/tracking.rb', line 89

def tracking_relationships
  @tracking_relationships ||= []
end