contentful_model 0.1.5 → 0.1.6

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: 8400d7ed706da1984ae22bff94c4c428ecb2b9e7
4
- data.tar.gz: 5021acd89b16206ffecef168e3c389691018234b
3
+ metadata.gz: 038d352d42cc8f428379d27b2db70bccb7661244
4
+ data.tar.gz: 1b370619c14aecd9ee170ff0f82b72315a9cef7c
5
5
  SHA512:
6
- metadata.gz: 0e3111eb0c5bf198a29bd349cc66cd8231fa2dd996b152a5666253e00d722a26d8c1ddd4b148100d2cfa050fb8fa453a5cead625f65445438af7002c6c5aa85a
7
- data.tar.gz: fb553a246fd47575f8adbc4232dd23b5facf38b1f0d3affad9b86f1bc48e8bc7be36977f61f7a4470220605429d8f7fbaad786f72bf6e1e3ec8bafb153e8b682
6
+ metadata.gz: 0692c693706a5211fc011398e410dc79b70557cfd8f260950f56548a0136875da82f6d7aaf6cbecdf45f2a9f4d1e26419d7c0d24249db480b80d78ab004c6313
7
+ data.tar.gz: 3e20613dea10448a65e96a6f449389f6683dc967016ed26a9a0ba868247f8f90523709bbe5cffa6322da4cd7862d8871b9bfd38afdeaa2f1c1ff916d46565ce5
@@ -60,7 +60,7 @@ module ContentfulModel
60
60
  # Return the last member of the enumerable, which is the root
61
61
  # @return the root instance of this object
62
62
  define_method :root do
63
- find_ancestors.last
63
+ find_ancestors.to_a.last
64
64
  end
65
65
 
66
66
  # @return [Boolean] whether or not this instance has children
@@ -12,7 +12,13 @@ module ContentfulModel
12
12
  def method_missing(method, *args, &block)
13
13
  result = fields[:"#{method.to_s.camelize(:lower)}"]
14
14
  if result.nil?
15
- raise ContentfulModel::AttributeNotFoundError, "no attribute #{method} found"
15
+ # if self.class.rescue_from_no_attribute_fields.member?()
16
+ # end
17
+ if self.class.return_nil_for_empty_attribute_fields && self.class.return_nil_for_empty_attribute_fields.include?(method)
18
+ return nil
19
+ else
20
+ raise ContentfulModel::AttributeNotFoundError, "no attribute #{method} found"
21
+ end
16
22
  else
17
23
  # if there's no coercion specified, return the result
18
24
  if self.class.coercions[method].nil?
@@ -56,7 +62,7 @@ module ContentfulModel
56
62
  alias_method :create, :save
57
63
 
58
64
  class << self
59
- attr_accessor :content_type_id, :coercions
65
+ attr_accessor :content_type_id, :coercions, :return_nil_for_empty_attribute_fields
60
66
 
61
67
  def descendents
62
68
  ObjectSpace.each_object(Class).select { |klass| klass < self }
@@ -69,8 +75,14 @@ module ContentfulModel
69
75
  end
70
76
 
71
77
  def client
78
+ # add an entry mapping for this content type
72
79
  self.add_entry_mapping
73
- @client ||= Contentful::Client.new(ContentfulModel.configuration.to_hash)
80
+ if ContentfulModel.use_preview_api
81
+ @preview_client ||= ContentfulModel::Client.new(ContentfulModel.configuration.to_hash)
82
+ else
83
+ @client ||= ContentfulModel::Client.new(ContentfulModel.configuration.to_hash)
84
+ end
85
+
74
86
  end
75
87
 
76
88
  def content_type
@@ -85,6 +97,22 @@ module ContentfulModel
85
97
  @coercions
86
98
  end
87
99
 
100
+ def return_nil_for_empty(*fields)
101
+ @return_nil_for_empty_attribute_fields ||= []
102
+
103
+ fields.each do |field|
104
+ define_method field do
105
+ begin
106
+ super()
107
+ rescue ContentfulModel::AttributeNotFoundError
108
+ nil
109
+ end
110
+ end
111
+
112
+ @return_nil_for_empty_attribute_fields.push(field)
113
+ end
114
+ end
115
+
88
116
  end
89
117
 
90
118
 
@@ -93,4 +121,4 @@ module ContentfulModel
93
121
 
94
122
 
95
123
  end
96
- end
124
+ end
@@ -22,6 +22,11 @@ module ContentfulModel
22
22
  self
23
23
  end
24
24
 
25
+ def limit(n)
26
+ @query << {'limit' => n}
27
+ self
28
+ end
29
+
25
30
  def order(args)
26
31
  prefix = ''
27
32
  if args.is_a?(Hash)
@@ -32,7 +37,6 @@ module ContentfulModel
32
37
  prefix = ''
33
38
  end
34
39
  @query << {'order' => "#{prefix}fields.#{column.camelize(:lower)}"}
35
- puts @query.inspect
36
40
  self
37
41
  end
38
42
 
@@ -43,8 +47,16 @@ module ContentfulModel
43
47
  #query is a hash
44
48
  if query.values.first.is_a?(Array) #we need to do an 'in' query
45
49
  @query << {"fields.#{query.keys.first.to_s.camelize(:lower)}[in]" => query.values.first.join(",")}
46
- elsif query.values.first.is_a?(String)
50
+ elsif query.values.first.is_a?(String) || [TrueClass,FalseClass].member?(query.values.first.class)
47
51
  @query << {"fields.#{query.keys.first.to_s.camelize(:lower)}" => query.values.first}
52
+ elsif query.values.first.is_a?(Hash)
53
+ # if the search is a hash, use the key to specify the search field operator
54
+ # For example
55
+ # Model.search(start_date: {gte: DateTime.now}) => "fields.start_date[gte]" => DateTime.now
56
+ query.each do |field, condition|
57
+ search_predicate, search_value = *condition.flatten
58
+ @query << {"fields.#{field.to_s.camelize(:lower)}[#{search_predicate}]" => search_value}
59
+ end
48
60
  end
49
61
  end
50
62
  self
@@ -64,4 +76,4 @@ module ContentfulModel
64
76
  end
65
77
 
66
78
  end
67
- end
79
+ end
@@ -0,0 +1,14 @@
1
+ module ContentfulModel
2
+ class Client < Contentful::Client
3
+
4
+ PREVIEW_API_URL = "preview.contentful.com"
5
+
6
+ def initialize(configuration)
7
+ if ContentfulModel.use_preview_api
8
+ configuration[:api_url] = PREVIEW_API_URL
9
+ configuration[:access_token] = configuration[:preview_access_token]
10
+ end
11
+ super(configuration)
12
+ end
13
+ end
14
+ end
@@ -3,7 +3,6 @@ module ContentfulModel
3
3
  attr_accessor :parameters
4
4
  def initialize(referenced_class, parameters=nil)
5
5
  @parameters = parameters || {}
6
- @client = referenced_class.send(:client)
7
6
  @referenced_class = referenced_class
8
7
  end
9
8
 
@@ -17,7 +16,11 @@ module ContentfulModel
17
16
 
18
17
  def execute
19
18
  query = @parameters.merge!(default_parameters)
20
- return @client.entries(query)
19
+ return client.send(:entries,query)
20
+ end
21
+
22
+ def client
23
+ @client ||= @referenced_class.send(:client)
21
24
  end
22
25
 
23
26
  def reset
@@ -1,3 +1,3 @@
1
1
  module ContentfulModel
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -6,6 +6,9 @@ require "active_support/all"
6
6
 
7
7
  module ContentfulModel
8
8
  class << self
9
+ #accessor to set the preview API for use instead of the production one
10
+ attr_accessor :use_preview_api
11
+
9
12
  #access the configuration class as ContentfulModel.configuration
10
13
  attr_accessor :configuration
11
14
 
@@ -17,7 +20,10 @@ module ContentfulModel
17
20
  end
18
21
 
19
22
  class Configuration
20
- attr_accessor :access_token, :space, :entry_mapping
23
+ attr_accessor :access_token,
24
+ :preview_access_token,
25
+ :space,
26
+ :entry_mapping
21
27
 
22
28
  def initialize
23
29
  @entry_mapping ||= {}
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.5
4
+ version: 0.1.6
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-22 00:00:00.000000000 Z
11
+ date: 2015-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: contentful
@@ -85,6 +85,7 @@ files:
85
85
  - lib/contentful_model/associations/has_one.rb
86
86
  - lib/contentful_model/base.rb
87
87
  - lib/contentful_model/chainable_queries.rb
88
+ - lib/contentful_model/client.rb
88
89
  - lib/contentful_model/errors.rb
89
90
  - lib/contentful_model/queries.rb
90
91
  - lib/contentful_model/query.rb