15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/belongs_to_one_of.rb', line 15
def belongs_to_one_of(
resource_key,
raw_possible_resource_types,
include_type_column: false
)
resource_type_field = "#{resource_key}_type"
unless [true, false].include?(include_type_column)
resource_type_field =
include_type_column
end
config_model = BelongsToOneOfModel.new(
resource_key,
raw_possible_resource_types,
include_type_column,
resource_type_field,
self,
)
define_method "belongs_to_exactly_one_#{resource_key}" do
config_model.validate_exactly_one_resource(self)
end
define_method "belongs_to_at_most_one_#{resource_key}" do
config_model.validate_at_most_one_resource(self)
end
if include_type_column
define_method "#{resource_type_field}_matches_#{resource_key}" do
config_model.validate_correct_resource_type(self)
end
end
define_method "#{resource_key}=" do |resource|
config_model.resource_setter(resource, self)
end
define_method resource_key do
config_model.resource_getter(self)
end
define_method "#{resource_key}_id" do
config_model.resource_id_getter(self)
end
unless include_type_column
define_method "#{resource_key}_type" do
config_model.resource_type_getter(self)
end
end
end
|