dynamic-active-model 0.9.0 → 0.9.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7040fc598ff4554e76a3c57dcd5fc32c37fc2592f0567af929c1a5f93319313a
|
|
4
|
+
data.tar.gz: 8601378b7117b7d612db65698025879a31a6e432952a44f2c77cab2ba027e7ca
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a598ae139fbe14648219525a1250061089a960978b346ff40a05f392fbd6313b69ba9a8640cb17753b7dab8086d1b2d4d7c4974f9006084d57561eabeea48f3c
|
|
7
|
+
data.tar.gz: a98ea335292e8ea96d4c7bd9123749ed425dcc8e6a0da7eaf6bd9d8a188c08859fa4bf04b2d44c81920f75aa64e7f9282ff5698a2c6e0c0716949e0546da9927
|
|
@@ -76,7 +76,7 @@ module DynamicActiveModel
|
|
|
76
76
|
|
|
77
77
|
@join_tables.each do |join_table_model|
|
|
78
78
|
models = join_table_model.column_names.map do |column_name|
|
|
79
|
-
foreign_key_to_models
|
|
79
|
+
foreign_key_to_models.dig(column_name.downcase, 0, 0)
|
|
80
80
|
end.compact
|
|
81
81
|
add_has_and_belongs_to_many(join_table_model, models) if models.size == 2
|
|
82
82
|
end
|
|
@@ -42,16 +42,17 @@ module DynamicActiveModel
|
|
|
42
42
|
str << " self.table_name = #{@model.table_name.to_sym.inspect}\n"
|
|
43
43
|
end
|
|
44
44
|
all_has_many_relationships.each do |assoc|
|
|
45
|
-
append_association!(str, assoc)
|
|
45
|
+
append_association!(str, 'has_many', assoc, has_many_association_options(assoc))
|
|
46
46
|
end
|
|
47
47
|
all_belongs_to_relationships.each do |assoc|
|
|
48
|
-
append_association!(str, assoc)
|
|
48
|
+
append_association!(str, 'belongs_to', assoc, belongs_to_association_options(assoc))
|
|
49
49
|
end
|
|
50
50
|
all_has_one_relationships.each do |assoc|
|
|
51
|
-
append_association!(str, assoc)
|
|
51
|
+
append_association!(str, 'has_one', assoc, has_one_association_options(assoc))
|
|
52
52
|
end
|
|
53
53
|
all_has_and_belongs_to_many_relationships.each do |assoc|
|
|
54
|
-
append_association!(str, assoc
|
|
54
|
+
append_association!(str, 'has_and_belongs_to_many', assoc,
|
|
55
|
+
has_and_belongs_to_many_association_options(assoc))
|
|
55
56
|
end
|
|
56
57
|
str << "end\n"
|
|
57
58
|
str
|
|
@@ -85,35 +86,13 @@ module DynamicActiveModel
|
|
|
85
86
|
|
|
86
87
|
# Appends an association definition to the source string
|
|
87
88
|
# @param str [String] The source string being built
|
|
89
|
+
# @param assoc_type [String] The association macro, e.g. 'has_many'
|
|
88
90
|
# @param assoc [ActiveRecord::Reflection::AssociationReflection] The association to add
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
when ActiveRecord::Reflection::HasManyReflection
|
|
92
|
-
'has_many'
|
|
93
|
-
when ActiveRecord::Reflection::BelongsToReflection
|
|
94
|
-
'belongs_to'
|
|
95
|
-
when ActiveRecord::Reflection::HasOneReflection
|
|
96
|
-
'has_one'
|
|
97
|
-
when ActiveRecord::Reflection::HasAndBelongsToManyReflection
|
|
98
|
-
'has_and_belongs_to_many'
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
association_options = case assoc_type
|
|
102
|
-
when 'has_many'
|
|
103
|
-
has_many_association_options(assoc)
|
|
104
|
-
when 'belongs_to'
|
|
105
|
-
belongs_to_association_options(assoc)
|
|
106
|
-
when 'has_one'
|
|
107
|
-
has_one_association_options(assoc)
|
|
108
|
-
when 'has_and_belongs_to_many'
|
|
109
|
-
has_and_belongs_to_many_association_options(assoc)
|
|
110
|
-
end
|
|
111
|
-
|
|
91
|
+
# @param association_options [Hash] Non-default options to write; nil values are skipped
|
|
92
|
+
def append_association!(str, assoc_type, assoc, association_options)
|
|
112
93
|
str << " #{assoc_type} #{assoc.name.inspect}"
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
str << ", #{name}: '#{value}'"
|
|
116
|
-
end
|
|
94
|
+
association_options.compact.each do |name, value|
|
|
95
|
+
str << ", #{name}: '#{value}'"
|
|
117
96
|
end
|
|
118
97
|
str << "\n"
|
|
119
98
|
end
|
|
@@ -166,7 +145,7 @@ module DynamicActiveModel
|
|
|
166
145
|
def has_and_belongs_to_many_association_options(assoc)
|
|
167
146
|
options = {}
|
|
168
147
|
options[:join_table] = assoc.options[:join_table] if assoc.options[:join_table]
|
|
169
|
-
unless assoc.options[:class_name].underscore.
|
|
148
|
+
unless assoc.options[:class_name].underscore.pluralize == assoc.name.to_s
|
|
170
149
|
options[:class_name] =
|
|
171
150
|
assoc.options[:class_name]
|
|
172
151
|
end
|
|
@@ -178,19 +157,5 @@ module DynamicActiveModel
|
|
|
178
157
|
def default_foreign_key_name
|
|
179
158
|
"#{@model.table_name.underscore.singularize}_id"
|
|
180
159
|
end
|
|
181
|
-
|
|
182
|
-
# Generates the default foreign key for the associated model
|
|
183
|
-
# @param assoc [ActiveRecord::Reflection::HasAndBelongsToManyReflection] The association
|
|
184
|
-
# @return [String] The generated foreign key name
|
|
185
|
-
def generate_association_foreign_key(assoc)
|
|
186
|
-
"#{assoc.options[:class_name].underscore.singularize}_id"
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
# Gets a constant by its fully qualified name
|
|
190
|
-
# @param class_name [String] The fully qualified class name
|
|
191
|
-
# @return [Class] The resolved class
|
|
192
|
-
def const_get(class_name)
|
|
193
|
-
class_name.split('::').inject(Object) { |mod, name| mod.const_get(name) }
|
|
194
|
-
end
|
|
195
160
|
end
|
|
196
161
|
end
|