contentful_model 0.1.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 199e61fb05a5d359cbf69b01d5db2dba308239a9
|
4
|
+
data.tar.gz: b9478b41fda3f6b2870d54a9f627d44a9662c0ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8801e3881b967c5cf1e44918cb605a58ded0fc33e1247018aa718c934c491159f3f8b2b6ad33931e5ac3b329ba63351a7caf94a786eeec6548910f0d0364ec0f
|
7
|
+
data.tar.gz: 20e98e13891d9b9b33c0b50e192b5b9ddef673e522f631b90f53f279b4f424cda334f220af2a2a9b7068bc6a39dca2f3d2714f7ab11f17d8cdd75b8603f356a9
|
@@ -25,6 +25,24 @@ module ContentfulModel
|
|
25
25
|
inverse_of: self.to_s.underscore.to_sym
|
26
26
|
}
|
27
27
|
options = default_options.merge(opts)
|
28
|
+
# Set up the association name for the instance which loaded this object
|
29
|
+
# This is useful in situations where, primarily, it's a 1:* relationship (i.e. belongs_to)
|
30
|
+
# even though this isn't actually supported by Contentful
|
31
|
+
#
|
32
|
+
# f = Foo.first
|
33
|
+
# b = f.bars.first
|
34
|
+
# b.foo #would return the instance of Foo which loaded it
|
35
|
+
|
36
|
+
define_method association_names.to_s.singularize do
|
37
|
+
instance_variable_get(:"@#{association_names.to_s.singularize}")
|
38
|
+
end
|
39
|
+
|
40
|
+
define_method "#{association_names.to_s.singularize}=" do |parent|
|
41
|
+
instance_variable_set(:"@#{association_names.to_s.singularize}",parent)
|
42
|
+
return self
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set up the association name (plural)
|
28
46
|
if self.respond_to?(association_names)
|
29
47
|
self.send(association_names)
|
30
48
|
else
|
@@ -18,14 +18,19 @@ module ContentfulModel
|
|
18
18
|
# @param association_names [Symbol] the name of the child model, as a plural symbol
|
19
19
|
def has_many(association_names, options = {})
|
20
20
|
default_options = {
|
21
|
-
class_name: association_names.to_s.singularize.classify
|
21
|
+
class_name: association_names.to_s.singularize.classify,
|
22
|
+
inverse_of: self.to_s.underscore.to_s
|
22
23
|
}
|
23
24
|
options = default_options.merge(options)
|
24
25
|
define_method association_names do
|
25
26
|
begin
|
26
|
-
#
|
27
|
-
# ContentfulModel::Base#method_missing and return
|
28
|
-
|
27
|
+
# Start by calling the association name as a method on the superclass.
|
28
|
+
# this will end up in ContentfulModel::Base#method_missing and return the value from Contentful.
|
29
|
+
# We set the singular of the association name on each object in the collection to allow easy
|
30
|
+
# reverse recursion without another API call (i.e. finding the Foo which called .bars())
|
31
|
+
super().collect do |child|
|
32
|
+
child.send(:"#{options[:inverse_of]}=",self)
|
33
|
+
end
|
29
34
|
rescue ContentfulModel::AttributeNotFoundError
|
30
35
|
# If AttributeNotFoundError is raised, that means that the association name isn't available on the object.
|
31
36
|
# We try to call the class name (pluralize) instead, or give up and return an empty collection
|
@@ -17,14 +17,17 @@ module ContentfulModel
|
|
17
17
|
# @param options [Hash] a hash, the only key of which is important is class_name.
|
18
18
|
def has_one(association_name, options = {})
|
19
19
|
default_options = {
|
20
|
-
class_name: association_name.to_s.classify
|
20
|
+
class_name: association_name.to_s.classify,
|
21
|
+
inverse_of: self.to_s.underscore.to_sym
|
21
22
|
}
|
22
23
|
options = default_options.merge(options)
|
23
24
|
# Define a method which matches the association name
|
24
25
|
define_method association_name do
|
25
26
|
begin
|
26
|
-
# Start by calling the association name as a method on the superclass.
|
27
|
-
|
27
|
+
# Start by calling the association name as a method on the superclass.
|
28
|
+
# this will end up in ContentfulModel::Base#method_missing and return the value from Contentful.
|
29
|
+
# We set the singular of the association name on this object to allow easy recursing.
|
30
|
+
super().send(:"#{options[:inverse_of]}=",self)
|
28
31
|
rescue ContentfulModel::AttributeNotFoundError
|
29
32
|
# If method_missing returns an error, the field doesn't exist. If a class is specified, try that.
|
30
33
|
if options[:class_name].underscore.to_sym != association_name
|