restpack_serializer 0.4.12 → 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 477bc16aca491e5318b8665a209f293ac5d4b933
4
- data.tar.gz: 9f5e498a6996a42b0e6cada857128a23412e5126
3
+ metadata.gz: c65793e190074e46598d0c1840e05fce55fd0c55
4
+ data.tar.gz: 0d6d15e20c00d2123d3a3ac584f51edd908e9ec5
5
5
  SHA512:
6
- metadata.gz: a9c20b94f9c75a1b18c38dd89a7e32686cc94e9c6101f29d52dbe937bb01d034047b8b8933886e89549a7433e77f417ac2a4a6e958f740797301944652ab1323
7
- data.tar.gz: ed68635e42185820ff8d39fc4b5ad5e4f76c33f0f89e67fbe326228c2c2c8c0623309f28976ba625d543297dd30beaec86a586d8cd1956218bf5d811da1d3f5a
6
+ metadata.gz: f1b48f4b0c4cbc764d9de0a495e0ead2125ad07d96ebbd4bb69a18a72298890e167b7bceab57555a5f623d06caab26dc86b292ca5056de745df86bbd1a01a624
7
+ data.tar.gz: e28f52414ae19524964d353a55fbc24380bfe5afff65d19739363828eb21aa6641e428aa60ae0bef55ac045087ee1cd7c0feeb28a8e9b5a04eca25fe56b21cdf
@@ -0,0 +1,12 @@
1
+ module RestPack::Serializer::Single
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def single(params = {}, scope = nil, context = {})
6
+ options = RestPack::Serializer::Options.new(self, params, scope, context)
7
+ model = options.scope_with_filters.first
8
+
9
+ return model ? self.as_json(model, context) : nil
10
+ end
11
+ end
12
+ end
@@ -4,6 +4,7 @@ require_relative "serializable/attributes"
4
4
  require_relative "serializable/filterable"
5
5
  require_relative "serializable/paging"
6
6
  require_relative "serializable/resource"
7
+ require_relative "serializable/single"
7
8
  require_relative "serializable/side_loading"
8
9
 
9
10
  module RestPack
@@ -20,6 +21,7 @@ module RestPack
20
21
 
21
22
  include RestPack::Serializer::Paging
22
23
  include RestPack::Serializer::Resource
24
+ include RestPack::Serializer::Single
23
25
  include RestPack::Serializer::Attributes
24
26
  include RestPack::Serializer::Filterable
25
27
  include RestPack::Serializer::SideLoading
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.12'
3
+ VERSION = '0.4.13'
4
4
  end
5
5
  end
data/spec/fixtures/db.rb CHANGED
@@ -50,7 +50,7 @@ module MyApp
50
50
 
51
51
  class Album < ActiveRecord::Base
52
52
  attr_accessible :title, :year, :artist
53
- scope :classic, where("year < 1950")
53
+ scope :classic, -> { where("year < 1950") }
54
54
 
55
55
  belongs_to :artist
56
56
  has_many :songs
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestPack::Serializer::Single do
4
+ before(:each) do
5
+ @album = FactoryGirl.create(:album_with_songs, song_count: 11)
6
+ @song = @album.songs.first
7
+ end
8
+
9
+ let(:resource) { MyApp::SongSerializer.single(params, scope, context) }
10
+ let(:params) { { id: @song.id } }
11
+ let(:scope) { nil }
12
+ let(:context) { { } }
13
+
14
+ it "returns a resource by id" do
15
+ resource[:id].should == @song.id.to_s
16
+ resource[:title].should == @song.title
17
+ end
18
+
19
+ context "with context" do
20
+ let(:context) { { reverse_title?: true } }
21
+
22
+ it "returns reversed titles" do
23
+ resource[:title].should == @song.title.reverse
24
+ end
25
+ end
26
+
27
+ context "invalid id" do
28
+ let(:params) { { id: @song.id + 100 } }
29
+
30
+ it "returns nil" do
31
+ resource.should == nil
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.4.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
@@ -232,6 +232,7 @@ files:
232
232
  - lib/restpack_serializer/serializable/paging.rb
233
233
  - lib/restpack_serializer/serializable/resource.rb
234
234
  - lib/restpack_serializer/serializable/side_loading.rb
235
+ - lib/restpack_serializer/serializable/single.rb
235
236
  - lib/restpack_serializer/version.rb
236
237
  - restpack_serializer.gemspec
237
238
  - spec/factory/factory_spec.rb
@@ -248,6 +249,7 @@ files:
248
249
  - spec/serializable/side_loading/belongs_to_spec.rb
249
250
  - spec/serializable/side_loading/has_many_spec.rb
250
251
  - spec/serializable/side_loading/side_loading_spec.rb
252
+ - spec/serializable/single_spec.rb
251
253
  - spec/spec_helper.rb
252
254
  - spec/support/factory.rb
253
255
  homepage: https://github.com/RestPack
@@ -288,6 +290,7 @@ test_files:
288
290
  - spec/serializable/side_loading/belongs_to_spec.rb
289
291
  - spec/serializable/side_loading/has_many_spec.rb
290
292
  - spec/serializable/side_loading/side_loading_spec.rb
293
+ - spec/serializable/single_spec.rb
291
294
  - spec/spec_helper.rb
292
295
  - spec/support/factory.rb
293
296
  has_rdoc: