Skip to content

Commit d76d098

Browse files
committed
chore: _type as default anchor schema name config
1 parent f49ce4d commit d76d098

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

lib/anchor/concerns/type_inferable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def method_added(method_name)
2626

2727
# inspiration from https://github.com/rmosolgo/graphql-ruby/blob/eda9b3d62b9e507787e590f0f179ec9d6956255a/lib/graphql/schema/member/base_dsl_methods.rb?plain=1#L102
2828
def anchor_default_schema_name
29+
# https://github.com/cerebris/jsonapi-resources/blob/d3c094b46a38650e583f40adc86474827b606fc7/lib/jsonapi/resource_common.rb?plain=1#L506
30+
# https://github.com/cerebris/jsonapi-resources/blob/d3c094b46a38650e583f40adc86474827b606fc7/lib/jsonapi/resource_common.rb?plain=1#L564
31+
return _type.to_s.classify if Anchor.config.use_type_as_schema_name
32+
2933
s_name = name.split("::").last
3034
s_name.end_with?("Resource") ? s_name.sub(/Resource\Z/, "") : s_name
3135
end

lib/anchor/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Config
55
:use_active_record_presence,
66
:use_active_record_comment,
77
:infer_nullable_relationships_as_optional,
8-
:empty_relationship_type
8+
:empty_relationship_type,
9+
:use_type_as_schema_name
910

1011
def initialize
1112
@ar_column_to_type = nil
@@ -14,6 +15,7 @@ def initialize
1415
@use_active_record_comment = nil
1516
@infer_nullable_relationships_as_optional = nil
1617
@empty_relationship_type = nil
18+
@use_type_as_schema_name = nil
1719
end
1820
end
1921
end

spec/anchor/concerns/type_inferable_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,43 @@ def one = 1
4343
end
4444

4545
context "without schema name" do
46+
around { |example| stub_jsonapi_resource_subclass("TestResources", &example) }
47+
4648
it "uses the default schema name" do
4749
UserResource.class_eval do
4850
include Anchor::TypeInferable
4951
end
5052

5153
expect(anchor_schema_name).to eql("User")
5254
end
55+
56+
context "without use_type_as_schema_name config enabled" do
57+
before do
58+
allow(Anchor.config).to receive(:use_type_as_schema_name).and_return(false)
59+
end
60+
61+
it "uses the default schema name" do
62+
TestResources.class_eval do
63+
include Anchor::TypeInferable
64+
end
65+
66+
expect(TestResources.anchor_schema_name).to eql("TestResources")
67+
end
68+
end
69+
70+
context "with use_type_as_schema_name config enabled" do
71+
before do
72+
allow(Anchor.config).to receive(:use_type_as_schema_name).and_return(true)
73+
end
74+
75+
it "uses classified _type" do
76+
TestResources.class_eval do
77+
include Anchor::TypeInferable
78+
end
79+
80+
expect(TestResources.anchor_schema_name).to eql("TestResource")
81+
end
82+
end
5383
end
5484
end
5585

spec/support/stub.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# rubocop:disable RSpec/RemoveConst, Security/Eval
22
def stub_jsonapi_resource_subclass(name, &example)
33
previously_defined = Object.const_defined?(name)
4-
previous_klass = name.constantize
54

6-
Object.send(:remove_const, name) if previously_defined
5+
if previously_defined
6+
previous_klass = name.constantize
7+
Object.send(:remove_const, name)
8+
end
79

810
klass_string = <<~EVAL
911
class #{name} < JSONAPI::Resource

0 commit comments

Comments
 (0)