Module: Shared::OriginRelationship::ClassMethods

Defined in:
app/models/concerns/shared/origin_relationship.rb

Instance Method Summary collapse

Instance Method Details

#is_origin_for(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/concerns/shared/origin_relationship.rb', line 42

def is_origin_for(*args)
  if args.length == 0
    raise ArgumentError.new('is_origin_for must have an array full of valid target tables supplied!')
  end

  # @return [Array of Strings]
  #   valid new_object Classes
  define_method :valid_new_object_classes do
    args
  end

  # @return [Array of Strings]
  #   valid new_object Classes
  define_singleton_method :valid_new_object_classes do
    args
  end

  args.each do |a|
    relationship = 'derived_' + a.demodulize.tableize
    has_many relationship.to_sym, source_type: a, through: :origin_relationships, source: :new_object
  end
end

#originates_from(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/concerns/shared/origin_relationship.rb', line 65

def originates_from(*args)
  if args.length == 0
    raise ArgumentError.new('is_origin_for must have an array full of valid target tables supplied!')
  end

  # @return [Array of Strings]
  #   valid new_object Classes
  define_method :valid_old_object_classes do
    args
  end

  # @return [Array of Strings]
  #   valid new_object Classes
  define_singleton_method :valid_old_object_classes do
    args
  end

  args.each do |a|
    relationship = 'origin_' + a.demodulize.tableize
    has_many relationship.to_sym, source_type: a, through: :related_origin_relationships, source: :old_object
  end
end