Skip to content

Instantly share code, notes, and snippets.

@nightpool
Created September 5, 2017 14:21
Show Gist options
  • Save nightpool/3cce6fa7e36794bb15316a4527e83714 to your computer and use it in GitHub Desktop.
Save nightpool/3cce6fa7e36794bb15316a4527e83714 to your computer and use it in GitHub Desktop.
commit d5c69689a1f1a373b477971ba5f750a5c33754b9
Author: nightpool <[email protected]>
Date: Wed Jun 28 20:04:56 2017 -0400
change bio length to 413, only 8 newlines, auto-size bio textarea
diff --git a/app/javascript/packs/public.js b/app/javascript/packs/public.js
index da1f550f..db39f657 100644
--- a/app/javascript/packs/public.js
+++ b/app/javascript/packs/public.js
@@ -37,6 +37,12 @@ function main() {
const datetime = new Date(content.getAttribute('datetime'));
content.textContent = relativeFormat.format(datetime);
});
+
+ if (document.fonts && document.fonts.ready) {
+ document.fonts.ready.then(sizeBioText);
+ } else {
+ sizeBioText();
+ }
});
delegate(document, '.video-player video', 'click', ({ target }) => {
@@ -82,12 +88,7 @@ function main() {
}
});
- delegate(document, '.account_note', 'input', ({ target }) => {
- const noteCounter = document.querySelector('.note-counter');
- if (noteCounter) {
- noteCounter.textContent = 160 - length(target.value);
- }
- });
+ delegate(document, '.account_note', 'input', sizeBioText);
delegate(document, '#account_avatar', 'change', ({ target }) => {
const avatar = document.querySelector('.card.compact .avatar img');
@@ -104,6 +105,20 @@ function main() {
});
}
+function sizeBioText() {
+ const noteCounter = document.querySelector('.note-counter');
+ const bioTextArea = document.querySelector('#account_note');
+
+ if (noteCounter) {
+ noteCounter.textContent = 413 - length(bioTextArea.value);
+ }
+
+ if (bioTextArea) {
+ bioTextArea.style.height = 'auto';
+ bioTextArea.style.height = (bioTextArea.scrollHeight+3) + 'px';
+ }
+}
+
loadPolyfills().then(main).catch(error => {
console.error(error);
});
diff --git a/app/models/account.rb b/app/models/account.rb
index 163bd1c0..99a0d112 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -68,7 +68,8 @@ class Account < ApplicationRecord
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? }
validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? }
validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? }
- validates :note, length: { maximum: 160 }, if: -> { local? && will_save_change_to_note? }
+ validates :note, length: { maximum: 413 }, if: -> { local? && will_save_change_to_note? }
+ validate :note_has_eight_newlines?, if: -> { local? && will_save_change_to_note? }
# Timelines
has_many :stream_entries, inverse_of: :account, dependent: :destroy
@@ -166,6 +167,10 @@ class Account < ApplicationRecord
Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
end
+ def note_has_eight_newlines?
+ errors.add(:note, 'Bio can\'t have more then 8 newlines') unless note.count("\n") <= 8
+ end
+
class << self
def domains
reorder(nil).pluck('distinct accounts.domain')
diff --git a/app/views/settings/profiles/show.html.haml b/app/views/settings/profiles/show.html.haml
index e0fb613f..4d1923eb 100644
--- a/app/views/settings/profiles/show.html.haml
+++ b/app/views/settings/profiles/show.html.haml
@@ -6,7 +6,7 @@
.fields-group
= f.input :display_name, placeholder: t('simple_form.labels.defaults.display_name'), hint: t('simple_form.hints.defaults.display_name', count: 30 - @account.display_name.size).html_safe
- = f.input :note, placeholder: t('simple_form.labels.defaults.note'), hint: t('simple_form.hints.defaults.note', count: 160 - @account.note.size).html_safe
+ = f.input :note, placeholder: t('simple_form.labels.defaults.note'), hint: t('simple_form.hints.defaults.note', count: 413 - @account.note.size).html_safe
.card.compact{ style: "background-image: url(#{@account.header.url(:original)})" }
.avatar= image_tag @account.avatar.url(:original)
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index eeaebb77..6ce3cbc5 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -558,8 +558,8 @@ RSpec.describe Account, type: :model do
expect(account).to model_have_error_on_field(:display_name)
end
- it 'is invalid if the note is longer than 160 characters' do
- account = Fabricate.build(:account, note: Faker::Lorem.characters(161))
+ it 'is invalid if the note is longer than 413 characters' do
+ account = Fabricate.build(:account, note: Faker::Lorem.characters(414))
account.valid?
expect(account).to model_have_error_on_field(:note)
end
@@ -598,8 +598,8 @@ RSpec.describe Account, type: :model do
expect(account).not_to model_have_error_on_field(:display_name)
end
- it 'is valid even if the note is longer than 160 characters' do
- account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(161))
+ it 'is valid even if the note is longer than 413 characters' do
+ account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(414))
account.valid?
expect(account).not_to model_have_error_on_field(:note)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment