Skip to content

Commit 9550fec

Browse files
authored
Merge pull request #157 from zeisler/attribute_disable_schema
Allow attribute to be used, but not to show up in the schema.
2 parents b92d662 + c42e4ba commit 9550fec

File tree

7 files changed

+72
-7
lines changed

7 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Unreleased
2+
3+
Features:
4+
5+
- [157](https://github.com/graphiti-api/graphiti/pull/157) Using attribute option schema: false.
6+
This option is default true and is not effected by only and except options. (@zeisler)
7+
18
## 1.1.0
29

310
Features:

lib/graphiti/resource/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class << self
7878
:attributes_writable_by_default,
7979
:attributes_sortable_by_default,
8080
:attributes_filterable_by_default,
81+
:attributes_schema_by_default,
8182
:relationships_readable_by_default,
8283
:relationships_writable_by_default
8384

@@ -97,6 +98,7 @@ def self.inherited(klass)
9798
default(klass, :attributes_writable_by_default, true)
9899
default(klass, :attributes_sortable_by_default, true)
99100
default(klass, :attributes_filterable_by_default, true)
101+
default(klass, :attributes_schema_by_default, true)
100102
default(klass, :relationships_readable_by_default, true)
101103
default(klass, :relationships_writable_by_default, true)
102104

lib/graphiti/resource/dsl.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def attribute(name, type, options = {}, &blk)
101101
attribute_option(options, :writable)
102102
attribute_option(options, :sortable)
103103
attribute_option(options, :filterable)
104+
attribute_option(options, :schema, true)
104105
options[:type] = type
105106
options[:proc] = blk
106107
config[:attributes][name] = options
@@ -151,11 +152,11 @@ def apply_extra_attributes_to_serializer
151152
Util::SerializerAttributes.new(self, extra_attributes, true).apply
152153
end
153154

154-
def attribute_option(options, name)
155+
def attribute_option(options, name, exclusive = false)
155156
if options[name] != false
156-
default = if (only = options[:only])
157+
default = if (only = options[:only]) && !exclusive
157158
Array(only).include?(name) ? true : false
158-
elsif (except = options[:except])
159+
elsif (except = options[:except]) && !exclusive
159160
Array(except).include?(name) ? false : true
160161
else
161162
send(:"attributes_#{name}_by_default")

lib/graphiti/schema.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def generate_resources
131131
def attributes(resource)
132132
{}.tap do |attrs|
133133
resource.attributes.each_pair do |name, config|
134-
if config.values_at(:readable, :writable).any?
134+
if config.values_at(:readable, :writable).any? && config[:schema]
135135
attrs[name] = {
136136
type: config[:type].to_s,
137137
readable: flag(config[:readable]),
@@ -166,6 +166,8 @@ def flag(value)
166166
def sorts(resource)
167167
{}.tap do |s|
168168
resource.sorts.each_pair do |name, sort|
169+
next unless resource.attributes[name][:schema]
170+
169171
config = {}
170172
config[:only] = sort[:only] if sort[:only]
171173
attr = resource.attributes[name]
@@ -180,6 +182,8 @@ def sorts(resource)
180182
def filters(resource)
181183
{}.tap do |f|
182184
resource.filters.each_pair do |name, filter|
185+
next unless resource.attributes[name][:schema]
186+
183187
config = {
184188
type: filter[:type].to_s,
185189
operators: filter[:operators].keys.map(&:to_s),

spec/resource_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
expect(klass.attributes_writable_by_default).to eq(true)
2222
expect(klass.attributes_sortable_by_default).to eq(true)
2323
expect(klass.attributes_filterable_by_default).to eq(true)
24+
expect(klass.attributes_schema_by_default).to eq(true)
2425
expect(klass.relationships_readable_by_default).to eq(true)
2526
expect(klass.relationships_writable_by_default).to eq(true)
2627
end
@@ -63,6 +64,7 @@
6364
expect(klass.attributes_writable_by_default).to eq(true)
6465
expect(klass.attributes_sortable_by_default).to eq(true)
6566
expect(klass.attributes_filterable_by_default).to eq(true)
67+
expect(klass.attributes_schema_by_default).to eq(true)
6668
expect(klass.relationships_readable_by_default).to eq(true)
6769
expect(klass.relationships_writable_by_default).to eq(true)
6870
end
@@ -147,6 +149,7 @@ def self.name
147149
self.attributes_writable_by_default = false
148150
self.attributes_sortable_by_default = false
149151
self.attributes_filterable_by_default = false
152+
self.attributes_schema_by_default = false
150153
self.relationships_readable_by_default = false
151154
self.relationships_writable_by_default = false
152155
end
@@ -160,6 +163,7 @@ def self.name
160163
expect(klass.attributes_writable_by_default).to eq(false)
161164
expect(klass.attributes_sortable_by_default).to eq(false)
162165
expect(klass.attributes_filterable_by_default).to eq(false)
166+
expect(klass.attributes_schema_by_default).to eq(false)
163167
expect(klass.relationships_readable_by_default).to eq(false)
164168
expect(klass.relationships_writable_by_default).to eq(false)
165169
end
@@ -288,6 +292,7 @@ class TestResourceOverrideSerializer < PORO::ApplicationSerializer
288292
self.attributes_writable_by_default = false
289293
self.attributes_sortable_by_default = false
290294
self.attributes_filterable_by_default = false
295+
self.attributes_schema_by_default = false
291296
self.relationships_readable_by_default = false
292297
self.relationships_writable_by_default = false
293298
end
@@ -301,6 +306,7 @@ class TestResourceOverrideSerializer < PORO::ApplicationSerializer
301306
expect(klass2.attributes_writable_by_default).to eq(false)
302307
expect(klass2.attributes_sortable_by_default).to eq(false)
303308
expect(klass2.attributes_filterable_by_default).to eq(false)
309+
expect(klass2.attributes_schema_by_default).to eq(false)
304310
expect(klass2.relationships_readable_by_default).to eq(false)
305311
expect(klass2.relationships_writable_by_default).to eq(false)
306312
end
@@ -564,6 +570,7 @@ def apply_attribute
564570
expect(attribute[:writable]).to eq(true)
565571
expect(attribute[:sortable]).to eq(true)
566572
expect(attribute[:filterable]).to eq(true)
573+
expect(attribute[:schema]).to eq(true)
567574
end
568575

569576
context "when :only passed" do
@@ -578,6 +585,7 @@ def apply_attribute
578585
expect(att[:readable]).to eq(false)
579586
expect(att[:sortable]).to eq(false)
580587
expect(att[:writable]).to eq(false)
588+
expect(att[:schema]).to eq(true)
581589
end
582590
end
583591

@@ -592,6 +600,7 @@ def apply_attribute
592600
expect(att[:readable]).to eq(false)
593601
expect(att[:sortable]).to eq(true)
594602
expect(att[:writable]).to eq(false)
603+
expect(att[:schema]).to eq(true)
595604
end
596605
end
597606

@@ -607,6 +616,7 @@ def apply_attribute
607616
expect(att[:readable]).to eq(false)
608617
expect(att[:sortable]).to eq(false)
609618
expect(att[:writable]).to eq(false)
619+
expect(att[:schema]).to eq(true)
610620
end
611621
end
612622
end
@@ -623,6 +633,7 @@ def apply_attribute
623633
expect(att[:readable]).to eq(true)
624634
expect(att[:sortable]).to eq(true)
625635
expect(att[:writable]).to eq(false)
636+
expect(att[:schema]).to eq(true)
626637
end
627638
end
628639

@@ -637,6 +648,7 @@ def apply_attribute
637648
expect(att[:readable]).to eq(true)
638649
expect(att[:sortable]).to eq(false)
639650
expect(att[:writable]).to eq(false)
651+
expect(att[:schema]).to eq(true)
640652
end
641653
end
642654
end
@@ -652,6 +664,7 @@ def apply_attribute
652664
expect(att[:readable]).to eq(true)
653665
expect(att[:sortable]).to eq(:admin?)
654666
expect(att[:writable]).to eq(false)
667+
expect(att[:schema]).to eq(true)
655668
end
656669
end
657670

@@ -737,6 +750,7 @@ def apply_attribute
737750
self.attributes_writable_by_default = false
738751
self.attributes_sortable_by_default = false
739752
self.attributes_filterable_by_default = false
753+
self.attributes_schema_by_default = false
740754
end
741755
end
742756

@@ -747,6 +761,7 @@ def apply_attribute
747761
expect(attribute[:writable]).to eq(false)
748762
expect(attribute[:sortable]).to eq(false)
749763
expect(attribute[:filterable]).to eq(false)
764+
expect(attribute[:schema]).to eq(false)
750765
end
751766
end
752767

@@ -812,6 +827,17 @@ def self.name
812827
end
813828
end
814829

830+
context "when explicit schema flag" do
831+
before do
832+
klass.attribute :foo, :string, schema: false
833+
end
834+
835+
it "overrides the default" do
836+
attribute = klass.config[:attributes][:foo]
837+
expect(attribute[:schema]).to eq(false)
838+
end
839+
end
840+
815841
context "when readable" do
816842
let(:serializer) do
817843
Class.new(Graphiti::Serializer)

spec/schema_diff_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ def self.name
240240
it { is_expected.to eq([]) }
241241
end
242242

243+
context "when attribute goes schema true to false" do
244+
before do
245+
resource_b.attribute :first_name, :string, schema: false
246+
end
247+
248+
it "returns error" do
249+
expect(diff).to eq([
250+
"SchemaDiff::EmployeeResource: attribute :first_name was removed.",
251+
"SchemaDiff::EmployeeResource: sort :first_name was removed.",
252+
"SchemaDiff::EmployeeResource: filter :first_name was removed."
253+
])
254+
end
255+
end
256+
243257
context "when attribute is removed" do
244258
before do
245259
resource_b

spec/schema_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def self.name
185185
self.description = "An employee of the organization"
186186

187187
attribute :first_name, :string, description: "The employee's first name"
188-
188+
attribute :hidden_attribute, :string, schema: false
189189
extra_attribute :net_sales, :float, description: "The total value of the employee's sales"
190190

191191
filter :title, :string do
@@ -271,8 +271,6 @@ def self.name
271271
end
272272
end
273273

274-
context
275-
276274
context "when attribute is not readable" do
277275
before do
278276
employee_resource.class_eval do
@@ -490,6 +488,19 @@ def self.name
490488
end
491489
end
492490

491+
context "when attribute changes to schema true" do
492+
before do
493+
employee_resource.class_eval do
494+
attribute :hidden_attribute, :string, schema: true
495+
end
496+
end
497+
498+
it "is readable" do
499+
expect(schema[:resources][0][:attributes][:hidden_attribute][:readable])
500+
.to eq(true)
501+
end
502+
end
503+
493504
context "when extra attribute is guarded" do
494505
before do
495506
employee_resource.class_eval do

0 commit comments

Comments
 (0)