restpack_serializer 0.5.6 → 0.5.7

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: 5d9ebc87b6344eadafb83a213f41bbf0cff31ba8
4
- data.tar.gz: c4f02a1c7357fda3564d4914283a661b68993309
3
+ metadata.gz: 999010d07a5c68b823e2f2c36496dd1a8b2adbcc
4
+ data.tar.gz: 16eca8065b150b5694aa041564f8598361004544
5
5
  SHA512:
6
- metadata.gz: 0ba7ae1a03d00c7bb84653881165c4765f9f1177249c119025d370084b70dba5d589470ceb280c09e82374a1805a68f61c494613f5e1e430adf2651b13e0c53e
7
- data.tar.gz: bad906f6db7243f1aec6e248917c8120606a841509968eb417e08140f180bb15a36e0e6dd50f9730223d6e63db2dcd1e0ad74f64e07c1471c163f4f394fd86c3
6
+ metadata.gz: a0be747ac4aa315149718107badbfc1327c2c70fc772679deae2c533ed24442a6be4199249644f1aa35eaaabbe408f053530dbc477a28fa88a3e694379ba1bbd
7
+ data.tar.gz: b0a43b5d5423e078ce5d521aa8853882f50c7d2d5329ffdc70e21a79bf15aeada5f53fa74299cb3cdefb6a511c5a1233a1428a5a8e3286e73497b3e873aebaf5
@@ -9,7 +9,7 @@ module RestPack::Serializer
9
9
 
10
10
  @page = params[:page] ? params[:page].to_i : 1
11
11
  @page_size = params[:page_size] ? params[:page_size].to_i : RestPack::Serializer.config.page_size
12
- @include = params[:include] ? params[:include].split(',').map(&:to_sym) : []
12
+ @include = params[:include] ? params[:include].split(',') : []
13
13
  @filters = filters_from_params(params, serializer)
14
14
  @sorting = sorting_from_params(params, serializer)
15
15
  @serializer = serializer
@@ -9,8 +9,8 @@ module RestPack
9
9
  end
10
10
 
11
11
  def side_load_belongs_to
12
- foreign_keys = @models.map { |model| model.send(@association.foreign_key) }.uniq
13
- side_load = @association.klass.find(foreign_keys)
12
+ foreign_keys = @models.map { |model| model.send(@association.foreign_key) }.uniq.compact
13
+ side_load = foreign_keys.any? ? @association.klass.find(foreign_keys) : []
14
14
  json_model_data = side_load.map { |model| @serializer.as_json(model) }
15
15
  { @association.plural_name.to_sym => json_model_data, meta: { } }
16
16
  end
@@ -18,7 +18,7 @@ module RestPack
18
18
  def side_load_has_many
19
19
  has_association_relation do |options|
20
20
  if join_table = @association.options[:through]
21
- options.scope = options.scope.joins(join_table)
21
+ options.scope = options.scope.joins(join_table).distinct
22
22
  association_fk = @association.through_reflection.foreign_key.to_sym
23
23
  options.filters = { join_table => { association_fk => model_ids } }
24
24
  else
@@ -68,7 +68,7 @@ module RestPack::Serializer::SideLoading
68
68
  end
69
69
 
70
70
  def association_from_include(include)
71
- raise_invalid_include(include) unless self.can_includes.include?(include)
71
+ raise_invalid_include(include) unless can_include?(include)
72
72
  possible_relations = [include.to_s.singularize.to_sym, include]
73
73
  select_association_from_possibles(possible_relations)
74
74
  end
@@ -82,6 +82,12 @@ module RestPack::Serializer::SideLoading
82
82
  raise_invalid_include(include)
83
83
  end
84
84
 
85
+ def can_include?(include)
86
+ !!self.can_includes.index do |can_include|
87
+ can_include == include || can_include.to_s == include
88
+ end
89
+ end
90
+
85
91
  def raise_invalid_include(include)
86
92
  raise RestPack::Serializer::InvalidInclude.new,
87
93
  ":#{include} is not a valid include for #{self.model_class}"
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.5.6'
3
+ VERSION = '0.5.7'
4
4
  end
5
5
  end
@@ -24,7 +24,7 @@ describe RestPack::Serializer::Options do
24
24
 
25
25
  describe 'with include' do
26
26
  let(:params) { { 'include' => 'model1,model2' } }
27
- it { subject.include.should == [:model1, :model2] }
27
+ it { subject.include.should == ["model1", "model2"] }
28
28
  end
29
29
 
30
30
  context 'with filters' do
@@ -135,7 +135,7 @@ describe RestPack::Serializer::Paging do
135
135
  end
136
136
 
137
137
  it "includes the side-loads in the main meta data" do
138
- page[:meta][:songs][:include].should == [:albums]
138
+ page[:meta][:songs][:include].should == ["albums"]
139
139
  end
140
140
 
141
141
  it "includes the side-loads in page hrefs" do
@@ -33,7 +33,7 @@ describe RestPack::Serializer::Resource do
33
33
  end
34
34
 
35
35
  it "includes the side-loads in the main meta data" do
36
- resource[:meta][:songs][:include].should == [:albums]
36
+ resource[:meta][:songs][:include].should == ["albums"]
37
37
  end
38
38
  end
39
39
 
@@ -67,6 +67,19 @@ describe RestPack::Serializer::SideLoading do
67
67
  end
68
68
  end
69
69
 
70
+ context 'without an associated model' do
71
+ let!(:b_side) { FactoryGirl.create(:song, album: nil) }
72
+ let(:models) { [b_side] }
73
+
74
+ context 'when including :albums' do
75
+ let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
76
+
77
+ it 'return a hash with no data' do
78
+ side_loads.should == { :meta => {}, :albums => [] }
79
+ end
80
+ end
81
+ end
82
+
70
83
  end
71
84
  end
72
85
  end
@@ -65,6 +65,17 @@ describe RestPack::Serializer::SideLoading do
65
65
  side_loads[:meta][:fans][:page].should == 1
66
66
  side_loads[:meta][:fans][:count].should == expected_count
67
67
  end
68
+
69
+ context "when there are shared fans" do
70
+ before do
71
+ artist_1.fans << artist_2.fans.first
72
+ end
73
+ it "should not include duplicates in the linked resource collection" do
74
+ expected_count = (artist_1.fans + artist_2.fans).uniq.count
75
+ expect(side_loads[:fans].count).to eq(expected_count)
76
+ expect(side_loads[:meta][:fans][:count]).to eq(expected_count)
77
+ end
78
+ end
68
79
  end
69
80
  end
70
81
  end
metadata CHANGED
@@ -1,207 +1,207 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-02 00:00:00.000000000 Z
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.3
20
- - - <
20
+ - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '5.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - '>='
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: 4.0.3
30
- - - <
30
+ - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activerecord
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - '>='
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: 4.0.3
40
- - - <
40
+ - - "<"
41
41
  - !ruby/object:Gem::Version
42
42
  version: '5.0'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - '>='
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: 4.0.3
50
- - - <
50
+ - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '5.0'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: kaminari
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - ~>
57
+ - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: 0.16.1
60
60
  type: :runtime
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - ~>
64
+ - - "~>"
65
65
  - !ruby/object:Gem::Version
66
66
  version: 0.16.1
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: restpack_gem
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - ~>
71
+ - - "~>"
72
72
  - !ruby/object:Gem::Version
73
73
  version: 0.0.9
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - ~>
78
+ - - "~>"
79
79
  - !ruby/object:Gem::Version
80
80
  version: 0.0.9
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rake
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ~>
85
+ - - "~>"
86
86
  - !ruby/object:Gem::Version
87
87
  version: 10.0.3
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - ~>
92
+ - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: 10.0.3
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: guard-rspec
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - ~>
99
+ - - "~>"
100
100
  - !ruby/object:Gem::Version
101
101
  version: 2.5.4
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - ~>
106
+ - - "~>"
107
107
  - !ruby/object:Gem::Version
108
108
  version: 2.5.4
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: growl
111
111
  requirement: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - ~>
113
+ - - "~>"
114
114
  - !ruby/object:Gem::Version
115
115
  version: 1.0.3
116
116
  type: :development
117
117
  prerelease: false
118
118
  version_requirements: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - ~>
120
+ - - "~>"
121
121
  - !ruby/object:Gem::Version
122
122
  version: 1.0.3
123
123
  - !ruby/object:Gem::Dependency
124
124
  name: factory_girl
125
125
  requirement: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ~>
127
+ - - "~>"
128
128
  - !ruby/object:Gem::Version
129
129
  version: 4.2.0
130
130
  type: :development
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - ~>
134
+ - - "~>"
135
135
  - !ruby/object:Gem::Version
136
136
  version: 4.2.0
137
137
  - !ruby/object:Gem::Dependency
138
138
  name: sqlite3
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - ~>
141
+ - - "~>"
142
142
  - !ruby/object:Gem::Version
143
143
  version: 1.3.7
144
144
  type: :development
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - ~>
148
+ - - "~>"
149
149
  - !ruby/object:Gem::Version
150
150
  version: 1.3.7
151
151
  - !ruby/object:Gem::Dependency
152
152
  name: database_cleaner
153
153
  requirement: !ruby/object:Gem::Requirement
154
154
  requirements:
155
- - - ~>
155
+ - - "~>"
156
156
  - !ruby/object:Gem::Version
157
157
  version: 1.0.1
158
158
  type: :development
159
159
  prerelease: false
160
160
  version_requirements: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - ~>
162
+ - - "~>"
163
163
  - !ruby/object:Gem::Version
164
164
  version: 1.0.1
165
165
  - !ruby/object:Gem::Dependency
166
166
  name: rspec
167
167
  requirement: !ruby/object:Gem::Requirement
168
168
  requirements:
169
- - - '>='
169
+ - - ">="
170
170
  - !ruby/object:Gem::Version
171
171
  version: '0'
172
172
  type: :development
173
173
  prerelease: false
174
174
  version_requirements: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - '>='
176
+ - - ">="
177
177
  - !ruby/object:Gem::Version
178
178
  version: '0'
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: bump
181
181
  requirement: !ruby/object:Gem::Requirement
182
182
  requirements:
183
- - - '>='
183
+ - - ">="
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  type: :development
187
187
  prerelease: false
188
188
  version_requirements: !ruby/object:Gem::Requirement
189
189
  requirements:
190
- - - '>='
190
+ - - ">="
191
191
  - !ruby/object:Gem::Version
192
192
  version: '0'
193
193
  - !ruby/object:Gem::Dependency
194
194
  name: protected_attributes
195
195
  requirement: !ruby/object:Gem::Requirement
196
196
  requirements:
197
- - - ~>
197
+ - - "~>"
198
198
  - !ruby/object:Gem::Version
199
199
  version: 1.0.5
200
200
  type: :development
201
201
  prerelease: false
202
202
  version_requirements: !ruby/object:Gem::Requirement
203
203
  requirements:
204
- - - ~>
204
+ - - "~>"
205
205
  - !ruby/object:Gem::Version
206
206
  version: 1.0.5
207
207
  description: Model serialization, paging, side-loading and filtering
@@ -211,10 +211,10 @@ executables: []
211
211
  extensions: []
212
212
  extra_rdoc_files: []
213
213
  files:
214
- - .gitignore
215
- - .rspec
216
- - .ruby-version
217
- - .travis.yml
214
+ - ".gitignore"
215
+ - ".rspec"
216
+ - ".ruby-version"
217
+ - ".travis.yml"
218
218
  - Gemfile
219
219
  - Guardfile
220
220
  - LICENSE
@@ -265,12 +265,12 @@ require_paths:
265
265
  - lib
266
266
  required_ruby_version: !ruby/object:Gem::Requirement
267
267
  requirements:
268
- - - '>='
268
+ - - ">="
269
269
  - !ruby/object:Gem::Version
270
270
  version: '0'
271
271
  required_rubygems_version: !ruby/object:Gem::Requirement
272
272
  requirements:
273
- - - '>='
273
+ - - ">="
274
274
  - !ruby/object:Gem::Version
275
275
  version: '0'
276
276
  requirements: []
@@ -299,3 +299,4 @@ test_files:
299
299
  - spec/serializable/sortable_spec.rb
300
300
  - spec/spec_helper.rb
301
301
  - spec/support/factory.rb
302
+ has_rdoc: