Fix URL Validations and Update Admin Dashboard (#1392)

* Validate for trailing slash instead of strict validation

* Remove unused sign_in_count field

* Add profile URLs to edit view

* Add profile URLs to admin controller params

* Add profile URLs to edit view

* Clean up index and search views for user

* Move around user fields to make more sense

* Display more informative label

* Remove name_of_user field in favor of new display label

* Fix typo

* Remove duplication for website, employer, and Mastodon URLs

* Add score to comment params

* Remove unused name_of_user field views

* Fix typo for medium url
This commit is contained in:
Andy Zhao 2018-12-26 18:00:02 -05:00 committed by Ben Halpern
parent 9e18264f9f
commit 0a8f9ca654
15 changed files with 73 additions and 108 deletions

View file

@ -14,7 +14,7 @@ module Admin
private
def comment_params
accessible = %i[user_id body_markdown deleted]
accessible = %i[user_id body_markdown deleted score]
params.require(:comment).permit(accessible)
end
end

View file

@ -14,53 +14,43 @@ module Admin
private
def user_params
accessible = %i[name
email
username
twitter_username
github_username
profile_image
website_url
summary
email_newsletter
email_comment_notifications
email_follower_notifications
organization_id
org_admin
bg_color_hex
text_color_hex
employer_name
employer_url
employment_title
currently_learning
available_for
mostly_work_with
currently_hacking_on
location
email_public
education
looking_for_work
looking_for_work_publicly
contact_consent
feed_url
feed_mark_canonical
feed_admin_publish_permission
reputation_modifier
onboarding_package_requested
onboarding_package_fulfilled
onboarding_package_requested_again
shipping_name
shipping_company
shipping_address
shipping_address_line_2
shipping_city
shipping_state
shipping_country
shipping_postal_code
shirt_size
shirt_gender
saw_onboarding
scholar_email]
accessible = %i[
name
email
username
twitter_username
github_username
profile_image
website_url
summary
email_newsletter
email_comment_notifications
email_follower_notifications
organization_id
org_admin
bg_color_hex
text_color_hex
employer_name
employer_url
employment_title
currently_learning
available_for
mostly_work_with
currently_hacking_on
location
email_public
education
feed_url
reputation_modifier
saw_onboarding
scholar_email
facebook_url
behance_url
dribbble_url
medium_url
gitlab_url
linkedin_url
]
accessible << %i[password password_confirmation] unless params[:user][:password].blank?
params.require(:user).permit(accessible)
end

View file

@ -100,6 +100,6 @@ class ArticleDashboard < Administrate::BaseDashboard
# across all pages of the admin dashboard.
#
def display_resource(article)
article.title
"Article ##{article.id} - #{article.title}"
end
end

View file

@ -10,7 +10,6 @@ class BadgeAchievementDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
user: Field::BelongsTo,
user_id: UserIdField,
name_of_user: NameOfUserField,
badge: Field::BelongsTo,
rewarder: Field::BelongsTo.with_options(class_name: "User"),
rewarding_context_message_markdown: Field::String,
@ -38,7 +37,6 @@ class BadgeAchievementDashboard < Administrate::BaseDashboard
SHOW_PAGE_ATTRIBUTES = %i[
id
user
name_of_user
badge
rewarding_context_message
rewarder

View file

@ -11,7 +11,6 @@ class CommentDashboard < Administrate::BaseDashboard
commentable: Field::Polymorphic,
user: Field::BelongsTo,
user_id: UserIdField,
name_of_user: NameOfUserField,
reactions: Field::HasMany,
id: Field::Number,
body_markdown: Field::Text.with_options(searchable: true),
@ -32,7 +31,7 @@ class CommentDashboard < Administrate::BaseDashboard
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
id
name_of_user
user
body_markdown
reactions
].freeze
@ -42,7 +41,6 @@ class CommentDashboard < Administrate::BaseDashboard
SHOW_PAGE_ATTRIBUTES = %i[
commentable
user
name_of_user
reactions
id
body_markdown
@ -61,13 +59,14 @@ class CommentDashboard < Administrate::BaseDashboard
FORM_ATTRIBUTES = %i[
user_id
body_markdown
score
deleted
].freeze
# Overwrite this method to customize how comments are displayed
# across all pages of the admin dashboard.
#
# def display_resource(comment)
# "Comment ##{comment.id}"
# end
def display_resource(comment)
"Comment ##{comment.id} - #{comment.title}"
end
end

View file

@ -56,7 +56,7 @@ class ReactionDashboard < Administrate::BaseDashboard
# Overwrite this method to customize how reactions are displayed
# across all pages of the admin dashboard.
#
# def display_resource(reaction)
# "Reaction ##{reaction.id}"
# end
def display_resource(reaction)
"#{reaction.category} on #{reaction.reactable_type} ##{reaction.reactable_id}"
end
end

View file

@ -30,7 +30,6 @@ class UserDashboard < Administrate::BaseDashboard
updated_at: Field::DateTime,
articles: Field::HasMany,
comments: Field::HasMany,
sign_in_count: Field::Number,
reputation_modifier: Field::Number,
signup_cta_variant: Field::String,
onboarding_package_requested: Field::Boolean,
@ -40,6 +39,12 @@ class UserDashboard < Administrate::BaseDashboard
bg_color_hex: Field::String,
text_color_hex: Field::String,
feed_url: Field::String,
facebook_url: Field::String,
behance_url: Field::String,
dribbble_url: Field::String,
medium_url: Field::String,
gitlab_url: Field::String,
linkedin_url: Field::String,
feed_admin_publish_permission: Field::Boolean,
feed_mark_canonical: Field::Boolean,
saw_onboarding: Field::Boolean,
@ -56,14 +61,11 @@ class UserDashboard < Administrate::BaseDashboard
COLLECTION_ATTRIBUTES = %i[
profile_image
id
created_at
username
name
twitter_username
github_username
following_tags_count
saw_onboarding
monthly_dues
name
banned
].freeze
# SHOW_PAGE_ATTRIBUTES
@ -90,6 +92,12 @@ class UserDashboard < Administrate::BaseDashboard
analytics
summary
website_url
facebook_url
behance_url
dribbble_url
medium_url
gitlab_url
linkedin_url
bg_color_hex
text_color_hex
reputation_modifier
@ -100,7 +108,7 @@ class UserDashboard < Administrate::BaseDashboard
# Overwrite this method to customize how users are displayed
# across all pages of the admin dashboard.
#
# def display_resource(user)
# user.username
# end
def display_resource(user)
"ID: ##{user.id} - #{user.username}"
end
end

View file

@ -1,7 +0,0 @@
require "administrate/field/base"
class NameOfUserField < Administrate::Field::Base
def to_s
data
end
end

View file

@ -22,10 +22,6 @@ class BadgeAchievement < ApplicationRecord
self.rewarding_context_message = final_html
end
def name_of_user
user.name
end
private
def notify_recipient

View file

@ -166,11 +166,6 @@ class Comment < ApplicationRecord
nil
end
# Administrate field
def name_of_user
user.name
end
def readable_publish_date
if created_at.year == Time.current.year
created_at.strftime("%b %e")

View file

@ -64,33 +64,33 @@ class User < ApplicationRecord
validates :github_username, uniqueness: { allow_blank: true }
validates :text_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
validates :bg_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
validates :website_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
validates :website_url, :employer_url, :mastodon_url,
url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
# rubocop:disable Metrics/LineLength
validates :facebook_url,
format: /\Ahttps:\/\/(www.facebook.com|facebook.com)\/[a-zA-Z0-9.]{5,50}\/?\Z/,
format: /\A(http(s)?:\/\/)?(www.facebook.com|facebook.com)\/.*\Z/,
allow_blank: true
validates :stackoverflow_url,
allow_blank: true,
format:
/\Ahttps:\/\/(www.stackoverflow.com|stackoverflow.com|www.stackexchange.com|stackexchange.com)\/([\S]{3,100})\Z/
/\A(http(s)?:\/\/)?(www.stackoverflow.com|stackoverflow.com|www.stackexchange.com|stackexchange.com)\/.*\Z/
validates :behance_url,
allow_blank: true,
format: /\Ahttps:\/\/(www.behance.net|behance.net)\/([a-zA-Z0-9\-\_]{3,20})\/?\Z/
format: /\A(http(s)?:\/\/)?(www.behance.net|behance.net)\/.*\Z/
validates :linkedin_url,
allow_blank: true,
format:
/\Ahttps:\/\/(www.linkedin.com|linkedin.com|[A-Za-z]{2}.linkedin.com)\/in\/([a-zA-Z0-9\-]{3,100})\/?\Z/
/\A(http(s)?:\/\/)?(www.linkedin.com|linkedin.com|[A-Za-z]{2}.linkedin.com)\/.*\Z/
validates :dribbble_url,
allow_blank: true,
format: /\Ahttps:\/\/(www.dribbble.com|dribbble.com)\/([a-zA-Z0-9\-\_]{2,20})\/?\Z/
format: /\A(http(s)?:\/\/)?(www.dribbble.com|dribbble.com)\/.*\Z/
validates :medium_url,
allow_blank: true,
format: /\Ahttps:\/\/(www.medium.com|medium.com)\/([a-zA-Z0-9\-\_\@\.]{2,32})\/?\Z/
format: /\A(http(s)?:\/\/)?(www.medium.com|medium.com)\/.*\Z/
validates :gitlab_url,
allow_blank: true,
format: /\Ahttps:\/\/(www.gitlab.com|gitlab.com)\/([a-zA-Z0-9_\-\.]{1,100})\/?\Z/
format: /\A(http(s)?:\/\/)?(www.gitlab.com|gitlab.com)\/.*\Z/
# rubocop:enable Metrics/LineLength
validates :employer_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
validates :shirt_gender,
inclusion: { in: %w(unisex womens),
message: "%{value} is not a valid shirt style" },
@ -109,10 +109,8 @@ class User < ApplicationRecord
validates :shipping_country,
length: { in: 2..2 },
allow_blank: true
validates :website_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
validates :website_url, :employer_name, :employer_url,
length: { maximum: 100 }
validates :mastodon_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }
validates :employment_title, :education, :location,
length: { maximum: 100 }
validates :mostly_work_with, :currently_learning,

View file

@ -1,6 +0,0 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.text_field field.attribute %>
</div>

View file

@ -1 +0,0 @@
<%= field.to_s %>

View file

@ -1 +0,0 @@
<%= field.to_s %>

View file

@ -141,10 +141,6 @@ RSpec.describe Comment, type: :model do
expect(comment.path).not_to be(nil)
end
it "returns name_of_user" do
expect(comment.name_of_user).to eq(comment.user.name)
end
it "returns the properly formed path" do
expect(comment.path).to eq("/#{comment.user.username}/comment/#{comment.id_code_generated}")
end