- Each tenant can define custom fields of different types (text, number, single_select, multiple_select).
- Users belong to a tenant and can set values for custom fields defined by their tenant.
- The API endpoint for updating custom fields for a user is
/users/:id
and expects acustom_fields
parameter with field IDs and values.
- Clone the repository.
- Run
bundle install
to install dependencies. - Run
rails db:migrate
to create the database schema. - Run
rails server
to start the application.
PATCH /users/:id
custom_fields
: Hash of custom field IDs and values.
curl -X PATCH "http://localhost:3000/users/1" \
-H "Content-Type: application/json" \
-d '{
"custom_fields": {
"1": "Ivan Zaitsev",
"2": "1234567890"
}
}'
To make custom fields usable by other entities, you can generalize the custom fields association. This might involve creating a polymorphic association for the CustomFieldValue
model. This would allow you to associate custom field values with different models, not just users.
app/models/custom_field_value.rb:
class CustomFieldValue < ApplicationRecord
belongs_to :customizable, polymorphic: true
belongs_to :custom_field
validates :value, presence: true
end
And then update associations:
class User < ApplicationRecord
belongs_to :tenant
has_many :custom_field_values, as: :customizable
# ... existing methods
end
class CustomField < ApplicationRecord
belongs_to :tenant
has_many :custom_field_values, as: :customizable
# ... existing methods
end