restpack_serializer 0.2.9 → 0.2.10

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- M2M0YjM0NjM5MGExZWY2Y2JiZmJiZGU3ZDBiN2U0NWRkZTBlYWIyNg==
4
+ NTQyMWM4MjkwMzA1NDc2ZDdhOWU3MzNlZDQzZmM3YWRkNzBlYTNkZA==
5
5
  data.tar.gz: !binary |-
6
- ZDNiOTQxNTk0ZDM4NDZhZGExZDM3MGVjMTQ5OGUwOGJlYzg0YzQ3YQ==
6
+ ZjczZTI5ZmJkZDRiNTViY2Y1Y2E2ZTJmNTU5YWIxOTBiZmY3ZjE3NQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OTQ4MTA0NmI5NDcxOTU3NTgxNWM0MTc3MzJjNzk4ZGYzYzBkZWYzY2I4NGFh
10
- YzA4NmRhMDdiYjFhODM2YWYwMzRhNjU1ZDAxNTIxNDNhMzhiODY1ZDE3MzE4
11
- M2Y5ZjkzMzU4Y2ZhNGQzOTg1OWEwMGZmYmE1ZjU4YWRiNmFkNzQ=
9
+ OWY5ZTZjYzY2MGZhNzk3MjkwZmYwMDcyN2NmYmNiZmU4ZjFhNWUyMzYzZmJh
10
+ OWY1ZGJlMDY3Mjk3NTIyOTU4NDUxMTRkZmZmMDg4MzQ5YThkMzQxMzU0NzRk
11
+ ZjgyOTkzOGJiYTc2ZDY4YjBlZjBiNjFjODMyMDA0MGRmNWJiMGQ=
12
12
  data.tar.gz: !binary |-
13
- ZWIyYjkzZTQzNjNhYzFhNmRjZTgyYjBlN2JhNzRjOWQ3N2YxOTg1MWI1ZmQ5
14
- NzI5NTk2NTVlYjljM2U0OTMxZjRkYzZmMDM2YTAyNmEzMmYzOTAyNDJhNDM0
15
- M2IyOTNhNDdhYmE2NzAyYTk2ZDBkZTNmNzZmM2FjODQ4ZGQzOGQ=
13
+ ZGI3YzY0ZDI0NWVkODIxOTE0ZTU4OTA4YWU4MzYxMWI3NjQ2MzcyY2ExYzhk
14
+ MzdkYjJjNjUxYTIxY2I4YzBmM2UzMDRmZDY4Mjc5ZjdmZGEzZDA3YWFkZjQw
15
+ ZTMxOTU0ODNiZDI1YTI3MDNmNGU5NWE5NTkxNDhlM2M2YzViZmM=
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  *.db
3
3
  tmp
4
4
  /coverage
5
+ Gemfile.lock
@@ -40,7 +40,7 @@ module RestPack::Serializer::Attributes
40
40
 
41
41
  unless method_defined?(method)
42
42
  define_method method do
43
- @options[method].nil? || @options[method]
43
+ @context[method].nil? || @context[method]
44
44
  end
45
45
  end
46
46
  end
@@ -16,8 +16,8 @@ module RestPack
16
16
 
17
17
  class InvalidInclude < Exception; end
18
18
 
19
- def as_json(model, options = {})
20
- @model, @options = model, options
19
+ def as_json(model, context = {})
20
+ @model, @context = model, context
21
21
 
22
22
  data = {}
23
23
  if self.class.serializable_attributes.present?
@@ -26,18 +26,31 @@ module RestPack
26
26
  end
27
27
  end
28
28
 
29
+ add_custom_attributes(data)
29
30
  add_links(model, data)
30
31
 
31
32
  data
32
33
  end
33
34
 
35
+ def custom_attributes
36
+ {}
37
+ end
38
+
34
39
  private
35
40
 
41
+ def add_custom_attributes(data)
42
+ custom = custom_attributes
43
+ data.merge!(custom) if custom
44
+ end
45
+
36
46
  def add_links(model, data)
37
47
  self.class.associations.each do |association|
38
48
  if association.macro == :belongs_to
39
49
  data[:links] ||= {}
40
- data[:links][association.name.to_sym] = model.send(association.foreign_key).to_s
50
+ foreign_key_value = model.send(association.foreign_key)
51
+ if foreign_key_value
52
+ data[:links][association.name.to_sym] = foreign_key_value.to_s
53
+ end
41
54
  end
42
55
  end
43
56
  data
@@ -48,8 +61,8 @@ module RestPack
48
61
  end
49
62
 
50
63
  module ClassMethods
51
- def as_json(model, options = {})
52
- new.as_json(model, options)
64
+ def as_json(model, context = {})
65
+ new.as_json(model, context)
53
66
  end
54
67
 
55
68
  def model_name
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.2.9'
3
+ VERSION = '0.2.10'
4
4
  end
5
5
  end
@@ -37,4 +37,13 @@ describe RestPack::Serializer::Resource do
37
37
  # https://github.com/RestPack/restpack_serializer/issues/27
38
38
  # https://github.com/json-api/json-api/issues/7
39
39
  end
40
+
41
+ describe "song with no artist" do
42
+ let(:song) { FactoryGirl.create(:song, :artist => nil) }
43
+ let(:resource) { SongSerializer.resource(id: song.id.to_s) }
44
+
45
+ it "should not have an artist link" do
46
+ resource[:songs][0][:links].keys.should_not include(:artist)
47
+ end
48
+ end
40
49
  end
@@ -40,21 +40,29 @@ describe RestPack::Serializer do
40
40
  end
41
41
 
42
42
  def include_admin_info?
43
- @options[:is_admin?]
43
+ @context[:is_admin?]
44
+ end
45
+
46
+ def custom_attributes
47
+ {
48
+ :custom_key => "custom value for model id #{@model.id}"
49
+ }
44
50
  end
45
51
  end
46
52
 
47
53
  describe ".as_json" do
48
54
  it "serializes specified attributes" do
49
55
  serializer.as_json(person).should == {
50
- id: '123', name: 'Gavin', description: 'This is person #123', href: '/people/123.json'
56
+ id: '123', name: 'Gavin', description: 'This is person #123',
57
+ href: '/people/123.json', custom_key: 'custom value for model id 123'
51
58
  }
52
59
  end
53
60
 
54
61
  context "with options" do
55
62
  it "excludes specified attributes" do
56
63
  serializer.as_json(person, { include_description?: false }).should == {
57
- id: '123', name: 'Gavin', href: '/people/123.json'
64
+ id: '123', name: 'Gavin', href: '/people/123.json',
65
+ custom_key: 'custom value for model id 123'
58
66
  }
59
67
  end
60
68
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-25 00:00:00.000000000 Z
11
+ date: 2013-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -175,7 +175,6 @@ files:
175
175
  - .rspec
176
176
  - .travis.yml
177
177
  - Gemfile
178
- - Gemfile.lock
179
178
  - Guardfile
180
179
  - LICENSE
181
180
  - README.md
data/Gemfile.lock DELETED
@@ -1,102 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- restpack_serializer (0.2.9)
5
- activerecord (>= 3.0)
6
- activesupport (>= 3.0)
7
- will_paginate (~> 3.0)
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- activemodel (3.2.13)
13
- activesupport (= 3.2.13)
14
- builder (~> 3.0.0)
15
- activerecord (3.2.13)
16
- activemodel (= 3.2.13)
17
- activesupport (= 3.2.13)
18
- arel (~> 3.0.2)
19
- tzinfo (~> 0.3.29)
20
- activesupport (3.2.13)
21
- i18n (= 0.6.1)
22
- multi_json (~> 1.0)
23
- arel (3.0.2)
24
- builder (3.0.4)
25
- bump (0.4.1)
26
- coderay (1.0.9)
27
- colorize (0.5.8)
28
- coveralls (0.6.7)
29
- colorize
30
- multi_json (~> 1.3)
31
- rest-client
32
- simplecov (>= 0.7)
33
- thor
34
- database_cleaner (0.9.1)
35
- diff-lcs (1.2.3)
36
- factory_girl (4.2.0)
37
- activesupport (>= 3.0.0)
38
- ffi (1.7.0)
39
- formatador (0.2.4)
40
- growl (1.0.3)
41
- guard (1.8.0)
42
- formatador (>= 0.2.4)
43
- listen (>= 1.0.0)
44
- lumberjack (>= 1.0.2)
45
- pry (>= 0.9.10)
46
- thor (>= 0.14.6)
47
- guard-rspec (2.5.4)
48
- guard (>= 1.1)
49
- rspec (~> 2.11)
50
- i18n (0.6.1)
51
- listen (1.0.0)
52
- rb-fsevent (>= 0.9.3)
53
- rb-inotify (>= 0.9)
54
- rb-kqueue (>= 0.2)
55
- lumberjack (1.0.3)
56
- method_source (0.8.1)
57
- mime-types (1.23)
58
- multi_json (1.7.2)
59
- pry (0.9.12)
60
- coderay (~> 1.0.5)
61
- method_source (~> 0.8)
62
- slop (~> 3.4)
63
- rake (10.0.4)
64
- rb-fsevent (0.9.3)
65
- rb-inotify (0.9.0)
66
- ffi (>= 0.5.0)
67
- rb-kqueue (0.2.0)
68
- ffi (>= 0.5.0)
69
- rest-client (1.6.7)
70
- mime-types (>= 1.16)
71
- rspec (2.13.0)
72
- rspec-core (~> 2.13.0)
73
- rspec-expectations (~> 2.13.0)
74
- rspec-mocks (~> 2.13.0)
75
- rspec-core (2.13.1)
76
- rspec-expectations (2.13.0)
77
- diff-lcs (>= 1.1.3, < 2.0)
78
- rspec-mocks (2.13.1)
79
- simplecov (0.7.1)
80
- multi_json (~> 1.0)
81
- simplecov-html (~> 0.7.1)
82
- simplecov-html (0.7.1)
83
- slop (3.4.4)
84
- sqlite3 (1.3.7)
85
- thor (0.18.1)
86
- tzinfo (0.3.37)
87
- will_paginate (3.0.4)
88
-
89
- PLATFORMS
90
- ruby
91
-
92
- DEPENDENCIES
93
- bump
94
- coveralls
95
- database_cleaner (~> 0.9.1)
96
- factory_girl (~> 4.2.0)
97
- growl (~> 1.0.3)
98
- guard-rspec (~> 2.5.4)
99
- rake (~> 10.0.3)
100
- restpack_serializer!
101
- rspec (~> 2.12)
102
- sqlite3 (~> 1.3.7)