Skip to content

Commit

Permalink
feat: configurable default description
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkhan committed Aug 19, 2024
1 parent 36003c2 commit 2d19382
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ If the `description` key is present in the options to `.attribute` or
`.relationship`, it will be used in the provided TypeScript Schema generator as
a comment for the property. The comment should show up in the LSP hover info.

By default, the `description` is inferred from the ActiveRecord column comment
if it exists. Examples of both in
With `Anchor.config.use_active_record_presence = true`, a default `description`
will be inferred from the ActiveRecord column comment if it exists. Examples of
both in
[spec/example/app/resources/exhaustive_resource.rb](./spec/example/app/resources/exhaustive_resource.rb)
and its resulting [TypeScript schema](./spec/example/test/files/schema.ts).

Expand Down Expand Up @@ -132,6 +133,7 @@ See `Anchor::TypeScript::Resource`, `Anchor::TypeScript::Serializer`, and
| ------------------------------------------ | ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `field_case` | `:camel \| :snake \| :kebab` | Case format for `attributes` and `relationships` properties. |
| `ar_column_to_type` | `Proc` | `ActiveRecord::Base.columns_hash[attribute]` to `Anchor::Type` |
| `use_active_record_comment` | `Boolean` | whether to use ActiveRecord comments as default value of `description` option |
| `use_active_record_presence` | `Boolean` | check presence of unconditional `validates :attribute, presence: true` to infer database nullable attribute as non-null |
| `infer_nullable_relationships_as_optional` | `Boolean` | `true` infers nullable relationships as optional. e.g. in TypeScript, `true` infers `{ relation?: Relation }` over `{ relation: Maybe<Relation> }` |

Expand Down
2 changes: 2 additions & 0 deletions lib/anchor/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ class Config
attr_accessor :ar_column_to_type,
:field_case,
:use_active_record_presence,
:use_active_record_comment,
:infer_nullable_relationships_as_optional

def initialize
@ar_column_to_type = nil
@field_case = nil
@use_active_record_presence = nil
@use_active_record_comment = nil
@infer_nullable_relationships_as_optional = nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/anchor/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def anchor_attributes_properties(included_fields:)
column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
if column
type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
description ||= column.comment
description ||= column.comment if Anchor.config.use_active_record_comment
check_presence = type.is_a?(Anchor::Types::Maybe) && Anchor.config.use_active_record_presence
if check_presence && _model_class.validators_on(model_method).any? do |v|
v.is_a?(ActiveRecord::Validations::PresenceValidator)
Expand Down
1 change: 1 addition & 0 deletions spec/example/config/initializers/anchor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Anchor
configure do |c|
c.field_case = :camel
c.use_active_record_comment = true
c.use_active_record_presence = true
c.infer_nullable_relationships_as_optional = true

Expand Down

0 comments on commit 2d19382

Please sign in to comment.