Add organizations to user profile (#5583) [deploy]

* Add organizations to user profile

* Add user organization info updated at column and cache key

* Remove default value for users organization_info_updated_at column

* Update organization_memberships factory

* Remove schema organization_info_updated_at default value

Co-authored-by: rhymes <rhymesete@gmail.com>
This commit is contained in:
Sebastien 2020-01-27 13:31:27 -05:00 committed by Ben Halpern
parent c91080c371
commit a25446b1d4
8 changed files with 49 additions and 3 deletions

View file

@ -5,4 +5,11 @@ class OrganizationMembership < ApplicationRecord
validates :user_id, :organization_id, :type_of_user, presence: true
validates :user_id, uniqueness: { scope: :organization_id }
validates :type_of_user, inclusion: { in: %w[admin member guest] }
after_create :update_user_organization_info_updated_at
after_destroy :update_user_organization_info_updated_at
def update_user_organization_info_updated_at
user.touch(:organization_info_updated_at)
end
end

View file

@ -0,0 +1,16 @@
<% if @user.organizations.present? %>
<div id="sidebar-organizations" class="widget">
<div class="widget-suggested-follows-container">
<header><h4>organizations</h4></header>
<div class="widget-body">
<% @user.organizations.find_each do |organization| %>
<div class="widget-user-pic">
<a href="/<%= organization.slug %>">
<img src="<%= ProfileImage.new(organization).get(90) %>" alt="<%= organization.name %> profile image">
</a>
</div>
<% end %>
</div>
</div>
</div>
<% end %>

View file

@ -1,6 +1,7 @@
<div id="sidebar-wrapper-right" class="sidebar-wrapper sidebar-wrapper-right">
<div class="sidebar-bg" id="sidebar-bg-right"></div>
<div class="side-bar sidebar-additional showing" id="sidebar-additional">
<%= render "users/organizations_area" %>
<% @user.github_repos.where(featured: true).order(stargazers_count: :desc, name: :asc).each do |repo| %>
<a class="widget" href="<%= repo.url %>" target="_blank">
<header>

View file

@ -196,7 +196,7 @@
loading...
</div>
</div>
<% cache "user-profile-sidebar-additional-#{@user.id}-#{@user.github_repos_updated_at}-#{@user.badge_achievements_count}", expires_in: 2.days do %>
<% cache "user-profile-sidebar-additional-#{@user.id}-#{@user.github_repos_updated_at}-#{@user.badge_achievements_count}-#{@user.organization_info_updated_at}", expires_in: 2.days do %>
<%= render "users/sidebar_additional" %>
<% end %>
</div>

View file

@ -0,0 +1,5 @@
class AddOrganizationInfoUpdatedAtToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :organization_info_updated_at, :datetime
end
end

View file

@ -1115,6 +1115,7 @@ ActiveRecord::Schema.define(version: 2020_01_20_053525) do
t.string "onboarding_variant_version", default: "0"
t.boolean "org_admin", default: false
t.integer "organization_id"
t.datetime "organization_info_updated_at"
t.boolean "permit_adjacent_sponsors", default: true
t.datetime "personal_data_updated_at"
t.string "profile_image"

View file

@ -1,7 +1,11 @@
FactoryBot.define do
factory :organization_membership do
user
organization
association :user, factory: :user, strategy: :create
association :organization, factory: :organization, strategy: :create
type_of_user { "member" }
after(:build) do |organization_membership|
organization_membership.class.skip_callback(:create, :after, :update_user_organization_info_updated_at, raise: false)
end
end
end

View file

@ -5,6 +5,7 @@ RSpec.describe "User index", type: :system do
let!(:article) { create(:article, user: user) }
let!(:other_article) { create(:article) }
let!(:comment) { create(:comment, user: user, commentable: other_article) }
let(:organization) { create(:organization) }
context "when user is unauthorized" do
context "when 1 article" do
@ -59,6 +60,17 @@ RSpec.describe "User index", type: :system do
end
end
context "when user has an organization membership" do
before do
user.organization_memberships.create(organization: organization, type_of_user: "member")
end
it "shows organizations" do
visit "/user3000"
expect(page).to have_css("#sidebar-wrapper-right h4", text: "organizations")
end
end
context "when visiting own profile" do
before do
sign_in user