test_track_rails_client 4.0.0.alpha12 → 4.0.0.alpha13
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4a778c1664c68c4ddab8f3ca75a3a3b55c10650
|
4
|
+
data.tar.gz: 6fcf9637535419b34e4ce377a91804c0497623fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b35d4919150c7fd05b48d92b63779def2829e5d113c35fa2d53b5a66011931e691d3da037e74cf410133cffaf04bdc630b4d61bf6ab30f8ea94232a2786ab922
|
7
|
+
data.tar.gz: e853d6515ab4417ce3044135e96f4a206a0e19dd8d44b49ce3e7a24a210206d4af3768916a4b2ab634d6bd3966851ca5b8b7ce6e06a5545d48e433eba7d9c125
|
@@ -17,10 +17,7 @@ module FakeableHer
|
|
17
17
|
response_attrs = fake_save_response_attributes
|
18
18
|
assign_attributes(response_attrs) if response_attrs.present?
|
19
19
|
|
20
|
-
|
21
|
-
@previously_changed = self.changed_attributes.clone
|
22
|
-
self.changed_attributes.clear
|
23
|
-
end
|
20
|
+
changes_applied
|
24
21
|
end
|
25
22
|
end
|
26
23
|
true
|
@@ -47,19 +44,6 @@ module FakeableHer
|
|
47
44
|
raise "You must define `#fake_save_response_attributes` to provide default values for #{self.class.name} during development. For more details, refer to the Retail app README."
|
48
45
|
end
|
49
46
|
|
50
|
-
unless method_defined?(:clear_changes_information)
|
51
|
-
def clear_changes_information
|
52
|
-
if respond_to?(:reset_changes)
|
53
|
-
reset_changes
|
54
|
-
else
|
55
|
-
@previously_changed = ActiveSupport::HashWithIndifferentAccess.new
|
56
|
-
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
private :clear_changes_information
|
62
|
-
|
63
47
|
module ClassMethods
|
64
48
|
def faked?
|
65
49
|
!ENV["#{service_name.to_s.upcase}_ENABLED"] && (Rails.env.development? || Rails.env.test?)
|
@@ -120,7 +104,7 @@ module FakeableHer
|
|
120
104
|
if @parent.faked?
|
121
105
|
args = ids
|
122
106
|
args << @params if @params.present?
|
123
|
-
@parent.new(@parent.fake_instance_attributes(*args)).tap { |p| p.send(:
|
107
|
+
@parent.new(@parent.fake_instance_attributes(*args)).tap { |p| p.send(:changes_applied) }
|
124
108
|
else
|
125
109
|
super
|
126
110
|
end
|
@@ -133,16 +117,15 @@ module FakeableHer
|
|
133
117
|
|
134
118
|
if id_param && !id_param.is_a?(Array)
|
135
119
|
@params.delete(@parent.primary_key)
|
136
|
-
@parent.new(@parent.fake_instance_attributes(id_param, @params)).tap { |p| p.send(:
|
120
|
+
@parent.new(@parent.fake_instance_attributes(id_param, @params)).tap { |p| p.send(:changes_applied) }
|
137
121
|
else
|
138
122
|
fetch_collection.first
|
139
123
|
end
|
140
124
|
end
|
141
125
|
|
142
126
|
def fetch_collection
|
143
|
-
Her::Collection.new @parent.fake_collection_attributes(@params).map { |attrs| @parent.new(attrs).tap { |p| p.send(:
|
127
|
+
Her::Collection.new @parent.fake_collection_attributes(@params).map { |attrs| @parent.new(attrs).tap { |p| p.send(:changes_applied) } }
|
144
128
|
end
|
145
129
|
end
|
146
130
|
end
|
147
131
|
end
|
148
|
-
|
data/vendor/gems/her/her.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
|
2
3
|
$:.push File.expand_path("../lib", __FILE__)
|
3
4
|
require "her/version"
|
4
5
|
|
@@ -14,17 +15,15 @@ Gem::Specification.new do |s|
|
|
14
15
|
|
15
16
|
s.files = `git ls-files`.split("\n")
|
16
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
18
19
|
s.require_paths = ["lib"]
|
19
20
|
|
20
|
-
s.add_development_dependency "rake", "~> 10.0"
|
21
|
-
s.add_development_dependency "rspec", "~> 3.5"
|
22
21
|
s.add_development_dependency "json", "~> 1.8"
|
23
22
|
s.add_development_dependency "pry"
|
24
|
-
s.add_development_dependency
|
23
|
+
s.add_development_dependency "rake", "~> 10.0"
|
24
|
+
s.add_development_dependency "rspec", "~> 3.5"
|
25
25
|
|
26
|
-
s.add_runtime_dependency "activemodel", ">=
|
27
|
-
s.add_runtime_dependency "activesupport", ">= 3.0.0", "<= 6.0.0"
|
26
|
+
s.add_runtime_dependency "activemodel", ">= 4.2.1"
|
28
27
|
s.add_runtime_dependency "faraday", ">= 0.8", "< 1.0"
|
29
28
|
s.add_runtime_dependency "multi_json", "~> 1.7"
|
30
29
|
end
|
@@ -2,6 +2,7 @@ module Her
|
|
2
2
|
# This class is where all HTTP requests are made. Before using Her, you must configure it
|
3
3
|
# so it knows where to make those requests. In Rails, this is usually done in `config/initializers/her.rb`:
|
4
4
|
class API
|
5
|
+
|
5
6
|
# @private
|
6
7
|
attr_reader :connection, :options
|
7
8
|
|
@@ -9,7 +10,7 @@ module Her
|
|
9
10
|
FARADAY_OPTIONS = [:request, :proxy, :ssl, :builder, :url, :parallel_manager, :params, :headers, :builder_class, :timeout, :open_timeout].freeze
|
10
11
|
|
11
12
|
# Setup a default API connection. Accepted arguments and options are the same as {API#setup}.
|
12
|
-
def self.setup(opts={}, &block)
|
13
|
+
def self.setup(opts = {}, &block)
|
13
14
|
@default_api = new(opts, &block)
|
14
15
|
end
|
15
16
|
|
@@ -68,11 +69,11 @@ module Her
|
|
68
69
|
# connection.use MyCustomParser
|
69
70
|
# connection.use Faraday::Adapter::NetHttp
|
70
71
|
# end
|
71
|
-
def setup(opts={}, &blk)
|
72
|
+
def setup(opts = {}, &blk)
|
72
73
|
opts[:url] = opts.delete(:base_uri) if opts.include?(:base_uri) # Support legacy :base_uri option
|
73
74
|
@options = opts
|
74
75
|
|
75
|
-
faraday_options = @options.
|
76
|
+
faraday_options = @options.select { |key, _| FARADAY_OPTIONS.include?(key.to_sym) }
|
76
77
|
@connection = Faraday.new(faraday_options) do |connection|
|
77
78
|
yield connection if block_given?
|
78
79
|
end
|
@@ -84,35 +85,42 @@ module Her
|
|
84
85
|
# and a metadata Hash.
|
85
86
|
#
|
86
87
|
# @private
|
87
|
-
def request(
|
88
|
-
method =
|
89
|
-
path =
|
90
|
-
headers =
|
91
|
-
options =
|
92
|
-
|
93
|
-
|
88
|
+
def request(opts = {})
|
89
|
+
method = opts.delete(:_method)
|
90
|
+
path = opts.delete(:_path)
|
91
|
+
headers = opts.delete(:_headers)
|
92
|
+
options = opts.delete(:_options) || {}
|
93
|
+
opts.delete_if { |key, _| key.to_s =~ /^_/ } # Remove all internal parameters
|
94
|
+
if method == :options
|
95
|
+
# Faraday doesn't support the OPTIONS verb because of a name collision with an internal options method
|
96
|
+
# so we need to call run_request directly.
|
94
97
|
request.headers.merge!(headers) if headers
|
98
|
+
response = @connection.run_request method, path, opts, headers
|
99
|
+
else
|
100
|
+
response = @connection.send method do |request|
|
101
|
+
request.headers.merge!(headers) if headers
|
95
102
|
|
96
|
-
|
97
|
-
|
98
|
-
|
103
|
+
timeout = options.delete(:timeout)
|
104
|
+
raise "options not implemented: #{options.keys.join(', ')}" unless options.empty?
|
105
|
+
request.options[:timeout] = timeout if timeout
|
99
106
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
+
if method == :get
|
108
|
+
# For GET requests, treat additional parameters as querystring data
|
109
|
+
request.url path, opts
|
110
|
+
else
|
111
|
+
# For POST, PUT and DELETE requests, treat additional parameters as request body
|
112
|
+
request.url path
|
113
|
+
request.body = opts
|
114
|
+
end
|
107
115
|
end
|
108
116
|
end
|
109
|
-
|
110
117
|
{ :parsed_data => response.env[:body], :response => response }
|
111
118
|
end
|
112
119
|
|
113
120
|
private
|
121
|
+
|
114
122
|
# @private
|
115
|
-
def self.default_api(opts={})
|
123
|
+
def self.default_api(opts = {})
|
116
124
|
defined?(@default_api) ? @default_api : nil
|
117
125
|
end
|
118
126
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
module Her
|
2
2
|
module Errors
|
3
3
|
class PathError < StandardError
|
4
|
+
|
4
5
|
attr_reader :missing_parameter
|
5
6
|
|
6
|
-
def initialize(message, missing_parameter=nil)
|
7
|
+
def initialize(message, missing_parameter = nil)
|
7
8
|
super(message)
|
8
9
|
@missing_parameter = missing_parameter
|
9
10
|
end
|
@@ -36,5 +37,14 @@ module Her
|
|
36
37
|
|
37
38
|
class RecordInvalid < ResponseError
|
38
39
|
end
|
40
|
+
|
41
|
+
class ResourceInvalid < StandardError
|
42
|
+
attr_reader :resource
|
43
|
+
def initialize(resource)
|
44
|
+
@resource = resource
|
45
|
+
errors = @resource.response_errors.join(", ")
|
46
|
+
super("Remote validation failed: #{errors}")
|
47
|
+
end
|
48
|
+
end
|
39
49
|
end
|
40
50
|
end
|
@@ -1,13 +1,12 @@
|
|
1
1
|
module Her
|
2
2
|
module JsonApi
|
3
3
|
module Model
|
4
|
-
|
5
4
|
def self.included(klass)
|
6
5
|
klass.class_eval do
|
7
6
|
include Her::Model
|
8
7
|
|
9
8
|
[:parse_root_in_json, :include_root_in_json, :root_element, :primary_key].each do |method|
|
10
|
-
define_method method do |*
|
9
|
+
define_method method do |*_|
|
11
10
|
raise NoMethodError, "Her::JsonApi::Model does not support the #{method} configuration option"
|
12
11
|
end
|
13
12
|
end
|
@@ -15,24 +14,21 @@ module Her
|
|
15
14
|
method_for :update, :patch
|
16
15
|
|
17
16
|
@type = name.demodulize.tableize
|
18
|
-
|
17
|
+
|
19
18
|
def self.parse(data)
|
20
19
|
data.fetch(:attributes).merge(data.slice(:id))
|
21
20
|
end
|
22
21
|
|
23
|
-
def self.to_params(attributes, changes={})
|
24
|
-
request_data = { type: @type }.tap
|
25
|
-
attrs = attributes.dup.symbolize_keys.tap
|
22
|
+
def self.to_params(attributes, changes = {})
|
23
|
+
request_data = { type: @type }.tap do |request_body|
|
24
|
+
attrs = attributes.dup.symbolize_keys.tap do |filtered_attributes|
|
26
25
|
if her_api.options[:send_only_modified_attributes]
|
27
|
-
filtered_attributes
|
28
|
-
hash[attribute] = filtered_attributes[attribute]
|
29
|
-
hash
|
30
|
-
end
|
26
|
+
filtered_attributes.slice! *changes.keys.map(&:to_sym)
|
31
27
|
end
|
32
|
-
|
28
|
+
end
|
33
29
|
request_body[:id] = attrs.delete(:id) if attrs[:id]
|
34
30
|
request_body[:attributes] = attrs
|
35
|
-
|
31
|
+
end
|
36
32
|
{ data: request_data }
|
37
33
|
end
|
38
34
|
|
@@ -2,6 +2,7 @@ module Her
|
|
2
2
|
module Middleware
|
3
3
|
# This middleware treat the received first-level JSON structure as the resource data.
|
4
4
|
class FirstLevelParseJSON < ParseJSON
|
5
|
+
|
5
6
|
# Parse the response body
|
6
7
|
#
|
7
8
|
# @param [String] body The response body
|
@@ -25,11 +26,11 @@ module Her
|
|
25
26
|
# @private
|
26
27
|
def on_complete(env)
|
27
28
|
env[:body] = case env[:status]
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
when 204
|
30
|
+
parse('{}')
|
31
|
+
else
|
32
|
+
parse(env[:body])
|
33
|
+
end
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
@@ -3,6 +3,7 @@ module Her
|
|
3
3
|
# This middleware expects the resource/collection data to be contained in the `data`
|
4
4
|
# key of the JSON object
|
5
5
|
class JsonApiParser < ParseJSON
|
6
|
+
|
6
7
|
# Parse the response body
|
7
8
|
#
|
8
9
|
# @param [String] body The response body
|
@@ -25,11 +26,11 @@ module Her
|
|
25
26
|
# @private
|
26
27
|
def on_complete(env)
|
27
28
|
env[:body] = case env[:status]
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
when 204, 304
|
30
|
+
parse('{}')
|
31
|
+
else
|
32
|
+
parse(env[:body])
|
33
|
+
end
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Her
|
2
2
|
module Middleware
|
3
3
|
class ParseJSON < Faraday::Response::Middleware
|
4
|
+
|
4
5
|
# @private
|
5
6
|
def parse_json(body = nil)
|
6
7
|
body = '{}' if body.blank?
|
@@ -12,7 +13,7 @@ module Her
|
|
12
13
|
raise Her::Errors::ParseError, message
|
13
14
|
end
|
14
15
|
|
15
|
-
raise Her::Errors::ParseError, message unless json.is_a?(Hash)
|
16
|
+
raise Her::Errors::ParseError, message unless json.is_a?(Hash) || json.is_a?(Array)
|
16
17
|
|
17
18
|
json
|
18
19
|
end
|
@@ -3,6 +3,7 @@ module Her
|
|
3
3
|
# This middleware expects the resource/collection data to be contained in the `data`
|
4
4
|
# key of the JSON object
|
5
5
|
class SecondLevelParseJSON < ParseJSON
|
6
|
+
|
6
7
|
# Parse the response body
|
7
8
|
#
|
8
9
|
# @param [String] body The response body
|
@@ -25,11 +26,11 @@ module Her
|
|
25
26
|
# @private
|
26
27
|
def on_complete(env)
|
27
28
|
env[:body] = case env[:status]
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
when 204
|
30
|
+
parse('{}')
|
31
|
+
else
|
32
|
+
parse(env[:body])
|
33
|
+
end
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
@@ -2,6 +2,7 @@ module Her
|
|
2
2
|
module Model
|
3
3
|
module Associations
|
4
4
|
class Association
|
5
|
+
|
5
6
|
# @private
|
6
7
|
attr_accessor :params
|
7
8
|
|
@@ -26,14 +27,7 @@ module Her
|
|
26
27
|
return {} unless data[data_key]
|
27
28
|
|
28
29
|
klass = klass.her_nearby_class(association[:class_name])
|
29
|
-
|
30
|
-
{ association[:name] => data[data_key] }
|
31
|
-
else
|
32
|
-
resource = klass.new(klass.parse(data[data_key]))
|
33
|
-
resource.instance_variable_set(:@changed_attributes, {})
|
34
|
-
resource.run_callbacks :find
|
35
|
-
{ association[:name] => resource }
|
36
|
-
end
|
30
|
+
{ association[:name] => klass.instantiate_record(klass, data: data[data_key]) }
|
37
31
|
end
|
38
32
|
|
39
33
|
# @private
|
@@ -52,8 +46,9 @@ module Her
|
|
52
46
|
|
53
47
|
return @cached_result unless @params.any? || @cached_result.nil?
|
54
48
|
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
|
49
|
+
return @opts[:default].try(:dup) if @parent.new?
|
55
50
|
|
56
|
-
path = build_association_path
|
51
|
+
path = build_association_path -> { "#{@parent.request_path(@params)}#{@opts[:path]}" }
|
57
52
|
@klass.get(path, @params).tap do |result|
|
58
53
|
@cached_result = result unless @params.any?
|
59
54
|
end
|
@@ -61,11 +56,16 @@ module Her
|
|
61
56
|
|
62
57
|
# @private
|
63
58
|
def build_association_path(code)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
59
|
+
instance_exec(&code)
|
60
|
+
rescue Her::Errors::PathError
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
|
64
|
+
# @private
|
65
|
+
def reset
|
66
|
+
@params = {}
|
67
|
+
@cached_result = nil
|
68
|
+
@parent.attributes.delete(@name)
|
69
69
|
end
|
70
70
|
|
71
71
|
# Add query parameters to the HTTP request performed to fetch the data
|
@@ -80,7 +80,7 @@ module Her
|
|
80
80
|
# user.comments.where(:approved => 1) # Fetched via GET "/users/1/comments?approved=1
|
81
81
|
def where(params = {})
|
82
82
|
return self if params.blank? && @parent.attributes[@name].blank?
|
83
|
-
AssociationProxy.new
|
83
|
+
AssociationProxy.new clone.tap { |a| a.params = a.params.merge(params) }
|
84
84
|
end
|
85
85
|
alias all where
|
86
86
|
|
@@ -96,10 +96,32 @@ module Her
|
|
96
96
|
# user.comments.find(3) # Fetched via GET "/users/1/comments/3
|
97
97
|
def find(id)
|
98
98
|
return nil if id.blank?
|
99
|
-
path = build_association_path
|
99
|
+
path = build_association_path -> { "#{@parent.request_path(@params)}#{@opts[:path]}/#{id}" }
|
100
100
|
@klass.get_resource(path, @params)
|
101
101
|
end
|
102
102
|
|
103
|
+
# Refetches the association and puts the proxy back in its initial state,
|
104
|
+
# which is unloaded. Cached associations are cleared.
|
105
|
+
#
|
106
|
+
# @example
|
107
|
+
# class User
|
108
|
+
# include Her::Model
|
109
|
+
# has_many :comments
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# class Comment
|
113
|
+
# include Her::Model
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# user = User.find(1)
|
117
|
+
# user.comments = [#<Comment(comments/2) id=2 body="Hello!">]
|
118
|
+
# user.comments.first.id = "Oops"
|
119
|
+
# user.comments.reload # => [#<Comment(comments/2) id=2 body="Hello!">]
|
120
|
+
# # Fetched again via GET "/users/1/comments"
|
121
|
+
def reload
|
122
|
+
reset
|
123
|
+
fetch
|
124
|
+
end
|
103
125
|
end
|
104
126
|
end
|
105
127
|
end
|
@@ -15,7 +15,7 @@ module Her
|
|
15
15
|
end
|
16
16
|
|
17
17
|
install_proxy_methods :association,
|
18
|
-
|
18
|
+
:build, :create, :where, :find, :all, :assign_nested_attributes, :reload
|
19
19
|
|
20
20
|
# @private
|
21
21
|
def initialize(association)
|
@@ -28,7 +28,7 @@ module Her
|
|
28
28
|
|
29
29
|
# @private
|
30
30
|
def method_missing(name, *args, &block)
|
31
|
-
if
|
31
|
+
if name == :object_id # avoid redefining object_id
|
32
32
|
return association.fetch.object_id
|
33
33
|
end
|
34
34
|
|
@@ -38,7 +38,6 @@ module Her
|
|
38
38
|
# resend message to fetched object
|
39
39
|
__send__(name, *args, &block)
|
40
40
|
end
|
41
|
-
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
@@ -80,7 +80,7 @@ module Her
|
|
80
80
|
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
|
81
81
|
|
82
82
|
path_params = @parent.attributes.merge(@params.merge(@klass.primary_key => foreign_key_value))
|
83
|
-
path = build_association_path
|
83
|
+
path = build_association_path -> { @klass.build_request_path(@opts[:path], path_params) }
|
84
84
|
@klass.get_resource(path, @params).tap do |result|
|
85
85
|
@cached_result = result if @params.blank?
|
86
86
|
end
|
@@ -31,7 +31,7 @@ module Her
|
|
31
31
|
return {} unless data[data_key]
|
32
32
|
|
33
33
|
klass = klass.her_nearby_class(association[:class_name])
|
34
|
-
{ association[:name] =>
|
34
|
+
{ association[:name] => klass.instantiate_collection(klass, :data => data[data_key]) }
|
35
35
|
end
|
36
36
|
|
37
37
|
# Initialize a new object with a foreign key to the parent
|
@@ -85,14 +85,14 @@ module Her
|
|
85
85
|
def fetch
|
86
86
|
super.tap do |o|
|
87
87
|
inverse_of = @opts[:inverse_of] || @parent.singularized_resource_name
|
88
|
-
o.each { |entry| entry.
|
88
|
+
o.each { |entry| entry.attributes[inverse_of] = @parent }
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
92
|
# @private
|
93
93
|
def assign_nested_attributes(attributes)
|
94
94
|
data = attributes.is_a?(Hash) ? attributes.values : attributes
|
95
|
-
@parent.attributes[@name] =
|
95
|
+
@parent.attributes[@name] = @klass.instantiate_collection(@klass, :data => data)
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
@@ -32,18 +32,18 @@ module Her
|
|
32
32
|
# @private
|
33
33
|
def associations
|
34
34
|
@_her_associations ||= begin
|
35
|
-
superclass.respond_to?(:associations) ? superclass.associations.dup : Hash.new { |h,k| h[k] = [] }
|
35
|
+
superclass.respond_to?(:associations) ? superclass.associations.dup : Hash.new { |h, k| h[k] = [] }
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
# @private
|
40
40
|
def association_names
|
41
|
-
associations.inject([]) { |memo, (
|
41
|
+
associations.inject([]) { |memo, (_, details)| memo << details }.flatten.map { |a| a[:name] }
|
42
42
|
end
|
43
43
|
|
44
44
|
# @private
|
45
45
|
def association_keys
|
46
|
-
associations.inject([]) { |memo, (
|
46
|
+
associations.inject([]) { |memo, (_, details)| memo << details }.flatten.map { |a| a[:data_key] }
|
47
47
|
end
|
48
48
|
|
49
49
|
# Parse associations data after initializing a new object
|
@@ -81,7 +81,7 @@ module Her
|
|
81
81
|
# @user = User.find(1)
|
82
82
|
# @user.articles # => [#<Article(articles/2) id=2 title="Hello world.">]
|
83
83
|
# # Fetched via GET "/users/1/articles"
|
84
|
-
def has_many(name, opts={})
|
84
|
+
def has_many(name, opts = {})
|
85
85
|
Her::Model::Associations::HasManyAssociation.attach(self, name, opts)
|
86
86
|
end
|
87
87
|
|
@@ -106,7 +106,7 @@ module Her
|
|
106
106
|
# @user = User.find(1)
|
107
107
|
# @user.organization # => #<Organization(organizations/2) id=2 name="Foobar Inc.">
|
108
108
|
# # Fetched via GET "/users/1/organization"
|
109
|
-
def has_one(name, opts={})
|
109
|
+
def has_one(name, opts = {})
|
110
110
|
Her::Model::Associations::HasOneAssociation.attach(self, name, opts)
|
111
111
|
end
|
112
112
|
|
@@ -132,7 +132,7 @@ module Her
|
|
132
132
|
# @user = User.find(1) # => #<User(users/1) id=1 team_id=2 name="Tobias">
|
133
133
|
# @user.team # => #<Team(teams/2) id=2 name="Developers">
|
134
134
|
# # Fetched via GET "/teams/2"
|
135
|
-
def belongs_to(name, opts={})
|
135
|
+
def belongs_to(name, opts = {})
|
136
136
|
Her::Model::Associations::BelongsToAssociation.attach(self, name, opts)
|
137
137
|
end
|
138
138
|
end
|