Skip to content

Commit 9b45988

Browse files
authored
Merge pull request #236 from michal-kazmierczak/master
Fix conditional rendering of an extra_attribute (:readable)
2 parents 873192c + aa87de7 commit 9b45988

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

spec/serialization_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,61 @@ def my_method
654654
expect(attributes["salary"]).to eq(200)
655655
end
656656
end
657+
658+
context "when an extra_attribute is conditionally readable" do
659+
before do
660+
PORO::Employee.create(salary: "40")
661+
resource.class_eval do
662+
attribute :first_name, :string
663+
extra_attribute :salary, :string, readable: :admin?
664+
665+
def admin?
666+
!!context.admin
667+
end
668+
end
669+
end
670+
671+
context "and the guard passes" do
672+
around do |e|
673+
Graphiti.with_context(OpenStruct.new(admin: true)) do
674+
e.run
675+
end
676+
end
677+
678+
it "is serialized" do
679+
render
680+
expect(json["data"][0]["attributes"]["salary"]).to eq("40")
681+
end
682+
end
683+
684+
context "and the guard fails" do
685+
around do |e|
686+
Graphiti.with_context(OpenStruct.new(admin: false)) do
687+
e.run
688+
end
689+
end
690+
691+
it "is not serialized" do
692+
render
693+
expect(json["data"][0]["attributes"]).to_not have_key("salary")
694+
end
695+
end
696+
697+
context "and the guard accepts an argument" do
698+
before do
699+
resource.class_eval do
700+
def admin?(object)
701+
object.is_a?(PORO::Employee)
702+
end
703+
end
704+
end
705+
706+
it "is passed the model instance as argument" do
707+
render
708+
expect(json["data"][0]["attributes"]["salary"]).to eq("40")
709+
end
710+
end
711+
end
657712
end
658713
end
659714

0 commit comments

Comments
 (0)