|
39 | 39 |
|
40 | 40 | it 'sets the :proc option in the exposure options' do
|
41 | 41 | block = lambda{|_| true }
|
42 |
| - subject.expose :name, &block |
43 |
| - subject.exposures[:name][:proc].should == block |
| 42 | + subject.expose :name, :using => 'Awesome', &block |
| 43 | + subject.exposures[:name].should == { :proc => block, :using => 'Awesome' } |
44 | 44 | end
|
45 | 45 | end
|
46 | 46 |
|
|
358 | 358 | end
|
359 | 359 | end
|
360 | 360 |
|
| 361 | + it "exposes attributes that don't exist on the object only when they are generated by a block with options" do |
| 362 | + module EntitySpec |
| 363 | + class TestEntity < Grape::Entity |
| 364 | + end |
| 365 | + end |
| 366 | + |
| 367 | + fresh_class.expose :nonexistent_attribute, using: EntitySpec::TestEntity do |model, _| |
| 368 | + "well, I do exist after all" |
| 369 | + end |
| 370 | + res = fresh_class.new(model).serializable_hash |
| 371 | + res.should have_key :nonexistent_attribute |
| 372 | + end |
| 373 | + |
| 374 | + it "does not expose attributes that are generated by a block but have not passed criteria" do |
| 375 | + fresh_class.expose :nonexistent_attribute, :proc => lambda {|model, _| |
| 376 | + "I exist, but it is not yet my time to shine" |
| 377 | + }, :if => lambda { |model, _| false } |
| 378 | + res = fresh_class.new(model).serializable_hash |
| 379 | + res.should_not have_key :nonexistent_attribute |
| 380 | + end |
| 381 | + |
361 | 382 | context '#serializable_hash' do
|
362 | 383 |
|
363 | 384 | module EntitySpec
|
@@ -451,6 +472,63 @@ class FriendEntity < Grape::Entity
|
451 | 472 | rep.last.serializable_hash[:name].should == 'Friend 2'
|
452 | 473 | end
|
453 | 474 |
|
| 475 | + it "passes through the proc which returns an array of objects with custom options(:using)" do |
| 476 | + module EntitySpec |
| 477 | + class FriendEntity < Grape::Entity |
| 478 | + root 'friends', 'friend' |
| 479 | + expose :name, :email |
| 480 | + end |
| 481 | + end |
| 482 | + |
| 483 | + fresh_class.class_eval do |
| 484 | + expose :custom_friends, :using => EntitySpec::FriendEntity do |user, options| |
| 485 | + user.friends |
| 486 | + end |
| 487 | + end |
| 488 | + |
| 489 | + rep = subject.send(:value_for, :custom_friends) |
| 490 | + rep.should be_kind_of Array |
| 491 | + rep.reject{|r| r.is_a?(EntitySpec::FriendEntity)}.should be_empty |
| 492 | + rep.first.serializable_hash.should == { :name => 'Friend 1', :email => '[email protected]'} |
| 493 | + rep.last.serializable_hash.should == { :name => 'Friend 2', :email => '[email protected]'} |
| 494 | + end |
| 495 | + it "passes through the proc which returns single object with custom options(:using)" do |
| 496 | + module EntitySpec |
| 497 | + class FriendEntity < Grape::Entity |
| 498 | + root 'friends', 'friend' |
| 499 | + expose :name, :email |
| 500 | + end |
| 501 | + end |
| 502 | + |
| 503 | + fresh_class.class_eval do |
| 504 | + expose :first_friend, :using => EntitySpec::FriendEntity do |user, options| |
| 505 | + user.friends.first |
| 506 | + end |
| 507 | + end |
| 508 | + |
| 509 | + rep = subject.send(:value_for, :first_friend) |
| 510 | + rep.should be_kind_of EntitySpec::FriendEntity |
| 511 | + rep.serializable_hash.should == { :name => 'Friend 1', :email => '[email protected]'} |
| 512 | + end |
| 513 | + it "passes through the proc which returns empty with custom options(:using)" do |
| 514 | + module EntitySpec |
| 515 | + class FriendEntity < Grape::Entity |
| 516 | + root 'friends', 'friend' |
| 517 | + expose :name, :email |
| 518 | + end |
| 519 | + end |
| 520 | + |
| 521 | + fresh_class.class_eval do |
| 522 | + expose :first_friend, :using => EntitySpec::FriendEntity do |user, options| |
| 523 | + |
| 524 | + end |
| 525 | + end |
| 526 | + |
| 527 | + rep = subject.send(:value_for, :first_friend) |
| 528 | + rep.should be_kind_of EntitySpec::FriendEntity |
| 529 | + rep.serializable_hash.should be_nil |
| 530 | + end |
| 531 | + |
454 | 532 | it 'passes through custom options' do
|
455 | 533 | module EntitySpec
|
456 | 534 | class FriendEntity < Grape::Entity
|
|
0 commit comments