Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gastonmorixe/rails-fields
📝 Declarative fields for Rails ActiveRecord models 🚀 automatic migrations and 🦄 graphql types generation!
https://github.com/gastonmorixe/rails-fields
activerecord graphql rails ruby
Last synced: 7 days ago
JSON representation
📝 Declarative fields for Rails ActiveRecord models 🚀 automatic migrations and 🦄 graphql types generation!
- Host: GitHub
- URL: https://github.com/gastonmorixe/rails-fields
- Owner: gastonmorixe
- Created: 2023-08-27T03:39:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-11T18:36:16.000Z (6 months ago)
- Last Synced: 2024-10-22T23:45:14.615Z (16 days ago)
- Topics: activerecord, graphql, rails, ruby
- Language: Ruby
- Homepage: https://rails-fields.dev
- Size: 417 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[![Gem Version](https://badge.fury.io/rb/rails-fields.svg?v=320)](https://badge.fury.io/rb/rails-fields)
# Rails Fields
Enforce field types and attributes for ActiveRecord models in Ruby on Rails applications.
- 🚀 Automatic ActiveRecord **Migrations** generation
- 🦄 Automatic [GraphQL types](https://graphql-ruby.org/type_definitions/objects.html) generation
- 📝 Explicit **declarative** model attributes annotation
- 💪🏻 Enforcement of fields declaration with real db columns
- 📜 Automatic YARD model documentation## Description
The `rails-fields` gem provides robust field type enforcement for ActiveRecord models in Ruby on Rails applications. It includes utility methods for type validation, logging, and field mappings between GraphQL and ActiveRecord types. Custom error classes provide clear diagnostics for field-related issues, making it easier to maintain consistent data models.## Usage
In your ActiveRecord models:
```ruby
class User < ApplicationRecord
field :id, :integer
field :created_at, :datetime
field :updated_at, :datetimefield :first_name, :string
field :country, :string
field :welcome, :stringhas_many :todos
def welcome
"Welcome #{first_name}!"
end
end
```Autogenerate GraphQL types using `#gql_type` class method:
```ruby
module Types
class QueryType < Types::BaseObject
field :users, [User.gql_type], null: true
def users
User.all
end
end
end
```## Installation
Add this line to your application's Gemfile:
```ruby
gem 'rails-fields'
```If you want to have graphql types generated for your models, add this line to your application's Gemfile:
```ruby
gem 'graphql'
```*Don't forget to install it `$ ./bin/rails generate graphql:install`*
And then execute:
```bash
$ bundle install
```## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Author
Gaston Morixe 2023 - [email protected]
[rails-fields.dev](https://rails-fields.dev/?gh)