|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | describe Grape::Entity do |
4 | | - let(:fresh_class){ Class.new(Grape::Entity) } |
| 4 | + let(:fresh_class) { Class.new(Grape::Entity) } |
5 | 5 |
|
6 | 6 | context 'class methods' do |
7 | | - subject{ fresh_class } |
| 7 | + subject { fresh_class } |
8 | 8 |
|
9 | 9 | describe '.expose' do |
10 | 10 | context 'multiple attributes' do |
|
238 | 238 |
|
239 | 239 | let(:model){ mock(attributes) } |
240 | 240 |
|
241 | | - let(:attributes){ { |
| 241 | + let(:attributes) { { |
242 | 242 | :name => 'Bob Bobson', |
243 | 243 | |
244 | 244 | :birthday => Time.gm(2012, 2, 27), |
|
286 | 286 | res.should_not have_key :non_existant_attribute2 |
287 | 287 | end |
288 | 288 |
|
289 | | - it 'should serialize embedded objects which respond to #serializable_hash' do |
290 | | - fresh_class.expose :name, :embedded |
291 | | - presenter = fresh_class.new(EmbeddedExampleWithOne.new) |
292 | | - presenter.serializable_hash.should == {:name => "abc", :embedded => {:abc => "def"}} |
293 | | - end |
| 289 | + context "#serializable_hash" do |
| 290 | + |
| 291 | + module EntitySpec |
| 292 | + class EmbeddedExample |
| 293 | + def serializable_hash(opts = {}) |
| 294 | + { :abc => 'def' } |
| 295 | + end |
| 296 | + end |
| 297 | + class EmbeddedExampleWithMany |
| 298 | + def name |
| 299 | + "abc" |
| 300 | + end |
| 301 | + def embedded |
| 302 | + [ EmbeddedExample.new, EmbeddedExample.new ] |
| 303 | + end |
| 304 | + end |
| 305 | + class EmbeddedExampleWithOne |
| 306 | + def name |
| 307 | + "abc" |
| 308 | + end |
| 309 | + def embedded |
| 310 | + EmbeddedExample.new |
| 311 | + end |
| 312 | + end |
| 313 | + end |
| 314 | + |
| 315 | + it 'should serialize embedded objects which respond to #serializable_hash' do |
| 316 | + fresh_class.expose :name, :embedded |
| 317 | + presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithOne.new) |
| 318 | + presenter.serializable_hash.should == {:name => "abc", :embedded => {:abc => "def"}} |
| 319 | + end |
294 | 320 |
|
295 | | - it 'should serialize embedded arrays of objects which respond to #serializable_hash' do |
296 | | - fresh_class.expose :name, :embedded |
297 | | - presenter = fresh_class.new(EmbeddedExampleWithMany.new) |
298 | | - presenter.serializable_hash.should == {:name => "abc", :embedded => [{:abc => "def"}, {:abc => "def"}]} |
| 321 | + it 'should serialize embedded arrays of objects which respond to #serializable_hash' do |
| 322 | + fresh_class.expose :name, :embedded |
| 323 | + presenter = fresh_class.new(EntitySpec::EmbeddedExampleWithMany.new) |
| 324 | + presenter.serializable_hash.should == {:name => "abc", :embedded => [{:abc => "def"}, {:abc => "def"}]} |
| 325 | + end |
| 326 | + |
299 | 327 | end |
| 328 | + |
300 | 329 | end |
301 | 330 |
|
302 | 331 | describe '#value_for' do |
@@ -330,12 +359,21 @@ def timestamp(date) |
330 | 359 | end |
331 | 360 |
|
332 | 361 | it 'should disable root key name for child representations' do |
| 362 | + |
| 363 | + module EntitySpec |
| 364 | + class FriendEntity < Grape::Entity |
| 365 | + root 'friends', 'friend' |
| 366 | + expose :name, :email |
| 367 | + end |
| 368 | + end |
| 369 | + |
333 | 370 | fresh_class.class_eval do |
334 | | - expose :friends, :using => FriendEntity |
| 371 | + expose :friends, :using => EntitySpec::FriendEntity |
335 | 372 | end |
| 373 | + |
336 | 374 | rep = subject.send(:value_for, :friends) |
337 | 375 | rep.should be_kind_of(Array) |
338 | | - rep.reject{|r| r.is_a?(FriendEntity)}.should be_empty |
| 376 | + rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty |
339 | 377 | rep.first.serializable_hash[:name].should == 'Friend 1' |
340 | 378 | rep.last.serializable_hash[:name].should == 'Friend 2' |
341 | 379 | end |
|
0 commit comments