active_model_version_serializers 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_model/version_serializer.rb +2 -2
- data/lib/active_model/version_serializers/version.rb +1 -1
- data/spec/scenerio/inheritance_spec.rb +40 -0
- data/spec/support/factories.rb +13 -0
- data/spec/support/models/car.rb +4 -0
- data/spec/support/serializers/inherited_serializer.rb +14 -0
- metadata +8 -2
@@ -63,9 +63,9 @@ module ActiveModel
|
|
63
63
|
#
|
64
64
|
# @param [Symbol] version
|
65
65
|
# @param [Proc] block
|
66
|
-
def self.version(version, &block)
|
66
|
+
def self.version(version, superclass = ActiveModel::Serializer, &block)
|
67
67
|
base_class = self
|
68
|
-
vklass = Class.new(
|
68
|
+
vklass = Class.new(superclass) do
|
69
69
|
self.root(base_class._root)
|
70
70
|
alias_method base_class._name.to_sym, :object
|
71
71
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
3
|
+
|
4
|
+
describe "InheritedSerialization" do
|
5
|
+
context "v1" do
|
6
|
+
context "serialization of a car object" do
|
7
|
+
let(:car_attributes) do
|
8
|
+
FactoryGirl.attributes_for(:v1_car)
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:car) do
|
12
|
+
FactoryGirl.build(:v1_car, car_attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:car_serializer) do
|
16
|
+
CarSerializer.new(car)
|
17
|
+
end
|
18
|
+
|
19
|
+
subject {car_serializer.as_json}
|
20
|
+
it {should eq({car: car_attributes})}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "v2" do
|
25
|
+
let(:car_attributes) do
|
26
|
+
FactoryGirl.attributes_for(:v2_car)
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:car) do
|
30
|
+
FactoryGirl.build(:v2_car, car_attributes)
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:car_serializer) do
|
34
|
+
CarSerializer.new(car, version: :v2)
|
35
|
+
end
|
36
|
+
|
37
|
+
subject {car_serializer.as_json}
|
38
|
+
it {should eq({car: car_attributes})}
|
39
|
+
end
|
40
|
+
end
|
data/spec/support/factories.rb
CHANGED
@@ -43,4 +43,17 @@ FactoryGirl.define do
|
|
43
43
|
factory :v3_turn, traits: [:turn_version_1, :turn_version_2]
|
44
44
|
factory :v4_turn, traits: [:turn_version_1, :turn_version_2]
|
45
45
|
end
|
46
|
+
|
47
|
+
factory :car do
|
48
|
+
trait :car_ish do
|
49
|
+
audio_system { [:none, :radio, :cd_player, :turntables].sample }
|
50
|
+
wheels { [3, 4, 6, 8].sample }
|
51
|
+
end
|
52
|
+
|
53
|
+
factory :v1_car, traits: [:car_ish] do
|
54
|
+
colour { [:red, :green, :blue, :black, :white].sample }
|
55
|
+
end
|
56
|
+
|
57
|
+
factory :v2_car, traits: [:car_ish]
|
58
|
+
end
|
46
59
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class VehicleSerializer < ActiveModel::Serializer
|
2
|
+
attributes :colour, :wheels
|
3
|
+
end
|
4
|
+
|
5
|
+
class CarSerializer < ActiveModel::VersionSerializer
|
6
|
+
version :v1, VehicleSerializer do
|
7
|
+
attributes :audio_system
|
8
|
+
end
|
9
|
+
|
10
|
+
version :v2 do
|
11
|
+
version_attributes :v1, without: [:colour]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_version_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active_model_serializers
|
@@ -141,11 +141,14 @@ files:
|
|
141
141
|
- lib/active_model/version_serializer.rb
|
142
142
|
- lib/active_model/version_serializers/version.rb
|
143
143
|
- lib/active_model_version_serializers.rb
|
144
|
+
- spec/scenerio/inheritance_spec.rb
|
144
145
|
- spec/scenerio/user_serialization_spec.rb
|
145
146
|
- spec/spec_helper.rb
|
146
147
|
- spec/support/factories.rb
|
148
|
+
- spec/support/models/car.rb
|
147
149
|
- spec/support/models/turn.rb
|
148
150
|
- spec/support/models/user.rb
|
151
|
+
- spec/support/serializers/inherited_serializer.rb
|
149
152
|
- spec/support/serializers/turn_serializer.rb
|
150
153
|
- spec/support/serializers/user_serializer.rb
|
151
154
|
- spec/version_spec.rb
|
@@ -174,11 +177,14 @@ signing_key:
|
|
174
177
|
specification_version: 3
|
175
178
|
summary: Active Model Serializer with versioning
|
176
179
|
test_files:
|
180
|
+
- spec/scenerio/inheritance_spec.rb
|
177
181
|
- spec/scenerio/user_serialization_spec.rb
|
178
182
|
- spec/spec_helper.rb
|
179
183
|
- spec/support/factories.rb
|
184
|
+
- spec/support/models/car.rb
|
180
185
|
- spec/support/models/turn.rb
|
181
186
|
- spec/support/models/user.rb
|
187
|
+
- spec/support/serializers/inherited_serializer.rb
|
182
188
|
- spec/support/serializers/turn_serializer.rb
|
183
189
|
- spec/support/serializers/user_serializer.rb
|
184
190
|
- spec/version_spec.rb
|