test_track_rails_client 4.0.0.alpha12 → 4.0.0.alpha13
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 +4 -4
- data/lib/test_track_rails_client/version.rb +1 -1
- data/vendor/gems/fakeable_her/lib/fakeable_her/model.rb +4 -21
- data/vendor/gems/her/her.gemspec +5 -6
- data/vendor/gems/her/lib/her/api.rb +30 -22
- data/vendor/gems/her/lib/her/collection.rb +2 -1
- data/vendor/gems/her/lib/her/errors.rb +11 -1
- data/vendor/gems/her/lib/her/json_api/model.rb +8 -12
- data/vendor/gems/her/lib/her/middleware/accept_json.rb +1 -0
- data/vendor/gems/her/lib/her/middleware/first_level_parse_json.rb +6 -5
- data/vendor/gems/her/lib/her/middleware/json_api_parser.rb +6 -5
- data/vendor/gems/her/lib/her/middleware/parse_json.rb +2 -1
- data/vendor/gems/her/lib/her/middleware/second_level_parse_json.rb +6 -5
- data/vendor/gems/her/lib/her/middleware.rb +1 -1
- data/vendor/gems/her/lib/her/model/associations/association.rb +38 -16
- data/vendor/gems/her/lib/her/model/associations/association_proxy.rb +2 -3
- data/vendor/gems/her/lib/her/model/associations/belongs_to_association.rb +1 -1
- data/vendor/gems/her/lib/her/model/associations/has_many_association.rb +3 -3
- data/vendor/gems/her/lib/her/model/associations.rb +6 -6
- data/vendor/gems/her/lib/her/model/attributes.rb +121 -92
- data/vendor/gems/her/lib/her/model/base.rb +2 -2
- data/vendor/gems/her/lib/her/model/http.rb +4 -4
- data/vendor/gems/her/lib/her/model/introspection.rb +6 -4
- data/vendor/gems/her/lib/her/model/nested_attributes.rb +1 -1
- data/vendor/gems/her/lib/her/model/orm.rb +101 -29
- data/vendor/gems/her/lib/her/model/parse.rb +35 -28
- data/vendor/gems/her/lib/her/model/paths.rb +3 -4
- data/vendor/gems/her/lib/her/model/relation.rb +51 -24
- data/vendor/gems/her/lib/her/version.rb +1 -1
- metadata +2 -2
@@ -10,7 +10,7 @@ module Her
|
|
10
10
|
# @user.to_params
|
11
11
|
# # => { :id => 1, :name => 'John Smith' }
|
12
12
|
def to_params
|
13
|
-
self.class.to_params(
|
13
|
+
self.class.to_params(attributes, changes)
|
14
14
|
end
|
15
15
|
|
16
16
|
module ClassMethods
|
@@ -31,14 +31,21 @@ module Her
|
|
31
31
|
end
|
32
32
|
|
33
33
|
# @private
|
34
|
-
def to_params(attributes, changes={})
|
35
|
-
filtered_attributes = attributes.
|
34
|
+
def to_params(attributes, changes = {})
|
35
|
+
filtered_attributes = attributes.each_with_object({}) do |(key, value), memo|
|
36
|
+
case value
|
37
|
+
when Her::Model
|
38
|
+
when ActiveModel::Serialization
|
39
|
+
value = value.serializable_hash.symbolize_keys
|
40
|
+
end
|
41
|
+
|
42
|
+
memo[key.to_sym] = value
|
43
|
+
end
|
44
|
+
|
36
45
|
filtered_attributes.merge!(embeded_params(attributes))
|
46
|
+
|
37
47
|
if her_api.options[:send_only_modified_attributes]
|
38
|
-
filtered_attributes
|
39
|
-
hash[attribute] = filtered_attributes[attribute]
|
40
|
-
hash
|
41
|
-
end
|
48
|
+
filtered_attributes.slice! *changes.keys.map(&:to_sym)
|
42
49
|
end
|
43
50
|
|
44
51
|
if include_root_in_json?
|
@@ -52,20 +59,16 @@ module Her
|
|
52
59
|
end
|
53
60
|
end
|
54
61
|
|
55
|
-
|
56
62
|
# @private
|
57
|
-
# TODO: Handle has_one
|
58
63
|
def embeded_params(attributes)
|
59
|
-
associations
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
hash
|
64
|
+
associations.values.flatten.each_with_object({}) do |definition, hash|
|
65
|
+
value = case association = attributes[definition[:name]]
|
66
|
+
when Her::Collection, Array
|
67
|
+
association.map { |a| a.to_params }.reject(&:empty?)
|
68
|
+
when Her::Model
|
69
|
+
association.to_params
|
70
|
+
end
|
71
|
+
hash[definition[:data_key]] = value if value.present?
|
69
72
|
end
|
70
73
|
end
|
71
74
|
|
@@ -127,11 +130,11 @@ module Her
|
|
127
130
|
# user.name # => "Tobias"
|
128
131
|
def root_element(value = nil)
|
129
132
|
if value.nil?
|
130
|
-
if json_api_format?
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
133
|
+
@_her_root_element ||= if json_api_format?
|
134
|
+
name.split("::").last.pluralize.underscore.to_sym
|
135
|
+
else
|
136
|
+
name.split("::").last.underscore.to_sym
|
137
|
+
end
|
135
138
|
else
|
136
139
|
@_her_root_element = value.to_sym
|
137
140
|
end
|
@@ -139,7 +142,8 @@ module Her
|
|
139
142
|
|
140
143
|
# @private
|
141
144
|
def root_element_included?(data)
|
142
|
-
data
|
145
|
+
element = data[parsed_root_element]
|
146
|
+
element.is_a?(Hash) || element.is_a?(Array)
|
143
147
|
end
|
144
148
|
|
145
149
|
# @private
|
@@ -198,17 +202,20 @@ module Her
|
|
198
202
|
|
199
203
|
# @private
|
200
204
|
def request_new_object_on_build?
|
201
|
-
@_her_request_new_object_on_build
|
205
|
+
return @_her_request_new_object_on_build unless @_her_request_new_object_on_build.nil?
|
206
|
+
superclass.respond_to?(:request_new_object_on_build?) && superclass.request_new_object_on_build?
|
202
207
|
end
|
203
208
|
|
204
209
|
# @private
|
205
210
|
def include_root_in_json?
|
206
|
-
@_her_include_root_in_json
|
211
|
+
return @_her_include_root_in_json unless @_her_include_root_in_json.nil?
|
212
|
+
superclass.respond_to?(:include_root_in_json?) && superclass.include_root_in_json?
|
207
213
|
end
|
208
214
|
|
209
215
|
# @private
|
210
216
|
def parse_root_in_json?
|
211
|
-
@_her_parse_root_in_json
|
217
|
+
return @_her_parse_root_in_json unless @_her_parse_root_in_json.nil?
|
218
|
+
superclass.respond_to?(:parse_root_in_json?) && superclass.parse_root_in_json?
|
212
219
|
end
|
213
220
|
end
|
214
221
|
end
|
@@ -19,7 +19,6 @@ module Her
|
|
19
19
|
end
|
20
20
|
|
21
21
|
module ClassMethods
|
22
|
-
|
23
22
|
# Define the primary key field that will be used to find and save records
|
24
23
|
#
|
25
24
|
# @example
|
@@ -88,13 +87,13 @@ module Her
|
|
88
87
|
# Return a custom path based on the collection path and variable parameters
|
89
88
|
#
|
90
89
|
# @private
|
91
|
-
def build_request_path(path=nil, parameters={})
|
90
|
+
def build_request_path(path = nil, parameters = {})
|
92
91
|
parameters = parameters.try(:with_indifferent_access)
|
93
92
|
|
94
93
|
unless path.is_a?(String)
|
95
94
|
parameters = path.try(:with_indifferent_access) || parameters
|
96
95
|
path =
|
97
|
-
if parameters.include?(primary_key) && parameters[primary_key] && !parameters[primary_key].
|
96
|
+
if parameters.include?(primary_key) && parameters[primary_key] && !parameters[primary_key].is_a?(Array)
|
98
97
|
resource_path.dup
|
99
98
|
else
|
100
99
|
collection_path.dup
|
@@ -117,7 +116,7 @@ module Her
|
|
117
116
|
end
|
118
117
|
|
119
118
|
# @private
|
120
|
-
def build_request_path_from_string_or_symbol(path, params={})
|
119
|
+
def build_request_path_from_string_or_symbol(path, params = {})
|
121
120
|
path.is_a?(Symbol) ? "#{build_request_path(params)}/#{path}" : path
|
122
121
|
end
|
123
122
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module Her
|
2
2
|
module Model
|
3
3
|
class Relation
|
4
|
+
|
4
5
|
# @private
|
5
6
|
attr_accessor :params, :request_headers, :request_options
|
7
|
+
attr_writer :parent
|
6
8
|
|
7
9
|
# @private
|
8
10
|
def initialize(parent)
|
@@ -49,7 +51,7 @@ module Her
|
|
49
51
|
# # Fetched via GET "/users?approved=1"
|
50
52
|
def where(params = {})
|
51
53
|
return self if params.blank? && !@_fetch.nil?
|
52
|
-
|
54
|
+
clone.tap do |r|
|
53
55
|
r.params = r.params.merge(params)
|
54
56
|
r.clear_fetch_cache!
|
55
57
|
end
|
@@ -75,7 +77,7 @@ module Her
|
|
75
77
|
|
76
78
|
# @private
|
77
79
|
def kind_of?(thing)
|
78
|
-
fetch.
|
80
|
+
fetch.is_a?(thing)
|
79
81
|
end
|
80
82
|
|
81
83
|
# Fetch a collection of resources
|
@@ -83,32 +85,14 @@ module Her
|
|
83
85
|
# @private
|
84
86
|
def fetch
|
85
87
|
@_fetch ||= begin
|
86
|
-
path = @parent.build_request_path(@params)
|
88
|
+
path = @parent.build_request_path(@parent.collection_path, @params)
|
87
89
|
method = @parent.method_for(:find)
|
88
|
-
@parent.request(@params.merge(:_method => method, :_path => path, :_headers => request_headers, :_options => request_options)) do |parsed_data,
|
90
|
+
@parent.request(@params.merge(:_method => method, :_path => path, :_headers => request_headers, :_options => request_options)) do |parsed_data, _|
|
89
91
|
@parent.new_collection(parsed_data)
|
90
92
|
end
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
# Fetch specific resource by some of its attributes
|
95
|
-
#
|
96
|
-
# @example
|
97
|
-
# @user = User.find_by(id: 1)
|
98
|
-
# # Fetched via GET "/users/1"
|
99
|
-
#
|
100
|
-
# @example
|
101
|
-
# @users = User.find_by(id: 1, age: 25)
|
102
|
-
# # Fetched via GET "/users/1?age=25"
|
103
|
-
#
|
104
|
-
# @example
|
105
|
-
# @users = User.find_by(age: 25)
|
106
|
-
# # Fetched via GET "/users?age=25"
|
107
|
-
# # locally limit to single result
|
108
|
-
def find_by(params = {})
|
109
|
-
where(params).first
|
110
|
-
end
|
111
|
-
|
112
96
|
# Fetch first result
|
113
97
|
#
|
114
98
|
# @example
|
@@ -181,7 +165,6 @@ module Her
|
|
181
165
|
@parent.request(request_params) do |parsed_data, response|
|
182
166
|
if response.success?
|
183
167
|
resource = @parent.new_from_parsed_data(parsed_data)
|
184
|
-
resource.instance_variable_set(:@changed_attributes, {})
|
185
168
|
resource.run_callbacks :find
|
186
169
|
else
|
187
170
|
raise Her::Errors::ResponseError.for(response.status).new("Request against #{@parent.name} returned a #{response.status}")
|
@@ -191,7 +174,51 @@ module Her
|
|
191
174
|
resource
|
192
175
|
end
|
193
176
|
|
194
|
-
ids.length > 1 || ids.first.
|
177
|
+
ids.length > 1 || ids.first.is_a?(Array) ? results : results.first
|
178
|
+
end
|
179
|
+
|
180
|
+
# Fetch first resource with the given attributes.
|
181
|
+
#
|
182
|
+
# If no resource is found, returns <tt>nil</tt>.
|
183
|
+
#
|
184
|
+
# @example
|
185
|
+
# @user = User.find_by(name: "Tobias", age: 42)
|
186
|
+
# # Called via GET "/users?name=Tobias&age=42"
|
187
|
+
def find_by(params)
|
188
|
+
where(params).first
|
189
|
+
end
|
190
|
+
|
191
|
+
# Fetch first resource with the given attributes, or create a resource
|
192
|
+
# with the attributes if one is not found.
|
193
|
+
#
|
194
|
+
# @example
|
195
|
+
# @user = User.find_or_create_by(email: "remi@example.com")
|
196
|
+
#
|
197
|
+
# # Returns the first item in the collection if present:
|
198
|
+
# # Called via GET "/users?email=remi@example.com"
|
199
|
+
#
|
200
|
+
# # If collection is empty:
|
201
|
+
# # POST /users with `email=remi@example.com`
|
202
|
+
# @user.email # => "remi@example.com"
|
203
|
+
# @user.new? # => false
|
204
|
+
def find_or_create_by(attributes)
|
205
|
+
find_by(attributes) || create(attributes)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Fetch first resource with the given attributes, or initialize a resource
|
209
|
+
# with the attributes if one is not found.
|
210
|
+
#
|
211
|
+
# @example
|
212
|
+
# @user = User.find_or_initialize_by(email: "remi@example.com")
|
213
|
+
#
|
214
|
+
# # Returns the first item in the collection if present:
|
215
|
+
# # Called via GET "/users?email=remi@example.com"
|
216
|
+
#
|
217
|
+
# # If collection is empty:
|
218
|
+
# @user.email # => "remi@example.com"
|
219
|
+
# @user.new? # => true
|
220
|
+
def find_or_initialize_by(attributes)
|
221
|
+
find_by(attributes) || build(attributes)
|
195
222
|
end
|
196
223
|
|
197
224
|
# Create a resource and return it
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_track_rails_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0.0.
|
4
|
+
version: 4.0.0.alpha13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan O'Neill
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2018-10-
|
16
|
+
date: 2018-10-30 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: airbrake
|