contentful_model 0.1.7.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/lib/contentful_model.rb +4 -1
- data/lib/contentful_model/base.rb +26 -21
- data/lib/contentful_model/chainable_queries.rb +28 -3
- data/lib/contentful_model/client.rb +2 -2
- data/lib/contentful_model/errors.rb +3 -2
- data/lib/contentful_model/manageable.rb +135 -0
- data/lib/contentful_model/management.rb +7 -0
- data/lib/contentful_model/migrations/content_type.rb +127 -0
- data/lib/contentful_model/migrations/content_type_factory.rb +27 -0
- data/lib/contentful_model/migrations/errors.rb +5 -0
- data/lib/contentful_model/migrations/migration.rb +25 -0
- data/lib/contentful_model/queries.rb +1 -3
- data/lib/contentful_model/query.rb +2 -2
- data/lib/contentful_model/validations/validates_presence_of.rb +21 -5
- data/lib/contentful_model/validations/validations.rb +5 -19
- data/lib/contentful_model/version.rb +1 -1
- metadata +111 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb158dcb23427ac8201185b1201a237ba7dfa0a5
|
4
|
+
data.tar.gz: 7f4e3ac0d68637daf9bb60662c85476ea6e8916a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a69c4da7a8cb02d67af7e4685bcff46f363d4fe63137d30a6a554f7d6c464314ae5980ea3bd2955d61145ee1120b5235675059e5e25c11b2ea986648b983017
|
7
|
+
data.tar.gz: a6b37e8649735c82a5ee6f7439291ceb45b21597e3fc8c0dfe0edf8b483b963af10c07f38a5f2272c876bf9cd4ec2ef1e8419c544379964ccaa2f847077a9ed4
|
data/MIT-LICENSE
CHANGED
data/lib/contentful_model.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'require_all'
|
2
|
+
require 'contentful/management'
|
2
3
|
require 'contentful'
|
3
4
|
require_rel '.'
|
4
5
|
|
@@ -23,7 +24,9 @@ module ContentfulModel
|
|
23
24
|
attr_accessor :access_token,
|
24
25
|
:preview_access_token,
|
25
26
|
:space,
|
26
|
-
:entry_mapping
|
27
|
+
:entry_mapping,
|
28
|
+
:management_token,
|
29
|
+
:default_locale
|
27
30
|
|
28
31
|
def initialize
|
29
32
|
@entry_mapping ||= {}
|
@@ -1,19 +1,41 @@
|
|
1
1
|
module ContentfulModel
|
2
|
-
class Base < Contentful::
|
2
|
+
class Base < Contentful::DynamicEntry
|
3
3
|
include ContentfulModel::ChainableQueries
|
4
4
|
include ContentfulModel::Associations
|
5
5
|
include ContentfulModel::Validations
|
6
|
+
include ContentfulModel::Manageable
|
6
7
|
|
7
8
|
def initialize(*args)
|
8
9
|
super
|
10
|
+
define_getters
|
9
11
|
self.class.coercions ||= {}
|
10
12
|
end
|
11
13
|
|
14
|
+
private
|
15
|
+
|
16
|
+
def define_getters
|
17
|
+
fields.each do |k, v|
|
18
|
+
if Contentful::Constants::KNOWN_LOCALES.include?(k.to_s)
|
19
|
+
v.keys.each do |name|
|
20
|
+
define_getter(name)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
define_getter(k)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def define_getter(name)
|
29
|
+
define_singleton_method "#{name.to_s.underscore}" do
|
30
|
+
fields(default_locale)[name]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
12
34
|
#use method_missing to call fields on the model
|
13
35
|
def method_missing(method, *args, &block)
|
14
36
|
result = fields[:"#{method.to_s.camelize(:lower)}"]
|
15
37
|
# we need to pull out any Contentful::Link references, and also things which don't have any fields at all
|
16
|
-
# because they're newly created
|
38
|
+
# because they're newly created
|
17
39
|
if result.is_a?(Array)
|
18
40
|
result.reject! {|r| r.is_a?(Contentful::Link) || (r.respond_to?(:invalid) && r.invalid?)}
|
19
41
|
elsif result.is_a?(Contentful::Link)
|
@@ -24,8 +46,6 @@ module ContentfulModel
|
|
24
46
|
result = nil
|
25
47
|
end
|
26
48
|
|
27
|
-
|
28
|
-
|
29
49
|
if result.nil?
|
30
50
|
# if self.class.rescue_from_no_attribute_fields.member?()
|
31
51
|
# end
|
@@ -66,15 +86,8 @@ module ContentfulModel
|
|
66
86
|
end
|
67
87
|
|
68
88
|
"#{self.class.to_s.underscore}/#{self.id}-#{self.updated_at.utc.to_s(:number)}"
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
def save
|
73
|
-
raise NotImplementedError, "Saving models isn't implemented; we need to use the Contentful Management API for that. Pull requests welcome!"
|
74
89
|
end
|
75
90
|
|
76
|
-
alias_method :create, :save
|
77
|
-
|
78
91
|
class << self
|
79
92
|
attr_accessor :content_type_id, :coercions, :return_nil_for_empty_attribute_fields
|
80
93
|
|
@@ -84,7 +97,7 @@ module ContentfulModel
|
|
84
97
|
|
85
98
|
def add_entry_mapping
|
86
99
|
unless ContentfulModel.configuration.entry_mapping.has_key?(@content_type_id)
|
87
|
-
ContentfulModel.configuration.entry_mapping[@content_type_id] =
|
100
|
+
ContentfulModel.configuration.entry_mapping[@content_type_id] = self.to_s.constantize
|
88
101
|
end
|
89
102
|
end
|
90
103
|
|
@@ -96,7 +109,6 @@ module ContentfulModel
|
|
96
109
|
else
|
97
110
|
@client ||= ContentfulModel::Client.new(ContentfulModel.configuration.to_hash)
|
98
111
|
end
|
99
|
-
|
100
112
|
end
|
101
113
|
|
102
114
|
def content_type
|
@@ -115,7 +127,7 @@ module ContentfulModel
|
|
115
127
|
@return_nil_for_empty_attribute_fields ||= []
|
116
128
|
|
117
129
|
fields.each do |field|
|
118
|
-
define_method field do
|
130
|
+
define_method field do
|
119
131
|
begin
|
120
132
|
super()
|
121
133
|
rescue ContentfulModel::AttributeNotFoundError
|
@@ -126,13 +138,6 @@ module ContentfulModel
|
|
126
138
|
@return_nil_for_empty_attribute_fields.push(field)
|
127
139
|
end
|
128
140
|
end
|
129
|
-
|
130
141
|
end
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
142
|
end
|
138
143
|
end
|
@@ -8,7 +8,12 @@ module ContentfulModel
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
10
|
def all
|
11
|
-
raise ArgumentError,
|
11
|
+
raise ArgumentError, 'You need to set self.content_type in your model class' if @content_type_id.nil?
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def params(options)
|
16
|
+
@query << options
|
12
17
|
self
|
13
18
|
end
|
14
19
|
|
@@ -27,6 +32,16 @@ module ContentfulModel
|
|
27
32
|
self
|
28
33
|
end
|
29
34
|
|
35
|
+
def locale(locale_code)
|
36
|
+
@query << {'locale' => locale_code}
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_children(n)
|
41
|
+
@query << {'include' => n}
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
30
45
|
def order(args)
|
31
46
|
prefix = ''
|
32
47
|
if args.is_a?(Hash)
|
@@ -35,6 +50,8 @@ module ContentfulModel
|
|
35
50
|
elsif args.is_a?(Symbol)
|
36
51
|
column = args.to_s
|
37
52
|
prefix = ''
|
53
|
+
else
|
54
|
+
column = args.to_s
|
38
55
|
end
|
39
56
|
@query << {'order' => "#{prefix}fields.#{column.camelize(:lower)}"}
|
40
57
|
self
|
@@ -47,7 +64,7 @@ module ContentfulModel
|
|
47
64
|
#query is a hash
|
48
65
|
if query.values.first.is_a?(Array) #we need to do an 'in' query
|
49
66
|
@query << {"fields.#{query.keys.first.to_s.camelize(:lower)}[in]" => query.values.first.join(",")}
|
50
|
-
elsif query.values.first.is_a?(String) || [TrueClass,FalseClass].member?(query.values.first.class)
|
67
|
+
elsif query.values.first.is_a?(String) || query.values.first.is_a?(Numeric) || [TrueClass,FalseClass].member?(query.values.first.class)
|
51
68
|
@query << {"fields.#{query.keys.first.to_s.camelize(:lower)}" => query.values.first}
|
52
69
|
elsif query.values.first.is_a?(Hash)
|
53
70
|
# if the search is a hash, use the key to specify the search field operator
|
@@ -65,7 +82,15 @@ module ContentfulModel
|
|
65
82
|
def search(parameters)
|
66
83
|
if parameters.is_a?(Hash)
|
67
84
|
parameters.each do |field, search|
|
68
|
-
|
85
|
+
# if the search is a hash, use the key to specify the search field operator
|
86
|
+
# For example
|
87
|
+
# Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now
|
88
|
+
if search.is_a?(Hash)
|
89
|
+
search_key, search_value = *search.flatten
|
90
|
+
@query << {"fields.#{field.to_s.camelize(:lower)}[#{search_key}]" => search_value}
|
91
|
+
else
|
92
|
+
@query << {"fields.#{field.to_s.camelize(:lower)}[match]" => search}
|
93
|
+
end
|
69
94
|
end
|
70
95
|
elsif parameters.is_a?(String)
|
71
96
|
@query << {"query" => parameters}
|
@@ -8,7 +8,7 @@ module ContentfulModel
|
|
8
8
|
configuration[:api_url] = PREVIEW_API_URL
|
9
9
|
configuration[:access_token] = configuration[:preview_access_token]
|
10
10
|
end
|
11
|
-
super(configuration)
|
11
|
+
super({ raise_errors: true, dynamic_entries: :auto }.merge(configuration))
|
12
12
|
end
|
13
13
|
end
|
14
|
-
end
|
14
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
module ContentfulModel
|
2
|
+
module Manageable
|
3
|
+
attr_reader :dirty
|
4
|
+
|
5
|
+
def initialize(*args)
|
6
|
+
super
|
7
|
+
@dirty = false
|
8
|
+
@changed_fields = []
|
9
|
+
define_setters
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_management(entry_to_update = management_entry)
|
13
|
+
published_entry = self.class.client.entry(id)
|
14
|
+
fields.each do |field, value|
|
15
|
+
entry_to_update.send(
|
16
|
+
"#{field.to_s.underscore}=",
|
17
|
+
management_field_value(
|
18
|
+
@changed_fields.include?(field) ? value : published_entry.send(field.to_s.underscore)
|
19
|
+
)
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
entry_to_update
|
24
|
+
end
|
25
|
+
|
26
|
+
def refetch_management_entry
|
27
|
+
@management_entry = fetch_management_entry
|
28
|
+
end
|
29
|
+
|
30
|
+
def save
|
31
|
+
begin
|
32
|
+
to_management.save
|
33
|
+
rescue Contentful::Management::Conflict
|
34
|
+
# Retries with re-fetched entry
|
35
|
+
to_management(refetch_management_entry).save
|
36
|
+
end
|
37
|
+
|
38
|
+
@dirty = false
|
39
|
+
@changed_fields = []
|
40
|
+
|
41
|
+
self
|
42
|
+
rescue Contentful::Management::Conflict
|
43
|
+
fail ContentfulModel::VersionMismatchError, "Version Mismatch persisting after refetch attempt, use :refetch_management_entry and try again later."
|
44
|
+
end
|
45
|
+
|
46
|
+
def publish
|
47
|
+
begin
|
48
|
+
to_management.publish
|
49
|
+
rescue Contentful::Management::Conflict
|
50
|
+
to_management(refetch_management_entry).save
|
51
|
+
end
|
52
|
+
|
53
|
+
self
|
54
|
+
rescue Contentful::Management::Conflict
|
55
|
+
fail ContentfulModel::VersionMismatchError, "Version Mismatch persisting after refetch attempt, use :refetch_management_entry and try again later."
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def management_space
|
61
|
+
@management_space ||= self.class.management(default_locale: locale).spaces.find(space.id)
|
62
|
+
end
|
63
|
+
|
64
|
+
def fetch_management_entry
|
65
|
+
management_space.entries.find(id)
|
66
|
+
end
|
67
|
+
|
68
|
+
def management_entry
|
69
|
+
# Fetches always in case of Version changes to avoid Validation errors
|
70
|
+
@management_entry ||= fetch_management_entry
|
71
|
+
end
|
72
|
+
|
73
|
+
def define_setters
|
74
|
+
fields.each do |k, v|
|
75
|
+
if Contentful::Constants::KNOWN_LOCALES.include?(k.to_s)
|
76
|
+
v.keys.each do |name|
|
77
|
+
define_setter(name)
|
78
|
+
end
|
79
|
+
else
|
80
|
+
define_setter(k)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def define_setter(name)
|
86
|
+
define_singleton_method "#{name.to_s.underscore}=" do |value|
|
87
|
+
@dirty = true
|
88
|
+
@changed_fields << name
|
89
|
+
fields(default_locale)[name] = value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def management_field_value(entry_value)
|
94
|
+
case entry_value
|
95
|
+
when Contentful::Entry
|
96
|
+
Contentful::Management::Entry.hash_with_link_object('Entry', entry_value)
|
97
|
+
when Contentful::Asset
|
98
|
+
Contentful::Management::Entry.hash_with_link_object('Asset', entry_value)
|
99
|
+
when Contentful::Link
|
100
|
+
Contentful::Management::Entry.hash_with_link_object(entry_value.sys[:contentType], entry_value)
|
101
|
+
else
|
102
|
+
entry_value
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
module ClassMethods
|
107
|
+
def management(options = {})
|
108
|
+
@management ||= ContentfulModel::Management.new(
|
109
|
+
options.merge(default_locale: ContentfulModel.configuration.default_locale)
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
def create(values = {}, publish = false)
|
114
|
+
entry = management.entries.create(
|
115
|
+
management.content_types.find(
|
116
|
+
ContentfulModel.configuration.space,
|
117
|
+
content_type_id
|
118
|
+
),
|
119
|
+
values
|
120
|
+
)
|
121
|
+
|
122
|
+
if publish
|
123
|
+
entry.publish
|
124
|
+
entry = self.find(entry.id)
|
125
|
+
end
|
126
|
+
|
127
|
+
entry
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.included(base)
|
132
|
+
base.extend(ClassMethods)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
module ContentfulModel
|
2
|
+
module Migrations
|
3
|
+
class ContentType
|
4
|
+
def initialize(name = nil, management_content_type = nil)
|
5
|
+
@name = name
|
6
|
+
@management_content_type = management_content_type
|
7
|
+
end
|
8
|
+
|
9
|
+
def save
|
10
|
+
if new?
|
11
|
+
@management_content_type = management.content_types.create(
|
12
|
+
ContentfulModel.configuration.space,
|
13
|
+
name: @name,
|
14
|
+
fields: fields
|
15
|
+
)
|
16
|
+
else
|
17
|
+
@management_content_type.fields = @fields
|
18
|
+
@management_content_type.save
|
19
|
+
end
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def publish
|
25
|
+
return self if new?
|
26
|
+
|
27
|
+
@management_content_type.publish
|
28
|
+
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def field(name, type)
|
33
|
+
name = name.to_s
|
34
|
+
type = type.to_s
|
35
|
+
|
36
|
+
new_field = Contentful::Management::Field.new
|
37
|
+
new_field.id = name.split(' ').map(&:capitalize).join('').underscore
|
38
|
+
new_field.name = name
|
39
|
+
new_field.type = management_type(type)
|
40
|
+
new_field.link_type = management_link_type(type) if link?(type)
|
41
|
+
new_field.items = management_items(type) if array?(type)
|
42
|
+
|
43
|
+
fields << new_field
|
44
|
+
|
45
|
+
new_field
|
46
|
+
end
|
47
|
+
|
48
|
+
def remove_field(field_id)
|
49
|
+
@management_content_type.fields.destroy(field_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def new?
|
53
|
+
@management_content_type.nil? || @management_content_type.id.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
def fields
|
57
|
+
if new?
|
58
|
+
return @fields ||= []
|
59
|
+
else
|
60
|
+
return @fields ||= fields_from_management_type
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def fields_from_management_type
|
67
|
+
@management_content_type.fields
|
68
|
+
end
|
69
|
+
|
70
|
+
def management_type(type)
|
71
|
+
if [
|
72
|
+
:text, :symbol,
|
73
|
+
:integer, :number,
|
74
|
+
:date, :boolean,
|
75
|
+
:location, :object
|
76
|
+
].include?(type.to_sym)
|
77
|
+
return type.capitalize
|
78
|
+
elsif type == 'string'
|
79
|
+
return 'Symbol'
|
80
|
+
elsif link?(type)
|
81
|
+
return 'Link'
|
82
|
+
elsif array?(type)
|
83
|
+
return 'Array'
|
84
|
+
else
|
85
|
+
raise_field_type_error(type)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def management_link_type(type)
|
90
|
+
if [:entry_link, :asset_link].include?(type.to_sym)
|
91
|
+
return type.split('_').first.capitalize
|
92
|
+
else
|
93
|
+
raise_field_type_error(type)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def management_items(type)
|
98
|
+
if [:entry_array, :asset_array].include?(type.to_sym)
|
99
|
+
items = Contentful::Management::Field.new
|
100
|
+
items.type = 'Link'
|
101
|
+
items.link_type = type.split('_').first.capitalize
|
102
|
+
|
103
|
+
return items
|
104
|
+
else
|
105
|
+
raise_field_type_error(type)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def link?(type)
|
110
|
+
type.end_with?('_link')
|
111
|
+
end
|
112
|
+
|
113
|
+
def array?(type)
|
114
|
+
type.end_with?('_array')
|
115
|
+
end
|
116
|
+
|
117
|
+
def management
|
118
|
+
@management ||= ContentfulModel::Management.new
|
119
|
+
end
|
120
|
+
|
121
|
+
def raise_field_type_error(type)
|
122
|
+
fail ContentfulModel::Migrations::InvalidFieldTypeError, "`:#{type}' is not a valid Field Type"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ContentfulModel
|
2
|
+
module Migrations
|
3
|
+
module ContentTypeFactory
|
4
|
+
def self.create(name, fields = {}, &block)
|
5
|
+
content_type = ContentfulModel::Migrations::ContentType.new(name)
|
6
|
+
|
7
|
+
yield(content_type) if block_given?
|
8
|
+
|
9
|
+
fields.each do |name, type|
|
10
|
+
content_type.field(name, type)
|
11
|
+
end
|
12
|
+
|
13
|
+
content_type.save
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.find(content_type_id)
|
17
|
+
ContentfulModel::Migrations::ContentType.new(
|
18
|
+
nil,
|
19
|
+
ContentfulModel::Management.new.content_types.find(
|
20
|
+
ContentfulModel.configuration.space,
|
21
|
+
content_type_id
|
22
|
+
)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ContentfulModel
|
2
|
+
module Migrations
|
3
|
+
module Migration
|
4
|
+
def create_content_type(name, fields = {}, &block)
|
5
|
+
ContentfulModel::Migrations::ContentTypeFactory.create(name, fields, &block)
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_content_type_field(content_type_id, name, type)
|
9
|
+
content_type = ContentfulModel::Migrations::ContentTypeFactory.find(content_type_id)
|
10
|
+
|
11
|
+
content_type.field(name, type)
|
12
|
+
|
13
|
+
content_type.save
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove_content_type_field(content_type_id, field_id)
|
17
|
+
content_type = ContentfulModel::Migrations::ContentTypeFactory.find(content_type_id)
|
18
|
+
|
19
|
+
content_type.remove_field(field_id)
|
20
|
+
|
21
|
+
content_type.save
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -5,13 +5,12 @@ module ContentfulModel
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
|
9
8
|
def inherited(subclass)
|
10
9
|
instantiate_query(subclass)
|
11
10
|
end
|
12
11
|
|
13
12
|
def instantiate_query(klass)
|
14
|
-
klass.instance_variable_set(:"@query",ContentfulModel::Query.new(klass))
|
13
|
+
klass.instance_variable_set(:"@query", ContentfulModel::Query.new(klass))
|
15
14
|
end
|
16
15
|
|
17
16
|
def load
|
@@ -24,7 +23,6 @@ module ContentfulModel
|
|
24
23
|
@query << {'sys.id' => id}
|
25
24
|
load.first
|
26
25
|
end
|
27
|
-
|
28
26
|
end
|
29
27
|
end
|
30
28
|
end
|
@@ -15,7 +15,7 @@ module ContentfulModel
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def execute
|
18
|
-
query = @parameters.merge
|
18
|
+
query = @parameters.merge(default_parameters)
|
19
19
|
result = client.send(:entries,query)
|
20
20
|
return result.to_a.reject {|entity| entity.is_a?(Contentful::Link) || entity.invalid?}
|
21
21
|
end
|
@@ -28,4 +28,4 @@ module ContentfulModel
|
|
28
28
|
@parameters = default_parameters
|
29
29
|
end
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
@@ -7,12 +7,28 @@ module Contentful
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
def validates_presence_of(*args)
|
10
|
-
@validations ||=
|
11
|
-
@validations
|
12
|
-
@validations[:presence].push(args)
|
13
|
-
@validations[:presence].flatten!
|
10
|
+
@validations ||= []
|
11
|
+
@validations << PresenceValidation.new(args)
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
15
|
+
|
16
|
+
class PresenceValidation
|
17
|
+
attr_reader :fields
|
18
|
+
|
19
|
+
def initialize(fields)
|
20
|
+
@fields = fields
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate(entry)
|
24
|
+
errors = []
|
25
|
+
|
26
|
+
fields.each do |field|
|
27
|
+
errors << "#{field} is required" unless entry.respond_to?(field)
|
28
|
+
end
|
29
|
+
|
30
|
+
errors
|
31
|
+
end
|
32
|
+
end
|
17
33
|
end
|
18
|
-
end
|
34
|
+
end
|
@@ -17,7 +17,7 @@ module ContentfulModel
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def validate
|
20
|
-
@errors
|
20
|
+
@errors = []
|
21
21
|
unless self.respond_to?(:fields)
|
22
22
|
@errors.push("Entity doesn't respond to the fields() method")
|
23
23
|
return false
|
@@ -25,26 +25,12 @@ module ContentfulModel
|
|
25
25
|
|
26
26
|
validations = self.class.send(:validations)
|
27
27
|
unless validations.nil?
|
28
|
-
validations.each do |
|
29
|
-
|
30
|
-
# validates_presence_of
|
31
|
-
when :presence
|
32
|
-
fields.each do |field|
|
33
|
-
unless self.respond_to?(field)
|
34
|
-
@errors << "#{field} is required"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
28
|
+
validations.each do |validation|
|
29
|
+
@errors += validation.validate(self)
|
38
30
|
end
|
39
31
|
end
|
40
32
|
|
41
|
-
|
42
|
-
if @errors.empty?
|
43
|
-
return true
|
44
|
-
else
|
45
|
-
return false
|
46
|
-
end
|
47
|
-
|
33
|
+
@errors.empty?
|
48
34
|
end
|
49
35
|
|
50
36
|
module ClassMethods
|
@@ -53,4 +39,4 @@ module ContentfulModel
|
|
53
39
|
end
|
54
40
|
end
|
55
41
|
end
|
56
|
-
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contentful_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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:
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: contentful
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '0.9'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: contentful-management
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: redcarpet
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +80,90 @@ dependencies:
|
|
66
80
|
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: vcr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: guard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: guard-rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: webmock
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
69
167
|
description: A wrapper around the Contentful gem to give you a base class to inherit
|
70
168
|
your models from
|
71
169
|
email:
|
@@ -87,6 +185,12 @@ files:
|
|
87
185
|
- lib/contentful_model/chainable_queries.rb
|
88
186
|
- lib/contentful_model/client.rb
|
89
187
|
- lib/contentful_model/errors.rb
|
188
|
+
- lib/contentful_model/manageable.rb
|
189
|
+
- lib/contentful_model/management.rb
|
190
|
+
- lib/contentful_model/migrations/content_type.rb
|
191
|
+
- lib/contentful_model/migrations/content_type_factory.rb
|
192
|
+
- lib/contentful_model/migrations/errors.rb
|
193
|
+
- lib/contentful_model/migrations/migration.rb
|
90
194
|
- lib/contentful_model/queries.rb
|
91
195
|
- lib/contentful_model/query.rb
|
92
196
|
- lib/contentful_model/validations/validates_presence_of.rb
|
@@ -113,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
217
|
version: '0'
|
114
218
|
requirements: []
|
115
219
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.
|
220
|
+
rubygems_version: 2.5.1
|
117
221
|
signing_key:
|
118
222
|
specification_version: 4
|
119
223
|
summary: A thin wrapper for Contentful gem
|