Skip to content

Commit

Permalink
Add organizations to user profile (#5583) [deploy]
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and benhalpern committed Jan 27, 2020
1 parent c91080c commit a25446b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/models/organization_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions app/views/users/_organizations_area.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
1 change: 1 addition & 0 deletions app/views/users/_sidebar_additional.html.erb
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrganizationInfoUpdatedAtToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :organization_info_updated_at, :datetime
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@
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"
Expand Down
8 changes: 6 additions & 2 deletions spec/factories/organization_memberships.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/system/user/view_user_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -59,6 +60,17 @@
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
Expand Down

0 comments on commit a25446b

Please sign in to comment.