contentful_model 0.1.1 → 0.1.4

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
  SHA1:
3
- metadata.gz: 199e61fb05a5d359cbf69b01d5db2dba308239a9
4
- data.tar.gz: b9478b41fda3f6b2870d54a9f627d44a9662c0ce
3
+ metadata.gz: 209fd18d71e5f92e2e101d42dbc12c2a1028c841
4
+ data.tar.gz: 16b9e1322d5102c7be988ca982f99f32e3ff0dc1
5
5
  SHA512:
6
- metadata.gz: 8801e3881b967c5cf1e44918cb605a58ded0fc33e1247018aa718c934c491159f3f8b2b6ad33931e5ac3b329ba63351a7caf94a786eeec6548910f0d0364ec0f
7
- data.tar.gz: 20e98e13891d9b9b33c0b50e192b5b9ddef673e522f631b90f53f279b4f424cda334f220af2a2a9b7068bc6a39dca2f3d2714f7ab11f17d8cdd75b8603f356a9
6
+ metadata.gz: 64ae64418a0f5b3d9687e2c4446de39ac92002d87b3eec38b0a6aeeb709f8b5f1eb49ee39b1fd1759fe403e29e5f86281e779023c21abcc5e1a81aa95e9ba2bb
7
+ data.tar.gz: adecfdeaabf91259bd92ea9225d58e35db73e3da88d0be1706078b6cea9fa886f5b3cb81b13545d5e20b8935c704e24950f6d92ea0fd8112949d3dacad4b3c0a
@@ -8,6 +8,7 @@ module ContentfulModel
8
8
  base.include HasOne
9
9
  base.include BelongsTo
10
10
  base.include BelongsToMany
11
+ base.include HasManyNested
11
12
  end
12
13
  end
13
14
  end
@@ -0,0 +1,76 @@
1
+ module ContentfulModel
2
+ module Associations
3
+ module HasManyNested
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ # has_many_nested allows you to set up a tree relationship
10
+ # it calls has_many and belongs_to_many on the class, and sets up
11
+ # some methods to find a deeply-nested instance's parents
12
+ #
13
+ # To set this up in contentful, add a multi-entry field validated to the same model
14
+ # as the parent, and give it a name. For example, Page might have a field called childPages:
15
+ #
16
+ # has_many_nested :child_pages
17
+ #
18
+ # would setup up an instance attribute called parent_pages which lists all the direct
19
+ # parents of this page. It would also create methods to find a page based on an array
20
+ # of its ancestors, and generate an array of ancestors. Note that this builds an array of the
21
+ # ancestors which called the object; because 'many' associations in Contentful are actually
22
+ # 'belongs_to_many' from the child end, we might have several ancestors to a page. You will need
23
+ # to write your own recursion for this, because it's probably an implementation-specific problem.
24
+ def has_many_nested(association_name)
25
+ has_many association_name, inverse_of: :"parent_#{self.to_s.underscore}"
26
+ belongs_to_many :"parent_#{self.to_s.underscore.pluralize}", class_name: self.to_s, inverse_of: association_name
27
+
28
+ # A utility method which returns the parent object; saves passing around interpolated strings
29
+ define_method :parent do
30
+ self.send(:"parent_#{self.class.to_s.underscore}")
31
+ end
32
+
33
+ # Determine if the object has any parents. If it doesn't, it's considered a root.
34
+ # This only works if the objects are called through their parents' 'child_[whatever]' method
35
+ define_method :root? do
36
+ parent.nil?
37
+ end
38
+
39
+ # Iterate over parents until you reach the root.
40
+ # @param [Proc] a block to call on each ancestor
41
+ # @return [Enumerable] which you can iterate over
42
+ define_method :find_ancestors do |&block|
43
+ return enum_for(:find_ancestors) unless block
44
+ block.call(parent)
45
+ unless parent.root?
46
+ parent.find_ancestors {|a| block.call(a)}
47
+ end
48
+ end
49
+
50
+ # A utility method to return the results of `find_ancestors` as an array
51
+ # @return [Array] of ancestors in reverse order (root last)
52
+ define_method :ancestors do
53
+ self.find_ancestors.to_a
54
+ end
55
+
56
+ # Return the last member of the enumerable, which is the root
57
+ # @return the root instance of this object
58
+ define_method :root do
59
+ find_ancestors.last
60
+ end
61
+
62
+ # @return [Boolean] whether or not this instance has children
63
+ define_method :has_children? do
64
+ !self.send(association_name).empty?
65
+ end
66
+
67
+ # @return [Array] a collection of child objects, based on the association name
68
+ define_method :children do
69
+ self.send(association_name)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -70,7 +70,7 @@ module ContentfulModel
70
70
 
71
71
  def client
72
72
  self.add_entry_mapping
73
- @@client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
73
+ @client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
74
74
  end
75
75
 
76
76
  def content_type
@@ -42,9 +42,9 @@ module ContentfulModel
42
42
  args.each do |query|
43
43
  #query is a hash
44
44
  if query.values.first.is_a?(Array) #we need to do an 'in' query
45
- @query << {"fields.#{query.keys.first}[in]" => query.values.first.join(",")}
45
+ @query << {"fields.#{query.keys.first.to_s.camelize(:lower)}[in]" => query.values.first.join(",")}
46
46
  elsif query.values.first.is_a?(String)
47
- @query << {"fields.#{query.keys.first}" => query.values.first}
47
+ @query << {"fields.#{query.keys.first.to_s.camelize(:lower)}" => query.values.first}
48
48
  end
49
49
  end
50
50
  self
@@ -53,7 +53,7 @@ module ContentfulModel
53
53
  def search(parameters)
54
54
  if parameters.is_a?(Hash)
55
55
  parameters.each do |field, search|
56
- @query << {"fields.#{field}[match]" => search}
56
+ @query << {"fields.#{field.to_s.camelize(:lower)}[match]" => search}
57
57
  end
58
58
  elsif parameters.is_a?(String)
59
59
  @query << {"query" => parameters}
@@ -1,3 +1,3 @@
1
1
  module ContentfulModel
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Error Creative Studio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-26 00:00:00.000000000 Z
11
+ date: 2015-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contentful
@@ -81,6 +81,7 @@ files:
81
81
  - lib/contentful_model/associations/belongs_to.rb
82
82
  - lib/contentful_model/associations/belongs_to_many.rb
83
83
  - lib/contentful_model/associations/has_many.rb
84
+ - lib/contentful_model/associations/has_many_nested.rb
84
85
  - lib/contentful_model/associations/has_one.rb
85
86
  - lib/contentful_model/base.rb
86
87
  - lib/contentful_model/chainable_queries.rb