docbrown/app/validators/profile_validator.rb
Michael Kohl 8f5cfa2c0f
Drop profile columns from user (#10707)
* Prepare to drop profile columns from user

* Update code and factory

* Also remove unused constant

* Move validation from user to profile

* Remove Profiles::ExtractData service object

* Add more comments

* Simplify sameAs attribute generation

* Obey me machine, I am your master

* Fix condition order in guard clause

* Temporarily disable callback

* Fix specs

* Reduce usage of Profile#refresh_attributes!

* Remove leftover comment

* Handle social media links differently

* More spec fixes

* Fix specs for admin profile fields controller

* Fix specs after merge

* Fix remaining specs

* Update user show request spec

* Add comment for follow_hiring_tag

* Only save profile when user is valid

* Fix seeds.rb for profile fields

* Switch from before_save to after_save

* Undo unrelated formattin change

* Update spec/fixtures/files/profile_fields.csv

Co-authored-by: Molly Struve <mollylbs@gmail.com>

* Remove data update script and spec

* Fix spec

* Fix typo in comment

* Fix typo in comment

* Move article resave logic to service object

* Move profile field creation to before(:suite)

* Refactor error handling in Profiles::Update

* Fix Profiles::Update specs and refactor

* Temporarily disable spec

* Add ProfileValidator

* Clean up

* Move DB ready check into app/lib

* Refresh attributes after importing from CSV

* Fix specs

* Remove unused file

* A girl has no name. A profile neither.

* Fix specs

* Add responds_to? check

* Spec fix

* Add name to user fields in profile settings page

Co-authored-by: Molly Struve <mollylbs@gmail.com>
2020-12-03 08:14:38 +07:00

63 lines
2 KiB
Ruby

class ProfileValidator < ActiveModel::Validator
SUMMARY_ATTRIBUTE = "summary".freeze
MAX_SUMMARY_LENGTH = 200
MAX_TEXT_AREA_LENGTH = 200
MAX_TEXT_FIELD_LENGTH = 100
HEX_COLOR_REGEXP = /^#?(?:\h{6}|\h{3})$/.freeze
ERRORS = {
color_field: "is not a valid hex color",
text_area: "is too long (maximum: #{MAX_TEXT_AREA_LENGTH})",
text_field: "is too long (maximum: #{MAX_TEXT_FIELD_LENGTH})"
}.with_indifferent_access.freeze
def validate(record)
# NOTE: @citizen428 The summary is a base profile field, which we add to all
# new Forem instances, so it should be safe to validate. The method itself
# also guards against the field's absence.
record.errors.add(:summary, "is too long") if summary_too_long?(record)
ProfileField.all.each do |field|
attribute = field.attribute_name
next if attribute == SUMMARY_ATTRIBUTE # validated above
next unless record.respond_to?(attribute) # avoid caching issues
next if __send__("#{field.input_type}_valid?", record, attribute)
record.errors.add(attribute, ERRORS[field.input_type])
end
end
private
def summary_too_long?(record)
return unless ProfileField.exists?(attribute_name: SUMMARY_ATTRIBUTE)
return if record.summary.blank?
# Grandfather in people who had a too long summary before
previous_summary = record.data_was[SUMMARY_ATTRIBUTE]
return if previous_summary && previous_summary.size > MAX_SUMMARY_LENGTH
record.summary.size > MAX_SUMMARY_LENGTH
end
def check_box_valid?(_record, _attribute)
true # checkboxes are always valid
end
def color_field_valid?(record, attribute)
hex_value = record.public_send(attribute)
hex_value.nil? || hex_value.match?(HEX_COLOR_REGEXP)
end
def text_area_valid?(record, attribute)
text = record.public_send(attribute)
text.nil? || text.size <= MAX_TEXT_AREA_LENGTH
end
def text_field_valid?(record, attribute)
text = record.public_send(attribute)
text.nil? || text.size <= MAX_TEXT_FIELD_LENGTH
end
end