File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ module TypeHelper
55 def self . default_type ( klass )
66 return nil if klass == Sanity ::Document
77
8- type = klass . to_s
8+ type = ( klass . try ( :document_type ) || klass ) . to_s
9+
910 type [ 0 ] . downcase + type [ 1 ..]
1011 end
1112 end
Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ def request_body
5959
6060 def serialize_variable_value ( value )
6161 case value
62- when String
62+ when String , Symbol
6363 "\" #{ value } \" "
6464 when Array , Hash
6565 value . to_json
Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ def included(base)
2828 module ClassMethods
2929 DEFAULT_KLASS_QUERIES = %i[ find where ] . freeze
3030
31+ attr_accessor :document_type
32+
3133 # See https://www.sanity.io/docs/http-query & https://www.sanity.io/docs/http-doc
3234 QUERY_ENDPOINTS = {
3335 find : "data/doc" ,
Original file line number Diff line number Diff line change @@ -15,5 +15,27 @@ class Foobar; end
1515 subject { Foobar }
1616 it { assert_equal "foobar" , Sanity ::TypeHelper . default_type ( subject ) }
1717 end
18+
19+ context "Sanity::Document" do
20+ context "without a custom document_type" do
21+ subject do
22+ Quux = Class . new ( Sanity ::Document )
23+ Quux
24+ end
25+
26+ it { assert_equal "quux" , Sanity ::TypeHelper . default_type ( subject ) }
27+ end
28+
29+ context "with custom document_type" do
30+ subject do
31+ BarBaz = Class . new ( Sanity ::Document ) do
32+ self . document_type = "custom_type"
33+ end
34+ BarBaz
35+ end
36+
37+ it { assert_equal "custom_type" , Sanity ::TypeHelper . default_type ( subject ) }
38+ end
39+ end
1840 end
1941end
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ def self.where_api_endpoint
2424 array_of_strings : [ "foo" , "bar" ] ,
2525 hash_var : { "foo" => "bar" } ,
2626 number_var : 42 ,
27+ symbol_var : :symbolized_variable ,
2728 boolean_var : true
2829 }
2930 end
@@ -38,6 +39,7 @@ def self.where_api_endpoint
3839 assert_includes decoded_query , "$hash_var={\" foo\" :\" bar\" }"
3940 assert_includes decoded_query , "$number_var=42"
4041 assert_includes decoded_query , "$boolean_var=true"
42+ assert_includes decoded_query , "$symbol_var=\" symbolized_variable\" "
4143 end
4244 end
4345 end
Original file line number Diff line number Diff line change 2222
2323 it { assert_respond_to klass , :find }
2424 it { assert_respond_to klass , :where }
25+
26+ describe ".document_type" do
27+ it "allows setting a custom type name" do
28+ klass . document_type = "custom_type"
29+ assert_equal "custom_type" , klass . document_type
30+ end
31+
32+ it "returns nil when document_type is not set" do
33+ new_klass = Class . new ( Sanity ::Document )
34+ assert_nil new_klass . document_type
35+ end
36+
37+ it "allows setting document_type to nil" do
38+ klass . document_type = "custom_type"
39+ assert_equal "custom_type" , klass . document_type
40+ klass . document_type = nil
41+ assert_nil klass . document_type
42+ end
43+ end
2544end
You can’t perform that action at this time.
0 commit comments