From 15fae701b58f8230b93941fa300525e8c23b260a Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Thu, 9 Nov 2023 10:53:58 +0300 Subject: [PATCH] Updates method names and generate_attributes_schema's logic --- lib/schemable/attribute_schema_generator.rb | 16 +++++++++++----- sig/schemable/attribute_schema_generator.rbs | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/schemable/attribute_schema_generator.rb b/lib/schemable/attribute_schema_generator.rb index 4e34447..2773073 100644 --- a/lib/schemable/attribute_schema_generator.rb +++ b/lib/schemable/attribute_schema_generator.rb @@ -11,21 +11,27 @@ module Schemable end # Generate the JSON schema for attributes - def generate_attribute_schema + def generate_attributes_schema schema = { type: :object, - properties: {} + properties: attributes.index_with do |attr| + generate_attribute_schema(attr) + end } - @model.attribute_names.each do |attribute| - schema[:properties][attribute.to_sym] = generate_property_schema(attribute) + # modify the schema to include additional response relations + schema = @schema_modifier.add_properties(schema, @model_definition.additional_response_attributes, 'properties') + + # modify the schema to exclude response relations + @model_definition.excluded_response_attributes.each do |key| + schema = @schema_modifier.delete_properties(schema, "properties.#{key}") end schema end # Generate the JSON schema for a specific attribute - def generate_property_schema(attribute) + def generate_attribute_schema(attribute) if @configuration.orm == :mongoid # Get the column hash for the attribute attribute_hash = @model.fields[attribute.to_s] diff --git a/sig/schemable/attribute_schema_generator.rbs b/sig/schemable/attribute_schema_generator.rbs index a1248b4..417fb6c 100644 --- a/sig/schemable/attribute_schema_generator.rbs +++ b/sig/schemable/attribute_schema_generator.rbs @@ -7,8 +7,8 @@ module Schemable attr_accessor schema_modifier: SchemaModifier def initialize: (Definition, Configuration) -> void - def generate_attribute_schema: -> Hash[Symbol, any] + def generate_attributes_schema: -> (Hash[Symbol, any] | Array[any]) - def generate_property_schema: (Symbol) -> Hash[Symbol, any] + def generate_attribute_schema: (Symbol) -> Hash[Symbol, any] end end