restpack_serializer 0.4.5 → 0.4.6

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: 8bb868ddc338559c261cf11b8b18c971b2d80bd7
4
- data.tar.gz: d667ad9327dd544adbb23eff8b2c15028e917e28
3
+ metadata.gz: d02fa321367307ada522ac8c84c68895ad53187b
4
+ data.tar.gz: 74d285e96499587df39af7183da04b5578f5a736
5
5
  SHA512:
6
- metadata.gz: 0ed2707234b857b4b5ea571fce09fe7247f2b8adf966f52045cc2540fbf1a2feb195a7bcff310fcd0b8d046bed11d9df70e786e1c0c073d76b6e7c61d68e248e
7
- data.tar.gz: d663714aa5135e88a14f4418f474b367217a3b933b5da2152d864f01804ad6578814f8217fb848822837de5728cbea487f3abd297a8c863170c154e349c2bc37
6
+ metadata.gz: c68fe0d43e8bb85c3743f5963c5526e33ac3f8b1914457056464dc69b1ce08edfd02743b74d0974b15d388ca01a89e343db14f1d4d302ab51890f9803e1fde05
7
+ data.tar.gz: 683ebf1933b2cdd9cb55148290d83b00ffac99b9bb5cd7a7178641b6581bd7e8df0dc6456c034667862c78bb12e1525533ede864393c34b5607cc25dc8be5425
data/README.md CHANGED
@@ -271,7 +271,7 @@ which yields:
271
271
 
272
272
  * http://restpack-serializer-sample.herokuapp.com/albums.json?includes=songs
273
273
 
274
- An album `:has_many` songs, so the side-loads are paged. We'll be soon adding URLs to the response meta data which will point to the next page of side-loaded data. These URLs will be something like:
274
+ An album `:has_many` songs, so the side-loaded songs are paged. The `meta.songs` includes `previous_href` and `next_href` which point to the previous and next page of this side-loaded data. These URLs take the form:
275
275
 
276
276
  * http://restpack-serializer-sample.herokuapp.com/songs.json?album_ids=1,2,3,4&page=2
277
277
 
@@ -0,0 +1,24 @@
1
+ module RestPack::Serializer::Filterable
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def serializable_filters
6
+ @serializable_filters
7
+ end
8
+
9
+ def can_filter_by(*attributes)
10
+ attributes.each do |attribute|
11
+ @serializable_filters ||= []
12
+ @serializable_filters << attribute.to_sym
13
+ end
14
+ end
15
+
16
+ def filterable_by
17
+ filters = [self.model_class.primary_key.to_sym]
18
+ filters += self.model_class.reflect_on_all_associations(:belongs_to).map(&:foreign_key).map(&:to_sym)
19
+
20
+ filters += @serializable_filters if @serializable_filters
21
+ filters.uniq
22
+ end
23
+ end
24
+ end
@@ -16,12 +16,6 @@ module RestPack::Serializer::SideLoading
16
16
  side_loads
17
17
  end
18
18
 
19
- def filterable_by
20
- filters = [self.model_class.primary_key.to_sym]
21
- filters += self.model_class.reflect_on_all_associations(:belongs_to).map(&:foreign_key).map(&:to_sym)
22
- filters.uniq
23
- end
24
-
25
19
  def can_includes
26
20
  @can_includes || []
27
21
  end
@@ -1,6 +1,7 @@
1
1
  require 'active_support/concern'
2
2
  require_relative "options"
3
3
  require_relative "serializable/attributes"
4
+ require_relative "serializable/filterable"
4
5
  require_relative "serializable/paging"
5
6
  require_relative "serializable/resource"
6
7
  require_relative "serializable/side_loading"
@@ -20,6 +21,7 @@ module RestPack
20
21
  include RestPack::Serializer::Paging
21
22
  include RestPack::Serializer::Resource
22
23
  include RestPack::Serializer::Attributes
24
+ include RestPack::Serializer::Filterable
23
25
  include RestPack::Serializer::SideLoading
24
26
 
25
27
  class InvalidInclude < Exception; end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.5'
3
+ VERSION = '0.4.6'
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Factory do
4
4
  let(:factory) { RestPack::Serializer::Factory }
@@ -3,12 +3,14 @@ module MyApp
3
3
  include RestPack::Serializer
4
4
  attributes :id, :title, :album_id
5
5
  can_include :albums, :artists
6
+ can_filter_by :title
6
7
  end
7
8
 
8
9
  class AlbumSerializer
9
10
  include RestPack::Serializer
10
11
  attributes :id, :title, :year, :artist_id
11
12
  can_include :artists, :songs
13
+ can_filter_by :year
12
14
  end
13
15
 
14
16
  class ArtistSerializer
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Attributes do
4
4
  class CustomSerializer
@@ -24,4 +24,4 @@ describe RestPack::Serializer::Attributes do
24
24
  it "correctly maps attribute with :key options" do
25
25
  @attributes[:new_key].should == :old_attribute
26
26
  end
27
- end
27
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestPack::Serializer::Filterable do
4
+ class CustomSerializer
5
+ include RestPack::Serializer
6
+ attributes :a, :b, :c
7
+
8
+ can_filter_by :a, :c
9
+ end
10
+
11
+ it "captures the specified filters" do
12
+ CustomSerializer.serializable_filters.should == [:a, :c]
13
+ end
14
+ end
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Options do
4
4
  let(:subject) { RestPack::Serializer::Options.new(MyApp::SongSerializer, params, scope) }
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Paging do
4
4
  before(:each) do
@@ -45,6 +45,22 @@ describe RestPack::Serializer::Paging do
45
45
  end
46
46
  end
47
47
 
48
+ context "with custom filter" do
49
+ context "valid :title" do
50
+ let(:params) { { title: @album1.songs[0].title } }
51
+ it "returns the album" do
52
+ page[:meta][:songs][:count].should == 1
53
+ end
54
+ end
55
+
56
+ context "invalid :title" do
57
+ let(:params) { { title: "this doesn't exist" } }
58
+ it "returns the album" do
59
+ page[:meta][:songs][:count].should == 0
60
+ end
61
+ end
62
+ end
63
+
48
64
  it "serializes results" do
49
65
  first = MyApp::Song.first
50
66
  page[:songs].first.should == {
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::Resource do
4
4
  before(:each) do
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer do
4
4
  let(:serializer) { PersonSerializer.new }
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::SideLoading do
4
4
  context "when side-loading" do
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::SideLoading do
4
4
  context "when side-loading" do
@@ -1,4 +1,4 @@
1
- require './spec/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RestPack::Serializer::SideLoading do
4
4
  context "invalid :includes" do
@@ -77,12 +77,12 @@ describe RestPack::Serializer::SideLoading do
77
77
  end
78
78
  context "a model with a single :belongs_torelations" do
79
79
  it "is filterable by primary key and foreign keys" do
80
- MyApp::AlbumSerializer.filterable_by.should =~ [:id, :artist_id]
80
+ MyApp::AlbumSerializer.filterable_by.should =~ [:id, :artist_id, :year]
81
81
  end
82
82
  end
83
83
  context "a model with multiple :belongs_to relations" do
84
84
  it "is filterable by primary key and foreign keys" do
85
- MyApp::SongSerializer.filterable_by.should =~ [:id, :artist_id, :album_id]
85
+ MyApp::SongSerializer.filterable_by.should =~ [:id, :artist_id, :album_id, :title]
86
86
  end
87
87
  end
88
88
  end
@@ -1,4 +1,4 @@
1
- require 'factory_girl'
1
+ require 'factory_girl'
2
2
 
3
3
  FactoryGirl.define do
4
4
  factory :artist, :class => MyApp::Artist do
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.4.5
4
+ version: 0.4.6
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-09-18 00:00:00.000000000 Z
11
+ date: 2013-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -227,6 +227,7 @@ files:
227
227
  - lib/restpack_serializer/options.rb
228
228
  - lib/restpack_serializer/serializable.rb
229
229
  - lib/restpack_serializer/serializable/attributes.rb
230
+ - lib/restpack_serializer/serializable/filterable.rb
230
231
  - lib/restpack_serializer/serializable/paging.rb
231
232
  - lib/restpack_serializer/serializable/resource.rb
232
233
  - lib/restpack_serializer/serializable/side_loading.rb
@@ -237,6 +238,7 @@ files:
237
238
  - spec/fixtures/serializers.rb
238
239
  - spec/restpack_serializer_spec.rb
239
240
  - spec/serializable/attributes_spec.rb
241
+ - spec/serializable/filterable_spec.rb
240
242
  - spec/serializable/options_spec.rb
241
243
  - spec/serializable/paging_spec.rb
242
244
  - spec/serializable/resource_spec.rb
@@ -265,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
265
267
  version: '0'
266
268
  requirements: []
267
269
  rubyforge_project:
268
- rubygems_version: 2.0.7
270
+ rubygems_version: 2.0.5
269
271
  signing_key:
270
272
  specification_version: 4
271
273
  summary: Model serialization, paging, side-loading and filtering
@@ -275,6 +277,7 @@ test_files:
275
277
  - spec/fixtures/serializers.rb
276
278
  - spec/restpack_serializer_spec.rb
277
279
  - spec/serializable/attributes_spec.rb
280
+ - spec/serializable/filterable_spec.rb
278
281
  - spec/serializable/options_spec.rb
279
282
  - spec/serializable/paging_spec.rb
280
283
  - spec/serializable/resource_spec.rb