Skip to content

Commit ac37c85

Browse files
authored
Allow symbol variable and type aliases (#44)
* Treat symbols like strings for serializing variables * Allow for a document_type
1 parent e8d6434 commit ac37c85

6 files changed

Lines changed: 48 additions & 2 deletions

File tree

lib/sanity/helpers/type_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

lib/sanity/http/where.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

lib/sanity/queryable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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",

test/sanity/helpers/type_helper_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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
1941
end

test/sanity/http/where_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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

test/sanity/resources/document_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,23 @@
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
2544
end

0 commit comments

Comments
 (0)