contentful_rails 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/contentful_rails.rb +1 -0
- data/lib/contentful_rails/nested_resource.rb +69 -0
- data/lib/contentful_rails/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4b2f71d5c5edbf6d9ae97162046165ce9b2070e
|
4
|
+
data.tar.gz: ca52e20c5d70341706caa5ed389043b3be1a2994
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d7dd059d4d2a1302a623756481247b7e1cca58f3c6bb78129d4b63c013e61846d0a21624e9b8096333383062494632950959db6ac623808eec630ecc9520ee6
|
7
|
+
data.tar.gz: 2eda21fd407da739feba79a8df08e04a89c7d78d3edd68e4075ac35b71819d7a0614021198b34fecbe75a2cc652431897a299a52054f6a3d4babb79bd36d0bb9
|
data/lib/contentful_rails.rb
CHANGED
@@ -2,6 +2,7 @@ require "contentful_rails/engine"
|
|
2
2
|
require "contentful_rails/development_constraint"
|
3
3
|
require "contentful_rails/cached_timestamps"
|
4
4
|
require "contentful_rails/markdown_renderer"
|
5
|
+
require "contentful_rails/nested_resource"
|
5
6
|
require 'redcarpet'
|
6
7
|
|
7
8
|
module ContentfulRails
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ContentfulRails
|
2
|
+
module NestedResource
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Get a deeply-nested object from a string which represents the heirarchy.
|
10
|
+
# The obvious use for this is to find an object from a URL
|
11
|
+
# e.g. /grandparent/parent/child
|
12
|
+
# @param [Symbol] the field to search by - for example, :slug
|
13
|
+
# @param [String] the path as a forward-slash separated string
|
14
|
+
def get_nested_from_path_by(field, path, opts)
|
15
|
+
if opts.present? && opts[:unescape]
|
16
|
+
path = CGI::unescape(path)
|
17
|
+
end
|
18
|
+
root, *children = path.gsub(/^\//, '').split("/")
|
19
|
+
|
20
|
+
if field.to_sym == :id
|
21
|
+
#we need to call find() to get by ID
|
22
|
+
root_entity = find(root)
|
23
|
+
else
|
24
|
+
begin
|
25
|
+
root_entity = search(field => root).first
|
26
|
+
rescue Contentful::BadRequest
|
27
|
+
#we have something which needs find_by called on it
|
28
|
+
root_entity = find_by(field => root).first
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if root_entity && root_entity.has_children?
|
33
|
+
return root_entity.get_child_entity_from_path_by(field, children)
|
34
|
+
elsif root_entity
|
35
|
+
return root_entity
|
36
|
+
else
|
37
|
+
return nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Given a field and an array of child fields, we need to recurse through them to get the last one
|
43
|
+
# @param [Symbol] the field we need to search for
|
44
|
+
# @param [Array] an array of field values to match against
|
45
|
+
# @return an entity matching this class, which is the last in the tree
|
46
|
+
def get_child_entity_from_path_by(field, children)
|
47
|
+
# the next child in the path
|
48
|
+
child_value = children.shift
|
49
|
+
# get the child entity
|
50
|
+
child = self.send(:children).find {|child| child.send(field) == child_value}
|
51
|
+
if child && children.size > 0
|
52
|
+
# we have some recursion to do - we're not at the end of the array
|
53
|
+
# so call this method again with a smaller set of children
|
54
|
+
child.get_child_entity_from_path_by(field, children)
|
55
|
+
else
|
56
|
+
return child #this is the final thing in the array - return it
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Given a field (and optional delimiter), return a path to the current object.
|
61
|
+
# e.g. you'd end up with /path/to/page (where this object is 'page')
|
62
|
+
# @param [Symbol] the field to use to create the path
|
63
|
+
# @param [String] the delimiter to use. Defaults to "/"
|
64
|
+
# @return [String] the path as a string
|
65
|
+
def nested_path_by(field, delimiter="/")
|
66
|
+
([self] + ancestors).reverse.collect {|a| a.send(field)}.join(delimiter)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contentful_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
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-03-
|
11
|
+
date: 2015-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: contentful_model
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/contentful_rails/development_constraint.rb
|
70
70
|
- lib/contentful_rails/engine.rb
|
71
71
|
- lib/contentful_rails/markdown_renderer.rb
|
72
|
+
- lib/contentful_rails/nested_resource.rb
|
72
73
|
- lib/contentful_rails/version.rb
|
73
74
|
- lib/tasks/contentful_rails_tasks.rake
|
74
75
|
- test/contentful_rails_test.rb
|