Add JSON representation of Profile Preview Card info (#14296)
* Add tests and Railsify a few things * Adds JSON representation of ProfilePreviewCardsController * Load profile and setting eagerly * Add profile preview card color
This commit is contained in:
parent
359f742de6
commit
a118b9095a
8 changed files with 179 additions and 10 deletions
|
|
@ -1,7 +0,0 @@
|
|||
class ProfilePreviewCardController < ApplicationController
|
||||
layout false
|
||||
|
||||
def show
|
||||
@actor = User.find_by(id: params[:id])
|
||||
end
|
||||
end
|
||||
7
app/controllers/profile_preview_cards_controller.rb
Normal file
7
app/controllers/profile_preview_cards_controller.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class ProfilePreviewCardsController < ApplicationController
|
||||
layout false
|
||||
|
||||
def show
|
||||
@user = User.includes(:profile, :setting).find(params[:id])
|
||||
end
|
||||
end
|
||||
|
|
@ -77,7 +77,9 @@ const fetchMissingProfilePreviewCard = async (placeholderElement) => {
|
|||
jsCommentUserId: commentUserId,
|
||||
jsDropdownContentId: dropdownContentId,
|
||||
} = placeholderElement.dataset;
|
||||
const response = await window.fetch(`/profile_preview_card/${commentUserId}`);
|
||||
const response = await window.fetch(
|
||||
`/profile_preview_cards/${commentUserId}`,
|
||||
);
|
||||
const htmlContent = await response.text();
|
||||
|
||||
const generatedElement = document.createElement('div');
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
<%= render "shared/profile_preview_card", actor: @actor, id: nil %>
|
||||
1
app/views/profile_preview_cards/show.html.erb
Normal file
1
app/views/profile_preview_cards/show.html.erb
Normal file
|
|
@ -0,0 +1 @@
|
|||
<%= render "shared/profile_preview_card", actor: @user, id: nil %>
|
||||
12
app/views/profile_preview_cards/show.json.jbuilder
Normal file
12
app/views/profile_preview_cards/show.json.jbuilder
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
json.extract!(
|
||||
@user.profile,
|
||||
:summary, :employment_title, :employer_name, :employer_url, :location, :education
|
||||
)
|
||||
|
||||
json.card_color(
|
||||
Color::CompareHex.new([user_colors(@user)[:bg], user_colors(@user)[:text]]).brightness(0.88),
|
||||
)
|
||||
|
||||
json.email @user.email if @user.setting.display_email_on_profile
|
||||
|
||||
json.created_at utc_iso_timestamp(@user.created_at)
|
||||
|
|
@ -189,7 +189,7 @@ Rails.application.routes.draw do
|
|||
resources :article_approvals, only: %i[create]
|
||||
resources :video_chats, only: %i[show]
|
||||
resources :sidebars, only: %i[show]
|
||||
resources :profile_preview_card, only: %i[show]
|
||||
resources :profile_preview_cards, only: %i[show]
|
||||
resources :user_subscriptions, only: %i[create] do
|
||||
collection do
|
||||
get "/subscribed", action: "subscribed"
|
||||
|
|
|
|||
155
spec/requests/profile_preview_cards_spec.rb
Normal file
155
spec/requests/profile_preview_cards_spec.rb
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "ProfilePreviewCards", type: :request do
|
||||
let(:user) { create(:profile).user }
|
||||
|
||||
describe "GET /:id" do
|
||||
context "when signed out" do
|
||||
it "does not find an unknown user id" do
|
||||
expect { get profile_preview_card_path(9999) }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
|
||||
it "is a successful response" do
|
||||
get profile_preview_card_path(user)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
|
||||
it "returns the data" do
|
||||
get profile_preview_card_path(user)
|
||||
|
||||
expect(response.body).to include("profile-preview-card__content")
|
||||
end
|
||||
end
|
||||
|
||||
context "when signed in" do
|
||||
before { sign_in(user) }
|
||||
|
||||
it "does not find an unknown user id" do
|
||||
expect { get profile_preview_card_path(9999) }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
|
||||
it "is a successful response" do
|
||||
get profile_preview_card_path(user)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
|
||||
it "returns the data" do
|
||||
get profile_preview_card_path(user)
|
||||
|
||||
expect(response.body).to include("profile-preview-card__content")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /:id as JSON" do
|
||||
let(:profile) { user.profile }
|
||||
|
||||
context "when signed out" do
|
||||
it "does not find an unknown user id" do
|
||||
expect { get profile_preview_card_path(9999), as: :json }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
|
||||
it "is a successful response" do
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
|
||||
it "returns the data", :aggregate_failures do
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card["summary"]).to eq(profile.summary)
|
||||
expect(preview_card["employment_title"]).to eq(profile.employment_title)
|
||||
expect(preview_card["employer_name"]).to eq(profile.employer_name)
|
||||
expect(preview_card["employer_url"]).to eq(profile.employer_url)
|
||||
expect(preview_card["location"]).to eq(profile.location)
|
||||
expect(preview_card["education"]).to eq(profile.education)
|
||||
expect(preview_card["created_at"]).to eq(profile.created_at.utc.iso8601)
|
||||
end
|
||||
|
||||
it "has the correct card color" do
|
||||
user.setting.update(brand_color1: Faker::Color.hex_color)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
expected_card_color = Color::CompareHex.new([user_colors(user)[:bg], user_colors(user)[:text]]).brightness(0.88)
|
||||
expect(response.parsed_body["card_color"]).to eq(expected_card_color)
|
||||
end
|
||||
|
||||
it "does not return the email if the user has asked not to" do
|
||||
user.setting.update_columns(display_email_on_profile: false)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card.key?("email")).to be(false)
|
||||
end
|
||||
|
||||
it "returns the email if the user wants to" do
|
||||
user.setting.update_columns(display_email_on_profile: true)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card["email"]).to eq(user.email)
|
||||
end
|
||||
end
|
||||
|
||||
context "when signed in" do
|
||||
before { sign_in(user) }
|
||||
|
||||
it "does not find an unknown user id" do
|
||||
expect { get profile_preview_card_path(9999), as: :json }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
|
||||
it "is a successful response" do
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
|
||||
it "returns the data", :aggregate_failures do
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card["summary"]).to eq(profile.summary)
|
||||
expect(preview_card["employment_title"]).to eq(profile.employment_title)
|
||||
expect(preview_card["employer_name"]).to eq(profile.employer_name)
|
||||
expect(preview_card["employer_url"]).to eq(profile.employer_url)
|
||||
expect(preview_card["location"]).to eq(profile.location)
|
||||
expect(preview_card["education"]).to eq(profile.education)
|
||||
expect(preview_card["created_at"]).to eq(profile.created_at.utc.iso8601)
|
||||
end
|
||||
|
||||
it "has the correct card color" do
|
||||
user.setting.update(brand_color1: Faker::Color.hex_color)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
expected_card_color = Color::CompareHex.new([user_colors(user)[:bg], user_colors(user)[:text]]).brightness(0.88)
|
||||
expect(response.parsed_body["card_color"]).to eq(expected_card_color)
|
||||
end
|
||||
|
||||
it "does not return the email if the user has asked not to" do
|
||||
user.setting.update_columns(display_email_on_profile: false)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card.key?("email")).to be(false)
|
||||
end
|
||||
|
||||
it "returns the email if the user wants to" do
|
||||
user.setting.update_columns(display_email_on_profile: true)
|
||||
|
||||
get profile_preview_card_path(user), as: :json
|
||||
|
||||
preview_card = response.parsed_body
|
||||
expect(preview_card["email"]).to eq(user.email)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue