Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/controllers/site/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def show
end

@jobs = Hiring::JobPost.visible_to(current_user).where(company: @company).newest
@viewer_profile = current_user&.developer_profile
@following = @viewer_profile&.following_company?(@company) || false
@followers_count = @company.company_follows.count
@technologies = @company.technologies.order(:name)
end
end
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/site/company_follows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Site
# A developer can follow an organization without creating a person-to-person
# connection or changing access to either developer's profile details.
class CompanyFollowsController < BaseController
before_action :require_viewer_profile

rate_limit to: 30, within: 1.minute, only: :create,
with: -> { redirect_back_to_company alert: "You're following companies too quickly. Try again in a minute." }

def create
viewer_profile.company_follows.find_or_create_by!(company: target_company)
redirect_back_to_company notice: "You're following #{target_company.display_name}."
rescue ActiveRecord::RecordInvalid => e
redirect_back_to_company alert: e.record.errors.full_messages.to_sentence
end

def destroy
viewer_profile.company_follows.where(company: target_company).destroy_all
redirect_back_to_company notice: "You've unfollowed #{target_company.display_name}."
end

private

def require_viewer_profile
if current_user.nil?
redirect_to rodauth(:user).login_path, alert: "Log in to follow companies."
elsif viewer_profile.nil?
redirect_to new_developer_profile_path, alert: "Create a developer profile to follow companies."
end
end

def viewer_profile
current_user&.developer_profile
end

def target_company
@target_company ||= Company.organizations.find_by!(slug: params[:slug])
end

def redirect_back_to_company(**flash)
redirect_back fallback_location: public_company_path(slug: params[:slug]), status: :see_other, **flash
end
end
end
2 changes: 2 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Company < ::ResourceRecord
has_many :technologies, through: :company_technologies, source: :skill
has_many :job_posts, class_name: "Hiring::JobPost", dependent: :destroy
has_many :company_user_invites, class_name: "Invites::CompanyUserInvite", dependent: :destroy
has_many :company_follows, class_name: "Network::CompanyFollow", inverse_of: :company, dependent: :delete_all
has_many :followers, through: :company_follows, source: :follower

# add has_many associations above.

Expand Down
22 changes: 22 additions & 0 deletions app/views/shared/_company_follow_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%# Follow / Following for a company.
Locals: company, following, variant: :site or :portal, size: :md or :sm. %>
<% variant = local_assigns.fetch(:variant, :site) %>
<% size = local_assigns.fetch(:size, :md) %>
<% path = main_app.follow_company_path(slug: company.slug) %>
<% classes = if variant == :portal
["pu-btn", (size == :sm) ? "pu-btn-sm" : "pu-btn-md", following ? "pu-btn-outline" : "pu-btn-primary"]
else
["dc-btn dc-btn-sm", ("px-3 py-1.5 text-xs" if size == :sm), following ? "dc-btn-white" : "dc-btn-primary"]
end %>
<% if following %>
<%= button_to path, method: :delete, class: class_names(classes, "group"), form: {class: "inline"}, title: "Unfollow #{company.display_name}" do %>
<%= render Phlex::TablerIcons::Check.new(class: "h-4 w-4") %>
<span class="group-hover:hidden group-focus-visible:hidden">Following</span>
<span class="hidden group-hover:inline group-focus-visible:inline">Unfollow</span>
<% end %>
<% else %>
<%= button_to path, method: :post, class: classes, form: {class: "inline"} do %>
<%= render Phlex::TablerIcons::Building.new(class: "h-4 w-4") %>
<span>Follow</span>
<% end %>
<% end %>
13 changes: 12 additions & 1 deletion app/views/site/companies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@
<% if company.organization_type_label.present? %>
<p class="mb-1 text-xs font-bold uppercase tracking-[0.14em] text-[#777]"><%= company.organization_type_label %></p>
<% end %>
<h1 class="dc-heading text-4xl"><%= company.display_name %></h1>
<div class="flex flex-wrap items-center justify-between gap-3">
<h1 class="dc-heading text-4xl"><%= company.display_name %></h1>
<% if @viewer_profile %>
<%= render "shared/company_follow_button", company:, following: @following %>
<% else %>
<%= link_to (current_user ? new_developer_profile_path : rodauth(:user).login_path), class: "dc-btn dc-btn-primary dc-btn-sm",
title: (current_user ? "Create a developer profile to follow companies" : "Log in to follow companies") do %>
<%= render Phlex::TablerIcons::Building.new(class: "h-4 w-4") %> Follow
<% end %>
<% end %>
</div>
<% if company.tagline.present? %><p class="mt-2 max-w-2xl text-lg text-[#444]"><%= company.tagline %></p><% end %>
<p class="mt-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-sm text-[#555]">
<% if location.present? %><span class="flex items-center gap-1"><%= render Phlex::TablerIcons::MapPin.new(class: "h-4 w-4") %> <%= location %></span><% end %>
<% if company.industry.present? %><span class="flex items-center gap-1"><%= render Phlex::TablerIcons::Building.new(class: "h-4 w-4") %> <%= company.industry %></span><% end %>
<% if company.founded_year.present? %><span>Founded <%= company.founded_year %></span><% end %>
<span><strong><%= @followers_count %></strong> <%= "follower".pluralize(@followers_count) %></span>
</p>
<% if company.website.present? || links.any? %>
<div class="mt-4 flex flex-wrap gap-2">
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
get "companies", to: "companies#index", as: :public_companies
get "companies/:slug/logo", to: "company_logos#show", as: :public_company_logo
get "companies/:slug", to: "companies#show", as: :public_company
post "companies/:slug/follow", to: "company_follows#create", as: :follow_company
delete "companies/:slug/follow", to: "company_follows#destroy"
get "projects", to: "projects#index", as: :public_projects
get "projects/:slug", to: "projects#show", as: :public_project

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20261107150000_create_network_company_follows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateNetworkCompanyFollows < ActiveRecord::Migration[8.1]
def change
create_table :network_company_follows do |t|
t.references :follower, null: false,
foreign_key: {to_table: :developers_profiles, on_delete: :cascade}
t.references :company, null: false, foreign_key: {on_delete: :cascade}

t.timestamps

t.index %i[follower_id company_id], unique: true
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added docs/screenshots/21-company-following.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/22-company-unfollow-state.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/screenshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ their env vars were set; no Slack messages were sent.
| 18-network | Developer portal network page, "People you may know" |
| 19-credits | Credits: projects you've been added to, to confirm or decline |
| 20-experience-description-checklist | New experience form with description guidance and a private self-review checklist |
| 21-company-following | Public company page with follower count and the signed-in developer's follow state |
| 22-company-unfollow-state | Public company page with the focused follow control offering to unfollow |
| 23-network-followed-companies | Developer network page listing followed companies |
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
module DeveloperPortal
# The viewer's network: connections, followers, people they follow and
# people they may know. Following and unfollowing go through the public
# FollowsController so the buttons work the same everywhere.
# The viewer's network: connections, followers, people and companies they
# follow, and people they may know. Follow actions use the public controllers
# so the buttons work the same everywhere.
class NetworkController < PlutoniumController
TABS = %w[connections followers following suggestions].freeze
TABS = %w[connections followers following companies suggestions].freeze

def index
@profile = current_scoped_entity
@tab = TABS.include?(params[:tab]) ? params[:tab] : "connections"
@counts = {
"connections" => @profile.connections.count,
"followers" => @profile.followers.count,
"following" => @profile.following.count
"following" => @profile.following.count,
"companies" => @profile.followed_companies.count
}
if @tab == "companies"
@companies = @profile.followed_companies.merge(::Network::CompanyFollow.newest).to_a
return
end

@people =
case @tab
when "connections" then @profile.connections.order(:name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
["connections", "Connections", @counts["connections"]],
["followers", "Followers", @counts["followers"]],
["following", "Following", @counts["following"]],
["companies", "Companies", @counts["companies"]],
["suggestions", "People you may know", nil]
] %>
<% empty = {
"connections" => "No connections yet. When you and someone else follow each other, you're connected.",
"followers" => "Nobody follows you yet. Share your profile page to get started.",
"following" => "You're not following anyone yet. Find people in the developer directory.",
"companies" => "You're not following any companies yet. Browse companies to find teams you want to keep up with.",
"suggestions" => "No suggestions yet. Add skills to your profile and follow a few people, and we'll suggest more."
} %>

<div class="mx-auto max-w-5xl space-y-6">
<div class="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
<div>
<h1 class="font-display text-3xl text-[var(--pu-text)]">Network</h1>
<p class="text-sm text-[var(--pu-text-muted)]">Follow people to keep up with them. When you follow each other you're connected: connections can endorse your skills and see contact details you share with them.</p>
<p class="text-sm text-[var(--pu-text-muted)]">Follow people and companies to keep up with them. When two people follow each other they're connected: connections can endorse skills and see contact details shared with them.</p>
</div>
<div class="flex flex-wrap gap-2">
<a href="<%= main_app.developers_directory_path %>" class="pu-btn pu-btn-md pu-btn-outline shrink-0">
<%= render Phlex::TablerIcons::Search.new(class: "mr-1 h-4 w-4") %> Find developers
</a>
<a href="<%= main_app.public_companies_path %>" class="pu-btn pu-btn-md pu-btn-outline shrink-0">
<%= render Phlex::TablerIcons::Building.new(class: "mr-1 h-4 w-4") %> Browse companies
</a>
</div>
<a href="<%= main_app.developers_directory_path %>" class="pu-btn pu-btn-md pu-btn-outline shrink-0">
<%= render Phlex::TablerIcons::Search.new(class: "mr-1 h-4 w-4") %> Find developers
</a>
</div>

<nav class="flex gap-1 overflow-x-auto border-b border-[var(--pu-border)]" aria-label="Network">
Expand All @@ -35,7 +42,26 @@
<% end %>
</nav>

<% if @people.any? %>
<% if @tab == "companies" && @companies.any? %>
<ul class="grid grid-cols-1 gap-4 md:grid-cols-2">
<% @companies.each do |company| %>
<li class="pu-card">
<div class="pu-card-body flex items-start gap-4">
<%= render InitialsAvatar.new(name: company.display_name, seed: company.slug, size: :md, shape: :square) %>
<div class="min-w-0 flex-1">
<a href="<%= main_app.public_company_path(slug: company.slug) %>" class="block truncate font-semibold text-[var(--pu-text)] hover:text-primary-600 dark:hover:text-primary-400"><%= company.display_name %></a>
<% if company.tagline.present? %><p class="line-clamp-2 text-sm text-[var(--pu-text-muted)]"><%= company.tagline %></p><% end %>
<% location = [company.city, company.country].compact_blank.join(", ") %>
<% if location.present? %><p class="mt-1 truncate text-xs text-[var(--pu-text-subtle)]"><%= location %></p><% end %>
</div>
<div class="shrink-0">
<%= render "shared/company_follow_button", company:, following: true, variant: :portal, size: :sm %>
</div>
</div>
</li>
<% end %>
</ul>
<% elsif @tab != "companies" && @people.any? %>
<ul class="grid grid-cols-1 gap-4 md:grid-cols-2">
<% @people.each do |person| %>
<li class="pu-card">
Expand Down
7 changes: 7 additions & 0 deletions packages/developers/app/models/developers/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Developers::Profile < Developers::ResourceRecord
has_many :incoming_follows, class_name: "Network::Follow", foreign_key: :followee_id, inverse_of: :followee, dependent: :delete_all
has_many :following, through: :outgoing_follows, source: :followee
has_many :followers, through: :incoming_follows, source: :follower
has_many :company_follows, class_name: "Network::CompanyFollow", foreign_key: :follower_id,
inverse_of: :follower, dependent: :delete_all
has_many :followed_companies, through: :company_follows, source: :company
has_many :given_endorsements, class_name: "Network::Endorsement", foreign_key: :endorser_id, inverse_of: :endorser, dependent: :destroy

# Showcase: projects they own, and their credits on other people's projects.
Expand Down Expand Up @@ -193,6 +196,10 @@ def followed_by?(other)
other.present? && incoming_follows.exists?(follower_id: other.id)
end

def following_company?(company)
company.present? && company_follows.exists?(company_id: company.id)
end

# Followers this profile hasn't followed back yet.
def unanswered_followers
followers.where.not(id: outgoing_follows.select(:followee_id))
Expand Down
17 changes: 17 additions & 0 deletions packages/network/app/models/network/company_follow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# One developer following a company. Company follows are deliberately separate
# from developer follows: they do not create connections or grant access to
# connection-only profile details.
require_relative "../network"

class Network::CompanyFollow < Network::ResourceRecord
belongs_to :follower, class_name: "Developers::Profile", inverse_of: :company_follows
belongs_to :company, inverse_of: :company_follows

scope :newest, -> { order(created_at: :desc) }

validates :company, uniqueness: {scope: :follower_id, message: "is already followed"}

def to_label
"#{follower&.name} follows #{company&.display_name}"
end
end
65 changes: 65 additions & 0 deletions test/integration/company_following_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "test_helper"

class CompanyFollowingTest < ActionDispatch::IntegrationTest
include Plutonium::Testing::AuthHelpers
include AccountsTestHelper

setup do
@profile = create_profile!(name: "Ada Lovelace", handle: "ada", visibility: :everyone)
@company = create_company!(name: "Acme Labs").tap do |company|
company.update!(tagline: "Infrastructure for growing teams", city: "Accra", country: "Ghana")
end
end

test "a developer follows and unfollows a company" do
login_user(@profile.user)

post "/companies/acme-labs/follow"

assert_redirected_to "/companies/acme-labs"
assert @profile.reload.following_company?(@company)
assert_no_difference("Network::CompanyFollow.count") { post "/companies/acme-labs/follow" }
follow_redirect!
assert_includes response.body, "You&#39;re following Acme Labs."
assert_select "form[action='/companies/acme-labs/follow'] button", text: /Following/
assert_select "form[action='/companies/acme-labs/follow'] button span", text: "Unfollow"
assert_select "span", text: /1 follower/

delete "/companies/acme-labs/follow"

assert_not @profile.reload.following_company?(@company)
end

test "following requires a developer profile" do
post "/companies/acme-labs/follow"
assert_redirected_to "/users/login"

login_user(create_user!)
post "/companies/acme-labs/follow"
assert_redirected_to "/setup/developer/new"

assert_equal 0, Network::CompanyFollow.count
end

test "personal posting spaces cannot be followed" do
personal = Company.personal_for!(@profile.user)
login_user(@profile.user)

post "/companies/#{personal.slug}/follow"

assert_response :not_found
assert_equal 0, Network::CompanyFollow.count
end

test "followed companies appear in the developer network" do
@profile.company_follows.create!(company: @company)
login_user(@profile.user)

get "/developer/ada/network", params: {tab: "companies"}

assert_response :success
assert_select "a[href='/companies/acme-labs']", "Acme Labs"
assert_includes response.body, "Infrastructure for growing teams"
assert_select "form[action='/companies/acme-labs/follow'] button", text: /Following/
end
end
Loading