Skip to content

Commit 86e6f1c

Browse files
authored
Merge pull request #12 from contentstack/publish_fallback
Publish fallback
2 parents 2872573 + 864598d commit 86e6f1c

File tree

9 files changed

+209
-9
lines changed

9 files changed

+209
-9
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
## CHANGELOG
22
------------------------------------------------
33

4+
## Version 0.2.0
5+
6+
### New Features
7+
- Entry
8+
- locale - function for passing locale is added
9+
- only - function for getting only specified field
10+
- except - function for getting field except specified field
11+
- include_reference - function for including reference in entry
12+
- include_schema - function for including schema along with entry added
13+
- include_content_type - function for including content type details along with entry added
14+
- include_owner - function for getting owner of entry
15+
- include_fallback - function for getting published fallback locale content, if specified locale content is not published
16+
17+
- Query
18+
- include_fallback - function for getting published fallback locale content, if specified locale content is not published
19+
20+
21+
------------------------------------------------
22+
423
## Version 0.1.0
524

625
### Bug

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ include:
2222

2323
Examples of unacceptable behavior by participants include:
2424

25-
* The use of sexualized language or imagery and unwelcome sexual attention or
26-
advances
25+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
2726
* Trolling, insulting/derogatory comments, and personal or political attacks
2827
* Public or private harassment
2928
* Publishing others' private information, such as a physical or electronic

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ source "https://rubygems.org"
44
gem "rspec"
55
gem "webmock"
66
gem "simplecov"
7-
gem "activesupport"
7+
gem "activesupport"
8+
gem "yard"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ GEM
4444
addressable (>= 2.3.6)
4545
crack (>= 0.3.2)
4646
hashdiff (>= 0.4.0, < 2.0.0)
47+
yard (0.9.25)
4748
zeitwerk (2.3.0)
4849

4950
PLATFORMS
@@ -54,6 +55,7 @@ DEPENDENCIES
5455
rspec
5556
simplecov
5657
webmock
58+
yard
5759

5860
BUNDLED WITH
5961
2.1.4

lib/contentstack/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def self.fetch_entries(content_type, query)
2929
send_request(path, query)
3030
end
3131

32-
def self.fetch_entry(content_type, entry_uid)
32+
def self.fetch_entry(content_type, entry_uid, query)
3333
path = "/content_types/#{content_type}/entries/#{entry_uid}"
34-
send_request(path)
34+
send_request(path, query)
3535
end
3636

3737
def self.get_assets(asset_uid=nil)

lib/contentstack/content_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def self.all
4444
end
4545
end
4646

47-
def self.find_by_uid(uid)
47+
def fetch
4848
content_type = API.fetch_content_types(uid)["content_type"]
4949
ContentType.new(content_type.inject({}){|clone,(k,v)| clone[k.to_sym] = v; clone})
5050
end

lib/contentstack/entry.rb

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,178 @@
22

33
module Contentstack
44
class Entry
5-
attr_reader :fields, :content_type, :uid, :owner
5+
attr_reader :fields, :content_type, :uid, :owner, :query, :schema, :content_type
66
def initialize(attrs, content_type_uid=nil)
77
setup(attrs, content_type_uid)
88
end
99

10+
# Get entries from the specified locale.
11+
#
12+
# @param [String] code The locale code of the entry
13+
#
14+
# Example
15+
# @entry = @stack.content_type('category').entry(entry_uid)
16+
# @entry.locale('en-us')
17+
#
18+
# @return [Contentstack::Entry]
19+
def locale(code)
20+
@query[:locale] = code
21+
self
22+
end
23+
24+
# Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
25+
#
26+
# @param [Array] fields Array of the 'only' reference keys to be included in response.
27+
# @param [Array] fields_with_base Can be used to denote 'only' fields of the reference class
28+
#
29+
# Example
30+
# # Include only title and description field in response
31+
# @entry = @stack.content_type('category').entry(entry_uid)
32+
# @entry.only(['title', 'description'])
33+
#
34+
# # Query product and include only the title and description from category reference
35+
# @entry = @stack.content_type('product').entry(entry_uid)
36+
# @entry.include_reference('category')
37+
# .only('category', ['title', 'description'])
38+
#
39+
# @return [Contentstack::Entry]
40+
def only(fields, fields_with_base=nil)
41+
q = {}
42+
if [Array, String].include?(fields_with_base.class)
43+
fields_with_base = [fields_with_base] if fields_with_base.class == String
44+
q[fields.to_sym] = fields_with_base
45+
else
46+
fields = [fields] if fields.class == String
47+
q = {BASE: fields}
48+
end
49+
50+
@query[:only] = q
51+
self
52+
end
53+
54+
# Specifies list of field uids that would be 'excluded' from the response.
55+
#
56+
# @param [Array] fields Array of field uid which get 'excluded' from the response.
57+
# @param [Array] fields_with_base Can be used to denote 'except' fields of the reference class
58+
#
59+
# Example
60+
# # Exclude 'description' field in response
61+
# @entry = @stack.content_type('category').entry(entry_uid)
62+
# @entry.except(['description'])
63+
#
64+
# # Query product and exclude the 'description' from category reference
65+
# @entry = @stack.content_type('product').entry(entry_uid)
66+
# @entry.include_reference('category')
67+
# .except('category', ['description'])
68+
#
69+
# @return [Contentstack::Entry]
70+
def except(fields, fields_with_base=nil)
71+
q = {}
72+
if [Array, String].include?(fields_with_base.class)
73+
fields_with_base = [fields_with_base] if fields_with_base.class == String
74+
q[fields.to_sym] = fields_with_base
75+
else
76+
fields = [fields] if fields.class == String
77+
q = {BASE: fields}
78+
end
79+
80+
@query[:except] = q
81+
self
82+
end
83+
84+
# Add a constraint that requires a particular reference key details.
85+
#
86+
# @param [String/Array] reference_field_uids Pass string or array of reference fields that must be included in the response
87+
#
88+
# Example
89+
#
90+
# # Include reference of 'category'
91+
# @entry = @stack.content_type('product').entry(entry_uid)
92+
# @entry.include_reference('category')
93+
#
94+
# # Include reference of 'category' and 'reviews'
95+
# @entry = @stack.content_type('product').entry(entry_uid)
96+
# @entry.include_reference(['category', 'reviews'])
97+
#
98+
# @return [Contentstack::Entry]
99+
def include_reference(reference_field_uids)
100+
self.include(reference_field_uids)
101+
end
102+
103+
# Include schemas of all returned objects along with objects themselves.
104+
#
105+
# Example
106+
#
107+
# @entry = @stack.content_type('product').entry(entry_uid)
108+
# @entry.include_schema
109+
#
110+
# @return [Contentstack::Entry]
111+
def include_schema(flag=true)
112+
@query[:include_schema] = flag
113+
self
114+
end
115+
116+
# Include object owner's profile in the objects data.
117+
#
118+
# Example
119+
#
120+
# @entry = @stack.content_type('product').entry(entry_uid)
121+
# @entry.include_owner
122+
#
123+
# @return [Contentstack::Entry]
124+
def include_owner(flag=true)
125+
@query[:include_owner] = flag
126+
self
127+
end
128+
129+
# Include object's content_type in response
130+
#
131+
# Example
132+
#
133+
# @entry = @stack.content_type('product').entry(entry_uid)
134+
# @entry.include_content_type
135+
#
136+
# @return [Contentstack::Entry]
137+
def include_content_type(flag=true)
138+
@query[:include_content_type] = flag
139+
self
140+
end
141+
142+
# Include the fallback locale publish content, if specified locale content is not publish.
143+
#
144+
# Example
145+
#
146+
# @entry = @stack.content_type('product').entry(entry_uid)
147+
# @entry.include_fallback
148+
#
149+
# @return [Contentstack::Entry]
150+
def include_fallback(flag=true)
151+
@query[:include_fallback] = flag
152+
self
153+
end
154+
155+
#
156+
# @return [Contentstack::Query]
157+
def include(field_uids)
158+
field_uids = [field_uids] if field_uids.class == String
159+
@query[:include] ||= []
160+
@query[:include] = @query[:include] | field_uids
161+
self
162+
end
163+
164+
# Execute entry
165+
#
166+
# Example
167+
#
168+
# @entry = @stack.content_type('product').entry(entry_uid)
169+
# @entry.fetch
170+
#
171+
# @return [Contentstack::EntryCollection]
10172
def fetch
11-
entry = API.fetch_entry(@content_type, self.fields[:uid])
173+
entry = API.fetch_entry(@content_type, self.fields[:uid], @query)
12174
setup(entry["entry"])
175+
@schema = entry["schema"].symbolize_keys if entry["schema"]
176+
@content_type = entry["content_type"].symbolize_keys if entry["content_type"]
13177
self
14178
end
15179

@@ -24,6 +188,7 @@ def setup(attrs, content_type_uid=nil)
24188
@content_type = content_type_uid if !content_type_uid.blank?
25189
@owner = attrs[:_owner] if attrs[:_owner]
26190
@uid = attrs[:uid]
191+
@query = {}
27192
end
28193
end
29194
end

lib/contentstack/query.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,20 @@ def include_content_type(flag=true)
502502
self
503503
end
504504

505+
506+
# Include the fallback locale publish content, if specified locale content is not publish.
507+
#
508+
# Example
509+
#
510+
# @query = @stack.content_type('product').query
511+
# @query.include_fallback
512+
#
513+
# @return [Contentstack::Query]
514+
def include_fallback(flag=true)
515+
@query[:include_fallback] = flag
516+
self
517+
end
518+
505519
# Include objects in 'Draft' mode in response
506520
#
507521
# Example

lib/contentstack/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Contentstack
2-
VERSION = "0.1.0"
2+
VERSION = "0.2.0"
33
end

0 commit comments

Comments
 (0)