docbrown/app/models/profile_field.rb
Michael Kohl 2b3242087c
[deploy] Start using profile model (#9724)
* Persist attribute_name for profile fields

* Update fields

* Add temporary Rake task for creating profile fields

* Update specs

* Fix problems after splitting branch
2020-08-17 09:57:50 +07:00

31 lines
675 B
Ruby

class ProfileField < ApplicationRecord
before_create :generate_attribute_name
WORD_REGEX = /\w+/.freeze
# Key names follow the Rails form helpers
enum input_type: {
text_field: 0,
text_area: 1,
check_box: 2,
color_field: 3
}
validates :label, presence: true, uniqueness: { case_sensitive: false }
validates :active, inclusion: { in: [true, false] }
validates :attribute_name, presence: true, on: :update
scope :active, -> { where(active: true) }
def type
return :boolean if check_box?
:string
end
private
def generate_attribute_name
self.attribute_name = label.titleize.scan(WORD_REGEX).join.underscore
end
end