ember_data_active_model_parser 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/README.md +4 -0
- data/lib/ember_data_active_model_parser/embed_associations.rb +5 -5
- data/lib/ember_data_active_model_parser/version.rb +1 -1
- data/spec/embed_associations_spec.rb +25 -10
- data/spec/her_parsing_spec.rb +85 -0
- data/spec/spec_helper.rb +15 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d493d89fdd39959771b48b3adffbdd2ce42e95
|
4
|
+
data.tar.gz: 344f3ebeea6af632994cee0da8a6318948422373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5101a5950f89c699c79f43639baecdfe9759817033c93cdc1ec8a0cbbffe0e6253c0af4251ecf7e44a0ef961c702910f1ca8270c9066b9bc82699a12478b03ec
|
7
|
+
data.tar.gz: b7ee138fc70ff60673febbbd50d81503ede74a9e7447feff1ca7e9c9c73a5f8bc726cc80a712d2c486db728913572e58c40a19aadf405bfbc7e9967f098a4f77
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
This is a middleware for [Her](http://her-rb.org/) which makes it possible to consume API endpoints used by ember-data's [ActiveModelAdapter](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html). You can read more about the data format in the ember-data's [docs](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html)
|
4
4
|
|
5
|
+
## Status
|
6
|
+
|
7
|
+
[![Build status](https://travis-ci.org/valo/ember_data_active_model_parser.svg)](https://travis-ci.org/valo/ember_data_active_model_parser)
|
8
|
+
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
@@ -28,7 +28,7 @@ module EmberDataActiveModelParser
|
|
28
28
|
def walk_hash(hash)
|
29
29
|
hash.keys.each do |key|
|
30
30
|
if association_ids_key(key, hash[key])
|
31
|
-
handle_association_ids(hash, hash
|
31
|
+
handle_association_ids(hash, hash[key], association_name(key))
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -42,14 +42,14 @@ module EmberDataActiveModelParser
|
|
42
42
|
}
|
43
43
|
}
|
44
44
|
when Fixnum
|
45
|
-
hash[association_name.to_sym] = {
|
46
|
-
|
47
|
-
|
45
|
+
hash[association_name.to_sym] = association_array(association_name).find { |association_item| association_item[:id] == association_ids }
|
46
|
+
when nil
|
47
|
+
hash[association_name.to_sym] = nil
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
def association_ids_key(key, value)
|
52
|
-
key.to_s =~ /(.+)_id(s?)$/ && association_array(association_name(key)) && (value.is_a?(Array) || value.is_a?(Fixnum))
|
52
|
+
key.to_s =~ /(.+)_id(s?)$/ && association_array(association_name(key)) && (value.is_a?(Array) || value.is_a?(Fixnum) || value.nil?)
|
53
53
|
end
|
54
54
|
|
55
55
|
def association_name(key)
|
@@ -26,7 +26,7 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
26
26
|
end
|
27
27
|
let(:expected_json) do
|
28
28
|
{
|
29
|
-
project: { id: 1, name: "Shop list", tasks: { tasks: tasks } },
|
29
|
+
project: { id: 1, name: "Shop list", tasks: { tasks: tasks }, task_ids: [1,2,3] },
|
30
30
|
tasks: tasks
|
31
31
|
}
|
32
32
|
end
|
@@ -79,22 +79,25 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
79
79
|
[{
|
80
80
|
id: 1,
|
81
81
|
name: "Milk",
|
82
|
-
project:
|
82
|
+
project: projects[0],
|
83
|
+
project_id: projects[0][:id]
|
83
84
|
},
|
84
85
|
{
|
85
86
|
id: 2,
|
86
87
|
name: "Bread",
|
87
|
-
project:
|
88
|
+
project: projects[0],
|
89
|
+
project_id: projects[0][:id]
|
88
90
|
},
|
89
91
|
{
|
90
92
|
id: 3,
|
91
93
|
name: "Butter",
|
92
|
-
project:
|
94
|
+
project: projects[1],
|
95
|
+
project_id: projects[1][:id]
|
93
96
|
}]
|
94
97
|
end
|
95
98
|
|
96
99
|
let(:expected_projects) do
|
97
|
-
[{ id: 1, name: "Shop list", tasks: { tasks: expected_tasks[0, 2] } }, { id: 2, name: "Other Shop list", tasks: { tasks: expected_tasks[2, 3] } }]
|
100
|
+
[{ id: 1, name: "Shop list", tasks: { tasks: expected_tasks[0, 2] }, task_ids: [1,2] }, { id: 2, name: "Other Shop list", tasks: { tasks: expected_tasks[2, 3] }, task_ids: [3] }]
|
98
101
|
end
|
99
102
|
|
100
103
|
let(:expected_json) do
|
@@ -110,7 +113,6 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
110
113
|
end
|
111
114
|
end
|
112
115
|
|
113
|
-
|
114
116
|
context "with an object with some associations that have associations" do
|
115
117
|
let(:tasks) do
|
116
118
|
[{
|
@@ -122,6 +124,11 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
122
124
|
id: 2,
|
123
125
|
name: "Bread",
|
124
126
|
author_id: 2
|
127
|
+
},
|
128
|
+
{
|
129
|
+
id: 3,
|
130
|
+
name: "Cheese",
|
131
|
+
author_id: nil
|
125
132
|
}]
|
126
133
|
end
|
127
134
|
|
@@ -129,12 +136,20 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
129
136
|
[{
|
130
137
|
id: 1,
|
131
138
|
name: "Milk",
|
132
|
-
author:
|
139
|
+
author: authors[0],
|
140
|
+
author_id: 1
|
133
141
|
},
|
134
142
|
{
|
135
143
|
id: 2,
|
136
144
|
name: "Bread",
|
137
|
-
author:
|
145
|
+
author: authors[1],
|
146
|
+
author_id: 2
|
147
|
+
},
|
148
|
+
{
|
149
|
+
id: 3,
|
150
|
+
name: "Cheese",
|
151
|
+
author: nil,
|
152
|
+
author_id: nil
|
138
153
|
}]
|
139
154
|
end
|
140
155
|
|
@@ -151,14 +166,14 @@ describe EmberDataActiveModelParser::EmbedAssociations do
|
|
151
166
|
|
152
167
|
let(:json) do
|
153
168
|
{
|
154
|
-
project: { id: 1, name: "Shop list", task_ids: [1,2] },
|
169
|
+
project: { id: 1, name: "Shop list", task_ids: [1, 2, 3] },
|
155
170
|
tasks: tasks,
|
156
171
|
authors: authors
|
157
172
|
}
|
158
173
|
end
|
159
174
|
let(:expected_json) do
|
160
175
|
{
|
161
|
-
project: { id: 1, name: "Shop list", tasks: { tasks: expected_tasks } },
|
176
|
+
project: { id: 1, name: "Shop list", tasks: { tasks: expected_tasks }, task_ids: [1, 2, 3] },
|
162
177
|
tasks: expected_tasks,
|
163
178
|
authors: authors
|
164
179
|
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'her'
|
3
|
+
require 'active_support/core_ext/object/json'
|
4
|
+
|
5
|
+
class Project
|
6
|
+
include Her::Model
|
7
|
+
|
8
|
+
parse_root_in_json true, format: :active_model_serializers
|
9
|
+
|
10
|
+
has_many :tasks
|
11
|
+
has_one :organization
|
12
|
+
end
|
13
|
+
|
14
|
+
class Task
|
15
|
+
include Her::Model
|
16
|
+
|
17
|
+
parse_root_in_json true, format: :active_model_serializers
|
18
|
+
end
|
19
|
+
|
20
|
+
class Organization
|
21
|
+
include Her::Model
|
22
|
+
|
23
|
+
parse_root_in_json true, format: :active_model_serializers
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
describe "Parsing" do
|
28
|
+
let(:tasks) do
|
29
|
+
[{
|
30
|
+
id: 1,
|
31
|
+
name: "Milk",
|
32
|
+
},
|
33
|
+
{
|
34
|
+
id: 2,
|
35
|
+
name: "Bread"
|
36
|
+
},
|
37
|
+
{
|
38
|
+
id: 3,
|
39
|
+
name: "Butter"
|
40
|
+
}]
|
41
|
+
end
|
42
|
+
|
43
|
+
let(:organizations) do
|
44
|
+
[{
|
45
|
+
id: 1,
|
46
|
+
name: "ACME Inc."
|
47
|
+
}]
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:project_response) do
|
51
|
+
{
|
52
|
+
project: { id: 1, name: "Shop list", task_ids: [1,2,3], organization_id: 1 },
|
53
|
+
tasks: tasks,
|
54
|
+
organizations: organizations
|
55
|
+
}.to_json
|
56
|
+
end
|
57
|
+
|
58
|
+
before do
|
59
|
+
stub_api_for(Project) do |stub|
|
60
|
+
stub.get("/projects/1") { |env| [200, {}, project_response] }
|
61
|
+
end
|
62
|
+
stub_api_for(Organization) { }
|
63
|
+
stub_api_for(Task) { }
|
64
|
+
end
|
65
|
+
|
66
|
+
describe :find do
|
67
|
+
let(:project) { Project.find(1) }
|
68
|
+
|
69
|
+
it "has an array of tasks" do
|
70
|
+
expect(project.tasks).to be_an(Array)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "has the correct number of tasks" do
|
74
|
+
expect(project.tasks.length).to eq(tasks.length)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "has an organization" do
|
78
|
+
expect(project.organization).to be_an(Organization)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "assigns the correct organization" do
|
82
|
+
expect(project.organization.id).to eq(organizations[0][:id])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# spec/spec_helper.rb
|
2
|
+
RSpec.configure do |config|
|
3
|
+
config.include(Module.new do
|
4
|
+
def stub_api_for(klass)
|
5
|
+
klass.use_api (api = Her::API.new)
|
6
|
+
|
7
|
+
# Here, you would customize this for your own API (URL, middleware, etc)
|
8
|
+
# like you have done in your application’s initializer
|
9
|
+
api.setup url: "http://api.example.com" do |c|
|
10
|
+
c.use EmberDataActiveModelParser::Middleware
|
11
|
+
c.adapter(:test) { |s| yield(s) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end)
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember_data_active_model_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valentin Mihov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: her
|
@@ -75,6 +75,7 @@ extensions: []
|
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
77
|
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|
@@ -84,6 +85,8 @@ files:
|
|
84
85
|
- lib/ember_data_active_model_parser/embed_associations.rb
|
85
86
|
- lib/ember_data_active_model_parser/version.rb
|
86
87
|
- spec/embed_associations_spec.rb
|
88
|
+
- spec/her_parsing_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
87
90
|
homepage: https://github.com/valo/ember_data_active_model_parser
|
88
91
|
licenses:
|
89
92
|
- MIT
|
@@ -110,3 +113,5 @@ specification_version: 4
|
|
110
113
|
summary: A parser for Her compatible with ember-data's active_model_serializers format
|
111
114
|
test_files:
|
112
115
|
- spec/embed_associations_spec.rb
|
116
|
+
- spec/her_parsing_spec.rb
|
117
|
+
- spec/spec_helper.rb
|