xing-backend 0.0.16 → 0.0.17

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: c5e09d2b2bea8388051e9e89f57f8288e5781434
4
- data.tar.gz: 44cc296801fd1915cdf22f3a747569f24b29ff97
3
+ metadata.gz: 13ae26abbc1ee9f7bd2d9afae55849632ec9ffa1
4
+ data.tar.gz: 0df9f7c564139946b873e56001df974a965882e1
5
5
  SHA512:
6
- metadata.gz: 856cd6bfe88478f7cdf77396e0062d4ab0d78f0b526ec0394f91e5c27611113477eba27e4cbc99b4e2f7489a7553b68dfbc22fd4624bb28695da3387d8356c2d
7
- data.tar.gz: 82d3a6b115af69c879839592f40f3d7941b47553fb29f3cfe482fe5d531c421fc9fffe27b71a1339c756f457fe640107701b4ecd85add6f9e6250c807f881b0f
6
+ metadata.gz: afc9e88098caf477eb19c55272412bad43f175f17bd28cd37c8d665662463db8d88a040076899dc45cc129159e34d7fd37804a8dc9e307e158dd526ea2a4422a
7
+ data.tar.gz: 018e22b3a6705f22c97949ef6bdfeb1c1812fe3b5384061fb21a66c0cd819a72149e4c5eaeeea465366e72d93e5d039acf9d8723ecc8271735636482aa1928eb
@@ -8,8 +8,7 @@ module Xing
8
8
  :ResourcesSerializer => Xing::Serializers::RootResources,
9
9
  :JsonTreeLister => Xing::Services::JsonTreeLister,
10
10
  :ActiveModelErrorConverter => Xing::Services::ErrorConverter,
11
- :RemoteSnapshotFetcher => Xing::Services::SnapshotFetcher,
12
- :ListDifferenceBuilder => Xing::Builders::OrderedListDifferenceBuilder
11
+ :RemoteSnapshotFetcher => Xing::Services::SnapshotFetcher
13
12
  }
14
13
  end
15
14
 
data/lib/xing/builders.rb CHANGED
@@ -3,5 +3,5 @@ module Xing
3
3
  end
4
4
  end
5
5
 
6
- require 'xing/builders/list_difference_builder'
7
- require 'xing/builders/ordered_list_difference_builder'
6
+ require 'xing/builders/list_builder'
7
+ require 'xing/builders/ordered_list_builder'
@@ -0,0 +1,42 @@
1
+ require 'xing/services/locator'
2
+
3
+ module Xing
4
+ module Builders
5
+ class ListBuilder
6
+ include Services::Locator
7
+
8
+ # list_data is is an array of JSON objects passed in by the mapper (the new list of records)
9
+ def initialize(list_data, mapper_class)
10
+ @list_data = list_data
11
+ @mapper_class = mapper_class
12
+
13
+ @errors = Hash.new { |hash, key| hash[key] = {} }
14
+ end
15
+
16
+ attr_reader :errors
17
+
18
+ def locator_for(data)
19
+ route_to(data[:links][:self])[:id].to_i unless (data[:links] || {})[:self].blank?
20
+ end
21
+
22
+ def build
23
+ @new_list = []
24
+ @list_data.each_with_index do |data, index|
25
+
26
+ mapper = @mapper_class.new(data, locator_for(data))
27
+
28
+ mapper.perform_mapping
29
+ set_position(mapper.record, index)
30
+
31
+ @new_list << mapper.record
32
+ @errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
33
+ end
34
+ @new_list
35
+ end
36
+
37
+ def set_position(record, index)
38
+ # position is not set in list builder
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ module Xing
2
+ module Builders
3
+ class OrderedListBuilder < ListBuilder
4
+ def set_position(record, index)
5
+ record.position = index if record.has_attribute?(:position)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xing::Builders::ListBuilder do
4
+
5
+ let :builder do
6
+ Xing::Builders::ListBuilder.new(list_data, mapper_class)
7
+ end
8
+
9
+ let :mapper_class do
10
+ double("ItemMapper")
11
+ end
12
+
13
+ let :mapper_instance do
14
+ double("mapper")
15
+ end
16
+
17
+ let :new_ar_object do
18
+ double("New Relation AR Object")
19
+ end
20
+
21
+ let :updated_ar_object do
22
+ double("Updated AR Object")
23
+ end
24
+
25
+ let :list_data do
26
+ [
27
+ {
28
+ links: {
29
+ self: "/somethings/1"
30
+ },
31
+ data: {
32
+ stuff: "some updated stuff"
33
+ }
34
+ },
35
+ {
36
+ links: {
37
+ self: ""
38
+ },
39
+ data: {
40
+ stuff: "some new stuff"
41
+ }
42
+ }
43
+ ]
44
+ end
45
+
46
+ it "initialize" do
47
+ expect(builder.instance_variable_get('@list_data')).to eq(list_data)
48
+ expect(builder.instance_variable_get('@mapper_class')).to eq(mapper_class)
49
+ expect(builder.instance_variable_get('@errors')).to eq({})
50
+ end
51
+
52
+ describe "#build" do
53
+ before :each do
54
+ allow(builder).to receive(:locator_for).and_return(1)
55
+ allow(mapper_class).to receive(:new).with(list_data[0], 1).and_return(mapper_instance)
56
+ allow(mapper_class).to receive(:new).with(list_data[1], 1).and_return(mapper_instance)
57
+ allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
58
+ allow(mapper_instance).to receive(:perform_mapping)
59
+ allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
60
+ allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
61
+ end
62
+
63
+ context "successful" do
64
+ before :each do
65
+ allow(mapper_instance).to receive(:errors).and_return({})
66
+ end
67
+
68
+ it "should return array of AR records" do
69
+ expect(builder.build).to match_array([new_ar_object, updated_ar_object])
70
+ end
71
+ end
72
+
73
+ context "with errors" do
74
+ before :each do
75
+ allow(mapper_instance).to receive(:errors).and_return({data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {})
76
+ builder.build
77
+ end
78
+
79
+ it "should return errors" do
80
+ expect(builder.errors).to eq({0=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xing::Builders::OrderedListBuilder do
4
+
5
+ let :builder do
6
+ Xing::Builders::OrderedListBuilder.new(list_data, mapper_class)
7
+ end
8
+
9
+ let :mapper_class do
10
+ double("ItemMapper")
11
+ end
12
+
13
+ let :mapper_instance do
14
+ double("mapper")
15
+ end
16
+
17
+ let :new_ar_object do
18
+ double("New Relation AR Object")
19
+ end
20
+
21
+ let :updated_ar_object do
22
+ double("Updated AR Object")
23
+ end
24
+
25
+ let :list_data do
26
+ [
27
+ {
28
+ links: {
29
+ self: "/somethings/1"
30
+ },
31
+ data: {
32
+ stuff: "some updated stuff"
33
+ }
34
+ },
35
+ {
36
+ links: {
37
+ self: ""
38
+ },
39
+ data: {
40
+ stuff: "some new stuff"
41
+ }
42
+ }
43
+ ]
44
+ end
45
+
46
+ it "initialize" do
47
+ expect(builder.instance_variable_get('@list_data')).to eq(list_data)
48
+ expect(builder.instance_variable_get('@mapper_class')).to eq(mapper_class)
49
+ expect(builder.instance_variable_get('@errors')).to eq({})
50
+ end
51
+
52
+ describe "#set_position" do
53
+
54
+ it "should update the position if the record has a position attribute" do
55
+ allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(true)
56
+ expect(new_ar_object).to receive(:position=).with(1)
57
+
58
+ builder.set_position(new_ar_object, 1)
59
+ end
60
+
61
+ it "should not blow up if the record does not have a position attribute" do
62
+ allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
63
+ expect(new_ar_object).to_not receive(:position=)
64
+
65
+ builder.set_position(new_ar_object, 1)
66
+ end
67
+ end
68
+
69
+ describe "#build" do
70
+ before :each do
71
+ allow(builder).to receive(:locator_for).and_return(1)
72
+ allow(mapper_class).to receive(:new).with(list_data[0], 1).and_return(mapper_instance)
73
+ allow(mapper_class).to receive(:new).with(list_data[1], 1).and_return(mapper_instance)
74
+ allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
75
+ allow(mapper_instance).to receive(:perform_mapping)
76
+ allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
77
+ allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
78
+ end
79
+
80
+ context "successful" do
81
+ before :each do
82
+ allow(mapper_instance).to receive(:errors).and_return({})
83
+ end
84
+
85
+ it "should return array of AR records" do
86
+ expect(builder.build).to match_array([new_ar_object, updated_ar_object])
87
+ end
88
+ end
89
+
90
+ context "with errors" do
91
+ before :each do
92
+ allow(mapper_instance).to receive(:errors).and_return({data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {})
93
+ builder.build
94
+ end
95
+
96
+ it "should return errors" do
97
+ expect(builder.errors).to eq({0=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
98
+ end
99
+ end
100
+ end
101
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xing-backend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Dorn
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-06-29 00:00:00.000000000 Z
14
+ date: 2015-07-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -126,8 +126,8 @@ files:
126
126
  - lib/deprecated_classes.rb
127
127
  - lib/xing-backend.rb
128
128
  - lib/xing/builders.rb
129
- - lib/xing/builders/list_difference_builder.rb
130
- - lib/xing/builders/ordered_list_difference_builder.rb
129
+ - lib/xing/builders/list_builder.rb
130
+ - lib/xing/builders/ordered_list_builder.rb
131
131
  - lib/xing/controllers/base.rb
132
132
  - lib/xing/controllers/root_resources_controller.rb
133
133
  - lib/xing/engine.rb
@@ -148,11 +148,10 @@ files:
148
148
  - spec/deprecated_classes/base_serializer_spec.rb
149
149
  - spec/deprecated_classes/hypermedia_json_mapper_spec.rb
150
150
  - spec/deprecated_classes/json_tree_lister_spec.rb
151
- - spec/deprecated_classes/list_difference_builder_spec.rb
152
151
  - spec/deprecated_classes/remote_snapshot_fetcher_spec.rb
153
152
  - spec/deprecated_classes/resources_serializer_spec.rb
154
- - spec/xing/builders/list_difference_builder_spec.rb
155
- - spec/xing/builders/ordered_list_difference_builder_spec.rb
153
+ - spec/xing/builders/list_builder_spec.rb
154
+ - spec/xing/builders/ordered_list_builder_spec.rb
156
155
  - spec/xing/controllers/base_spec.rb
157
156
  - spec/xing/controllers/root_resources_controller_spec.rb
158
157
  - spec/xing/mappers/base_spec.rb
@@ -208,7 +207,7 @@ rdoc_options:
208
207
  - "--main"
209
208
  - doc/README
210
209
  - "--title"
211
- - xing-backend-0.0.16 Documentation
210
+ - xing-backend-0.0.17 Documentation
212
211
  require_paths:
213
212
  - lib/
214
213
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -1,61 +0,0 @@
1
- require 'xing/services/locator'
2
-
3
- module Xing
4
- module Builders
5
- class ListDifferenceBuilder
6
- include Services::Locator
7
-
8
- # list_data is is an array of JSON objects passed in by the mapper (the new list of records)
9
- # collection is the ActiveRecord collection of existing records (i.e. person.pets, rainbow.colors, book.chapters)
10
- def initialize(list_data, collection, mapper_class)
11
- @list_data = list_data
12
- @collection = collection
13
- @mapper_class = mapper_class
14
-
15
- @errors = Hash.new { |hash, key| hash[key] = {} }
16
- end
17
-
18
- attr_reader :errors
19
-
20
- def build
21
- sort_json_items
22
- map_items
23
-
24
- { save: @new_list, delete: @delete_ids }
25
- end
26
-
27
- def sort_json_items
28
- @existing_ids = @collection.map{|item| item.send(@mapper_class.locator_attribute_name)}
29
-
30
- @list_data = @list_data.map do |data|
31
- { :locator => set_locator(data), :incoming => data}
32
- end
33
-
34
- @delete_ids = @existing_ids - @list_data.map { |item| item[:locator] }
35
- end
36
-
37
- def set_locator(data)
38
- locator_for(data) unless (data[:links] || {})[:self].blank?
39
- end
40
-
41
- def locator_for(data)
42
- route_to(data[:links][:self])[:id].to_i
43
- end
44
-
45
- def map_items
46
- @new_list = []
47
- @list_data.each_with_index do |item, index|
48
-
49
- mapper = @mapper_class.new(item[:incoming], item[:locator])
50
-
51
- # Sets association, attributes
52
- @collection << mapper.record
53
- mapper.perform_mapping
54
-
55
- @new_list << mapper
56
- @errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,25 +0,0 @@
1
- module Xing
2
- module Builders
3
- class OrderedListDifferenceBuilder < ListDifferenceBuilder
4
- def map_items
5
- @new_list = []
6
- @list_data.each_with_index do |item, index|
7
-
8
- mapper = @mapper_class.new(item[:incoming], item[:locator])
9
-
10
- # Sets association, attributes and position
11
- @collection << mapper.record
12
- mapper.perform_mapping
13
- set_position(mapper.record, index)
14
-
15
- @new_list << mapper
16
- @errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
17
- end
18
- end
19
-
20
- def set_position(record, index)
21
- record.position = index if record.has_attribute?(:position)
22
- end
23
- end
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- require 'deprecated_classes'
2
-
3
- describe ListDifferenceBuilder, :type => :deprecation do
4
- let :list_data do
5
- double('list_data')
6
- end
7
-
8
- let :collection do
9
- double('many.things')
10
- end
11
-
12
- let :mapper do
13
- double('ThingMapper')
14
- end
15
-
16
- it "should be the correct class" do
17
- expect(ListDifferenceBuilder.new(list_data, collection, mapper)).to be_a(Xing::Builders::OrderedListDifferenceBuilder)
18
- end
19
- end
@@ -1,193 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Xing::Builders::ListDifferenceBuilder do
4
-
5
- let :builder do
6
- Xing::Builders::ListDifferenceBuilder.new(list_data, collection, mapper_class)
7
- end
8
-
9
- let :mapper_class do
10
- double("ItemMapper")
11
- end
12
-
13
- let :mapper_instance do
14
- double("mapper")
15
- end
16
-
17
- let :collection do
18
- [double("Relation AR Object id: 1"), double("Relation AR Object id: 2"), double("Relation AR Object id: 3")]
19
- end
20
-
21
- let :new_ar_object do
22
- double("New Relation AR Object")
23
- end
24
-
25
- let :updated_ar_object do
26
- double("Updated AR Object")
27
- end
28
-
29
- let :existing_ids do
30
- [1,2,3]
31
- end
32
-
33
- let :list_data do
34
- [
35
- {
36
- links: {
37
- self: "/somethings/1"
38
- },
39
- data: {
40
- stuff: "some updated stuff"
41
- }
42
- },
43
- {
44
- links: {
45
- self: ""
46
- },
47
- data: {
48
- stuff: "some new stuff"
49
- }
50
- }
51
- ]
52
- end
53
-
54
- let :new_list_data do
55
- [
56
- {
57
- locator: 1,
58
- incoming: {
59
- links: {
60
- self: "/somethings/1"
61
- },
62
- data: {
63
- stuff: "some updated stuff"
64
- }
65
- }
66
- },
67
- {
68
- locator: nil,
69
- incoming: {
70
- links: {
71
- self: ""
72
- },
73
- data: {
74
- stuff: "some new stuff"
75
- }
76
- }
77
- }
78
- ]
79
- end
80
-
81
- it "should initialize and assign list_data, collection, mapper class and errors" do
82
- expect(builder.instance_variable_get('@list_data')).to eq(list_data)
83
- expect(builder.instance_variable_get('@collection')).to eq(collection)
84
- expect(builder.instance_variable_get('@mapper_class')).to eq(mapper_class)
85
- expect(builder.instance_variable_get('@errors')).to eq({})
86
- end
87
-
88
- describe "#sort_json_items" do
89
- before :each do
90
- allow(collection).to receive(:map).and_return(existing_ids)
91
- allow(builder).to receive(:locator_for).and_return(1)
92
- builder.sort_json_items
93
- end
94
-
95
- it "should create a list of existing ids" do
96
- expect(builder.instance_variable_get('@existing_ids')).to eq(existing_ids)
97
- end
98
-
99
- it "should insert index and locator into the item data" do
100
- expect(builder.instance_variable_get('@list_data')).to eq(new_list_data)
101
- end
102
-
103
- it "should find the ids to delete" do
104
- expect(builder.instance_variable_get('@delete_ids')).to eq([2,3])
105
- end
106
- end
107
-
108
- describe "#set_locator" do
109
-
110
- it "should be nil if links is empty" do
111
- data = { links: {}, data: { text:"bs" } }
112
- expect(builder.set_locator(data)).to be_nil
113
- end
114
-
115
- it "should be nil if links/self is empty" do
116
- data = { links: {self: ""}}
117
- expect(builder.set_locator(data)).to be_nil
118
- end
119
-
120
- it "should set locator if links/self is present" do
121
- data = { links: {self: "some_link/1/something"}}
122
- allow(builder).to receive(:locator_for).and_return(1)
123
-
124
- expect(builder.set_locator(data)).to eq(1)
125
- end
126
- end
127
-
128
- describe "#map_items" do
129
- before :each do
130
- builder.instance_variable_set('@list_data', new_list_data)
131
- allow(mapper_class).to receive(:new).and_return(mapper_instance)
132
- allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
133
- allow(mapper_instance).to receive(:perform_mapping)
134
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
135
- allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
136
- end
137
-
138
- context "successful" do
139
- it "should create a new ar list" do
140
- allow(mapper_instance).to receive(:errors).and_return({})
141
- builder.map_items
142
-
143
- expect(builder.instance_variable_get('@new_list')).to match_array([mapper_instance, mapper_instance])
144
- end
145
- end
146
-
147
- context "with errors" do
148
- it "should add to the error hash with the correct index" do
149
- allow(mapper_instance).to receive(:errors).and_return({}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}})
150
- builder.map_items
151
-
152
- expect(builder.instance_variable_get('@errors')).to eq({1=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
153
- end
154
- end
155
- end
156
-
157
- describe "#build" do
158
- before :each do
159
- allow(collection).to receive(:map).and_return(existing_ids)
160
- allow(builder).to receive(:locator_for).and_return(1)
161
- allow(mapper_class).to receive(:new).and_return(mapper_instance)
162
- allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
163
- allow(mapper_instance).to receive(:perform_mapping)
164
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
165
- allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
166
- end
167
-
168
- context "successful" do
169
- before :each do
170
- allow(mapper_instance).to receive(:errors).and_return({})
171
- end
172
-
173
- it "should return a hash with save keys" do
174
- expect(builder.build[:save]).to match_array([mapper_instance, mapper_instance])
175
- end
176
-
177
- it "should return a hash with delete keys" do
178
- expect(builder.build[:delete]).to match_array([2,3])
179
- end
180
- end
181
-
182
- context "with errors" do
183
- before :each do
184
- allow(mapper_instance).to receive(:errors).and_return({data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {})
185
- builder.build
186
- end
187
-
188
- it "should return errors" do
189
- expect(builder.errors).to eq({0=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
190
- end
191
- end
192
- end
193
- end
@@ -1,210 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Xing::Builders::OrderedListDifferenceBuilder do
4
-
5
- let :builder do
6
- Xing::Builders::OrderedListDifferenceBuilder.new(list_data, collection, mapper_class)
7
- end
8
-
9
- let :mapper_class do
10
- double("ItemMapper")
11
- end
12
-
13
- let :mapper_instance do
14
- double("mapper")
15
- end
16
-
17
- let :collection do
18
- [double("Relation AR Object id: 1"), double("Relation AR Object id: 2"), double("Relation AR Object id: 3")]
19
- end
20
-
21
- let :new_ar_object do
22
- double("New Relation AR Object")
23
- end
24
-
25
- let :updated_ar_object do
26
- double("Updated AR Object")
27
- end
28
-
29
- let :existing_ids do
30
- [1,2,3]
31
- end
32
-
33
- let :list_data do
34
- [
35
- {
36
- links: {
37
- self: "/somethings/1"
38
- },
39
- data: {
40
- stuff: "some updated stuff"
41
- }
42
- },
43
- {
44
- links: {
45
- self: ""
46
- },
47
- data: {
48
- stuff: "some new stuff"
49
- }
50
- }
51
- ]
52
- end
53
-
54
- let :new_list_data do
55
- [
56
- {
57
- locator: 1,
58
- incoming: {
59
- links: {
60
- self: "/somethings/1"
61
- },
62
- data: {
63
- stuff: "some updated stuff"
64
- }
65
- }
66
- },
67
- {
68
- locator: nil,
69
- incoming: {
70
- links: {
71
- self: ""
72
- },
73
- data: {
74
- stuff: "some new stuff"
75
- }
76
- }
77
- }
78
- ]
79
- end
80
-
81
- it "should initialize and assign list_data, collection, mapper class and errors" do
82
- expect(builder.instance_variable_get('@list_data')).to eq(list_data)
83
- expect(builder.instance_variable_get('@collection')).to eq(collection)
84
- expect(builder.instance_variable_get('@mapper_class')).to eq(mapper_class)
85
- expect(builder.instance_variable_get('@errors')).to eq({})
86
- end
87
-
88
- describe "#sort_json_items" do
89
- before :each do
90
- allow(collection).to receive(:map).and_return(existing_ids)
91
- allow(builder).to receive(:locator_for).and_return(1)
92
- builder.sort_json_items
93
- end
94
-
95
- it "should create a list of existing ids" do
96
- expect(builder.instance_variable_get('@existing_ids')).to eq(existing_ids)
97
- end
98
-
99
- it "should insert index and locator into the item data" do
100
- expect(builder.instance_variable_get('@list_data')).to eq(new_list_data)
101
- end
102
-
103
- it "should find the ids to delete" do
104
- expect(builder.instance_variable_get('@delete_ids')).to eq([2,3])
105
- end
106
- end
107
-
108
- describe "#set_locator" do
109
-
110
- it "should be nil if links is empty" do
111
- data = { links: {}, data: { text:"bs" } }
112
- expect(builder.set_locator(data)).to be_nil
113
- end
114
-
115
- it "should be nil if links/self is empty" do
116
- data = { links: {self: ""}}
117
- expect(builder.set_locator(data)).to be_nil
118
- end
119
-
120
- it "should set locator if links/self is present" do
121
- data = { links: {self: "some_link/1/something"}}
122
- allow(builder).to receive(:locator_for).and_return(1)
123
-
124
- expect(builder.set_locator(data)).to eq(1)
125
- end
126
- end
127
-
128
- describe "#map_items" do
129
- before :each do
130
- builder.instance_variable_set('@list_data', new_list_data)
131
- allow(mapper_class).to receive(:new).and_return(mapper_instance)
132
- allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
133
- allow(mapper_instance).to receive(:perform_mapping)
134
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
135
- allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
136
- end
137
-
138
- context "successful" do
139
- it "should create a new ar list" do
140
- allow(mapper_instance).to receive(:errors).and_return({})
141
- builder.map_items
142
-
143
- expect(builder.instance_variable_get('@new_list')).to match_array([mapper_instance, mapper_instance])
144
- end
145
- end
146
-
147
- context "with errors" do
148
- it "should add to the error hash with the correct index" do
149
- allow(mapper_instance).to receive(:errors).and_return({}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}})
150
- builder.map_items
151
-
152
- expect(builder.instance_variable_get('@errors')).to eq({1=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
153
- end
154
- end
155
- end
156
-
157
- describe "#set_position" do
158
-
159
- it "should update the position if the record has a position attribute" do
160
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(true)
161
- expect(new_ar_object).to receive(:position=).with(1)
162
-
163
- builder.set_position(new_ar_object, 1)
164
- end
165
-
166
- it "should not blow up if the record does not have a position attribute" do
167
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
168
- expect(new_ar_object).to_not receive(:position=)
169
-
170
- builder.set_position(new_ar_object, 1)
171
- end
172
- end
173
-
174
- describe "#build" do
175
- before :each do
176
- allow(collection).to receive(:map).and_return(existing_ids)
177
- allow(builder).to receive(:locator_for).and_return(1)
178
- allow(mapper_class).to receive(:new).and_return(mapper_instance)
179
- allow(mapper_instance).to receive(:record).and_return(new_ar_object, new_ar_object, updated_ar_object, updated_ar_object)
180
- allow(mapper_instance).to receive(:perform_mapping)
181
- allow(new_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
182
- allow(updated_ar_object).to receive(:has_attribute?).with(:position).and_return(false)
183
- end
184
-
185
- context "successful" do
186
- before :each do
187
- allow(mapper_instance).to receive(:errors).and_return({})
188
- end
189
-
190
- it "should return a hash with save keys" do
191
- expect(builder.build[:save]).to match_array([mapper_instance, mapper_instance])
192
- end
193
-
194
- it "should return a hash with delete keys" do
195
- expect(builder.build[:delete]).to match_array([2,3])
196
- end
197
- end
198
-
199
- context "with errors" do
200
- before :each do
201
- allow(mapper_instance).to receive(:errors).and_return({data: {type: "I would do anything for love", message: "but I won't do that"}}, {data: {type: "I would do anything for love", message: "but I won't do that"}}, {})
202
- builder.build
203
- end
204
-
205
- it "should return errors" do
206
- expect(builder.errors).to eq({0=>{:type=>"I would do anything for love", :message=>"but I won't do that"}})
207
- end
208
- end
209
- end
210
- end