docbrown/app/models/user_optional_field.rb
Yechiel Kalmenson 131d11aa73
[deploy] Add optional fields to user (#7246)
* add optional fields to user

- this provides the back-end implementation for #2365

* fix mistakes in schema.rb

* renamed OptionalField to userOptionalField

* rename field column in user_optional_fields to label

* add uniqueness validation on user_optional_field labels

* scope flag uniqueness to user_id

* add indexing on label in user_optional_fields

* add optional fields to user

- this provides the back-end implementation for #2365

* fix mistakes in schema.rb

* add index on user_id for user_optional_fields

* Remove duplicate model

* Fix schema

* Re-add past indexes

* Remove old index

* Remove another old index

* add `null:false` to columns in optional fields

* add `null: false` to the migration

- Updated test to account for the null constraint

* add null:false to user_id on user_optional_tables

Co-authored-by: rhymes <rhymesete@gmail.com>
Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
2020-04-28 15:42:41 -04:00

13 lines
422 B
Ruby

class UserOptionalField < ApplicationRecord
belongs_to :user
validates :label, presence: true, length: { maximum: 30 }, uniqueness: { scope: :user_id }
validates :value, presence: true, length: { maximum: 128 }
validate :validate_quota
def validate_quota
return unless user
errors.add(:user_id, "already has the maximum allowable optional fields") unless user.user_optional_fields.count < 3
end
end