From 0a8f9ca6548294dfbe402f29f726ebb91e4f7284 Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Wed, 26 Dec 2018 18:00:02 -0500 Subject: [PATCH] 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 --- app/controllers/admin/comments_controller.rb | 2 +- app/controllers/admin/users_controller.rb | 84 ++++++++----------- app/dashboards/article_dashboard.rb | 2 +- app/dashboards/badge_achievement_dashboard.rb | 2 - app/dashboards/comment_dashboard.rb | 11 ++- app/dashboards/reaction_dashboard.rb | 6 +- app/dashboards/user_dashboard.rb | 26 ++++-- app/fields/name_of_user_field.rb | 7 -- app/models/badge_achievement.rb | 4 - app/models/comment.rb | 5 -- app/models/user.rb | 20 ++--- .../fields/name_of_user_field/_form.html.erb | 6 -- .../fields/name_of_user_field/_index.html.erb | 1 - .../fields/name_of_user_field/_show.html.erb | 1 - spec/models/comment_spec.rb | 4 - 15 files changed, 73 insertions(+), 108 deletions(-) delete mode 100644 app/fields/name_of_user_field.rb delete mode 100644 app/views/fields/name_of_user_field/_form.html.erb delete mode 100644 app/views/fields/name_of_user_field/_index.html.erb delete mode 100644 app/views/fields/name_of_user_field/_show.html.erb diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 1a82ae1d9..3db862bf8 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -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 diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 4a8240e81..9a59503a0 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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 diff --git a/app/dashboards/article_dashboard.rb b/app/dashboards/article_dashboard.rb index 2bb9e7fd4..8abc28ded 100644 --- a/app/dashboards/article_dashboard.rb +++ b/app/dashboards/article_dashboard.rb @@ -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 diff --git a/app/dashboards/badge_achievement_dashboard.rb b/app/dashboards/badge_achievement_dashboard.rb index ed67a763c..0a89ec5d2 100644 --- a/app/dashboards/badge_achievement_dashboard.rb +++ b/app/dashboards/badge_achievement_dashboard.rb @@ -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 diff --git a/app/dashboards/comment_dashboard.rb b/app/dashboards/comment_dashboard.rb index 31e8a1ada..e6eafaf63 100644 --- a/app/dashboards/comment_dashboard.rb +++ b/app/dashboards/comment_dashboard.rb @@ -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 diff --git a/app/dashboards/reaction_dashboard.rb b/app/dashboards/reaction_dashboard.rb index ad70a553d..0c2dce800 100644 --- a/app/dashboards/reaction_dashboard.rb +++ b/app/dashboards/reaction_dashboard.rb @@ -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 diff --git a/app/dashboards/user_dashboard.rb b/app/dashboards/user_dashboard.rb index 72be9137b..12df387d3 100644 --- a/app/dashboards/user_dashboard.rb +++ b/app/dashboards/user_dashboard.rb @@ -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 diff --git a/app/fields/name_of_user_field.rb b/app/fields/name_of_user_field.rb deleted file mode 100644 index 53272b729..000000000 --- a/app/fields/name_of_user_field.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "administrate/field/base" - -class NameOfUserField < Administrate::Field::Base - def to_s - data - end -end diff --git a/app/models/badge_achievement.rb b/app/models/badge_achievement.rb index 278922c82..204c6b113 100644 --- a/app/models/badge_achievement.rb +++ b/app/models/badge_achievement.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index fccae0ad2..8e96a8397 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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") diff --git a/app/models/user.rb b/app/models/user.rb index 74a0a93ef..17aa3668d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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, diff --git a/app/views/fields/name_of_user_field/_form.html.erb b/app/views/fields/name_of_user_field/_form.html.erb deleted file mode 100644 index 1e72f7636..000000000 --- a/app/views/fields/name_of_user_field/_form.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -
- <%= f.label field.attribute %> -
-
- <%= f.text_field field.attribute %> -
diff --git a/app/views/fields/name_of_user_field/_index.html.erb b/app/views/fields/name_of_user_field/_index.html.erb deleted file mode 100644 index 6d9dbc907..000000000 --- a/app/views/fields/name_of_user_field/_index.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= field.to_s %> diff --git a/app/views/fields/name_of_user_field/_show.html.erb b/app/views/fields/name_of_user_field/_show.html.erb deleted file mode 100644 index 6d9dbc907..000000000 --- a/app/views/fields/name_of_user_field/_show.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= field.to_s %> diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index de32c145f..7b288edfa 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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