Skip to content

Commit

Permalink
Sociable
Browse files Browse the repository at this point in the history
  • Loading branch information
gorangorun committed Feb 4, 2025
1 parent d00054a commit 3011932
Show file tree
Hide file tree
Showing 37 changed files with 569 additions and 212 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/avo/resources/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Avo::Resources::Speaker < Avo::BaseResource
def fields
field :id, as: :id, link_to_record: true
field :name, as: :text, link_to_record: true, sortable: true
field :github, as: :text
field :bio, as: :textarea, hide_on: :index
field :slug, as: :text, hide_on: :index
field :talks_count, as: :number, sortable: true
Expand Down
74 changes: 74 additions & 0 deletions app/controllers/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class SocialProfilesController < ApplicationController
skip_before_action :authenticate_user!, only: [:edit, :update]
before_action :check_ownership, only: [:new, :create]
before_action :check_provider, only: [:new]
before_action :set_social_profile, only: [:edit, :update]

def new
@social_profile = @sociable.social_profiles.new(provider: params[:provider])
end

def create
@social_profile = @sociable.social_profiles.new(social_profile_params)

begin
ActiveRecord::Base.transaction do
@social_profile.save!
@suggestion = @social_profile.create_suggestion_from(
params: @social_profile.attributes.slice("provider", "value"),
new_record: true
)
end
rescue ActiveRecord::RecordInvalid
end

respond_to do |format|
if @suggestion&.persisted?
flash[:notice] = "Saved"
format.turbo_stream
else
format.turbo_stream { render :create, status: :unprocessable_entity }
end
end
end

def edit
end

def update
begin
@suggestion = @social_profile.create_suggestion_from(params: social_profile_params, user: Current.user)
rescue ActiveRecord::RecordInvalid
end

respond_to do |format|
if @suggestion&.persisted?
flash[:notice] = "Saved"
format.turbo_stream
else
format.turbo_stream { render :update, status: :unprocessable_entity }
end
end
end

private

def social_profile_params
params.require(:social_profile).permit(
:provider,
:value
)
end

def set_social_profile
@social_profile ||= SocialProfile.find(params[:id])
end

def check_provider
raise StandardError, "Invalid Social Provider" unless SocialProfile::PROVIDERS.include?(params[:provider])
end

def check_ownership
head :forbidden unless @sociable.managed_by?(Current.user)
end
end
9 changes: 9 additions & 0 deletions app/controllers/speakers/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Speakers::SocialProfilesController < ::SocialProfilesController
prepend_before_action :set_sociable

private

def set_sociable
@sociable ||= Speaker.find_by!(slug: params[:speaker_slug])
end
end
6 changes: 0 additions & 6 deletions app/controllers/speakers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ def speaker_params
params.require(:speaker).permit(
:name,
:github,
:twitter,
:bsky,
:linkedin,
:mastodon,
:bio,
:website,
:speakerdeck,
:pronouns_type,
:pronouns,
:slug
Expand Down
16 changes: 15 additions & 1 deletion app/models/concerns/sociable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ module Sociable
extend ActiveSupport::Concern

included do
has_many :social_profiles, as: :sociable, dependent: :destroy
has_many :social_profiles, -> { order(:created_at) }, as: :sociable, dependent: :destroy
end

SocialProfile::PROVIDERS.each do |method|
define_method(method) do
social_profiles.send(method).first&.value
end

define_method(:"build_#{method}") do |value|
social_profiles.build(provider: method, value:)
end

define_method(:"create_#{method}") do |value|
social_profiles.create(provider: method, value:)
end
end
end
7 changes: 5 additions & 2 deletions app/models/concerns/suggestable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module Suggestable
has_many :suggestions, as: :suggestable, dependent: :destroy
end

def create_suggestion_from(params:, user: Current.user)
suggestions.create(content: select_differences_for(params), suggested_by_id: user&.id).tap do |suggestion|
# NOTE: validate before saving
def create_suggestion_from(params:, user: Current.user, new_record: false)
params = select_differences_for(params) unless new_record

suggestions.create(content: params, suggested_by_id: user&.id).tap do |suggestion|
suggestion.approved!(approver: user) if managed_by?(user)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,6 @@ def year
end

def website
self[:website].presence || organisation.website
social_profiles.detect { |sp| sp.website? }&.value || organisation.website
end
end
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
class Organisation < ApplicationRecord
include Sluggable
include Suggestable
include Sociable

include ActionView::Helpers::TextHelper

Expand Down
55 changes: 37 additions & 18 deletions app/models/social_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
# Table name: social_profiles
#
# id :integer not null, primary key
# provider :integer
# sociable_type :string indexed => [sociable_id]
# value :string
# provider :string not null
# sociable_type :string not null, indexed => [sociable_id]
# value :string not null
# created_at :datetime not null
# updated_at :datetime not null
# sociable_id :integer indexed => [sociable_type]
# sociable_id :integer not null, indexed => [sociable_type]
#
# Indexes
#
# index_social_profiles_on_sociable (sociable_type,sociable_id)
#
class SocialProfile < ApplicationRecord
include Suggestable
PROVIDERS = %w[twitter linkedin bsky mastodon speakerdeck website]

belongs_to :sociable, polymorphic: true

enum :provider, {
github: 0,
twitter: 1,
linkedin: 2,
bsky: 3,
mastadon: 4
},
suffix: true,
validate: {presence: true}

before_save do
self.value = self.class.normalize_value_for(provider.to_sym, value)
enum :provider, PROVIDERS.index_by(&:itself), validate: {presence: true}

after_initialize do
self.value = self.class.normalize_value_for(provider.to_sym, value) if provider.present?
end

validates :provider, presence: true
validates :value, presence: true, uniqueness: {scope: :provider}

scope :excluding_provider, ->(provider) { where.not(provider:) }

# normalizes
normalizes :github, with: ->(value) { value.gsub(/^(?:https?:\/\/)?(?:www\.)?github\.com\//, "").gsub(/^@/, "") }
normalizes :twitter, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:x\.com|twitter\.com)/}, "").gsub(/@/, "") }
normalizes :linkedin, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:linkedin\.com/in)/}, "") }
normalizes :bsky, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:[^\/]+\.com)/}, "").gsub(/@/, "") }
normalizes :bsky, with: ->(value) { value.gsub(%r{https?://(?:www\.)?(?:x\.com|bsky\.app/profile)/}, "").gsub(/@/, "") }
normalizes :speakerdeck, with: ->(value) { value.gsub(/^(?:https?:\/\/)?(?:www\.)?speakerdeck\.com\//, "").gsub(/^@/, "") }
normalizes :mastodon, with: ->(value) {
return value if value&.match?(URI::DEFAULT_PARSER.make_regexp)
return "" unless value.count("@") == 2
Expand All @@ -44,4 +44,23 @@ class SocialProfile < ApplicationRecord

"https://#{instance}/@#{handle}"
}

def url
case provider.to_sym
when :twitter, :speakerdeck
"https://#{provider}.com/#{value}"
when :linkedin
"https://linkedin.com/in/#{value}"
when :bsky
"https://bsky.app/profile/#{value}"
else
value
end
end

private

def managed_by?(visiting_user)
sociable.managed_by?(visiting_user)
end
end
11 changes: 6 additions & 5 deletions app/models/speaker/profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def enhance_all_later
socials = github_client.social_accounts(speaker.github)
links = socials.pluck(:provider, :url).to_h

speaker.build_twitter(links["twitter"]) if links["twitter"].present?
speaker.build_mastodon(links["mastodon"]) if links["mastodon"].present?
speaker.build_bsky(links["bluesky"]) if links["bluesky"].present?
speaker.build_linkedin(links["linkedin"]) if links["linkedin"].present?
speaker.build_website(profile.blog) if profile.blog.present?

speaker.update!(
twitter: speaker.twitter.presence || links["twitter"] || "",
mastodon: speaker.mastodon.presence || links["mastodon"] || "",
bsky: speaker.bsky.presence || links["bluesky"] || "",
linkedin: speaker.linkedin.presence || links["linkedin"] || "",
bio: speaker.bio.presence || profile.bio || "",
website: speaker.website.presence || profile.blog || "",
github_metadata: {
profile: JSON.parse(profile.body),
socials: JSON.parse(socials.body)
Expand Down
12 changes: 7 additions & 5 deletions app/views/events/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
</div>

<div class="container py-8">
<div class="block lg:flex gap-8 align-center justify-between">
<div class="flex flex-col lg:flex-row gap-8 items-center lg:justify-right text-center lg:text-left mb-6 lg:mb-0">
<div class="justify-between block lg:flex gap-8 align-center">
<div class="flex flex-col items-center mb-6 text-center lg:flex-row gap-8 lg:justify-right lg:text-left lg:mb-0">
<%= image_tag image_path(event.avatar_image_path),
class: "rounded-full border border-[#D9DFE3] size-24 md:size-36",
alt: "#{event.name} Avatar",
style: "view-transition-name: avatar",
loading: :lazy %>

<div class="flex-col flex justify-center">
<h1 class="mb-2 text-black font-bold" style="view-transition-name: title"><%= event.name %></h1>
<div class="flex flex-col justify-center">
<h1 class="mb-2 font-bold text-black" style="view-transition-name: title"><%= event.name %></h1>
<h3 class="text-[#636B74]"><%= event.location %><%= event.formatted_dates %></h3>
<%= external_link_to event.website.gsub(%r{^https?://}, "").gsub(%r{/$}, ""), event.website %>
<% if event.website.present? %>
<%= external_link_to event.website.gsub(%r{^https?://}, "").gsub(%r{/$}, ""), event.website %>
<% end %>
</div>
</div>

Expand Down
10 changes: 10 additions & 0 deletions app/views/shared/_new_profile.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= turbo_frame_tag social_profile do %>
<div class="flex items-center gap-x-2">
<span class="font-semibold">Add</span>
<% SocialProfile::PROVIDERS.each do |provider| %>
<%= ui_button url: new_polymorphic_path([sociable, social_profile], provider:), kind: :circle, size: :sm, class: "hover:bg-#{provider} hover:fill-white border-base-200" do %>
<%= fab(provider, size: :md) %>
<% end %>
<% end %>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/social_profiles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= turbo_frame_tag social_profile do %>
<%= form_with(model: social_profile, url: url, class: "flex flex-col") do |form| %>
<%= form.label :value, "#{form.object.provider.humanize} (URL/Handle/Username)" %>
<div class="flex flex-col gap-x-1">
<span class="text-red-500"><%= social_profile.errors["value"] ? social_profile.errors["value"].first : "" %></span>
<div class="flex items-center gap-x-2">
<%= form.hidden_field :provider, class: "input input-bordered w-full" %>
<%= form.text_field :value, class: "input input-bordered w-full" %>
<%= ui_button "Save", type: :submit %>
</div>
</div>
<% end %>
<% end %>
18 changes: 18 additions & 0 deletions app/views/social_profiles/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= turbo_stream.update "flashes" do %>
<%= render "shared/flashes" %>
<% end %>

<% if @social_profile.persisted? %>
<%= turbo_stream.update "socials" do %>
<%= render "speakers/socials", speaker: @social_profile.sociable %>
<% end %>

<%= turbo_stream.append "social-profiles" do %>
<%= render "social_profiles/form", social_profile: @social_profile, url: social_profile_path(@social_profile) %>
<%= render "shared/new_profile", sociable: @social_profile.sociable, social_profile: SocialProfile.new %>
<% end %>
<% else %>
<%= turbo_stream.replace @social_profile do %>
<%= render "social_profiles/form", social_profile: @social_profile, url: speaker_social_profiles_path(@social_profile.sociable, @social_profile) %>
<% end %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/social_profiles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag @social_profile do %>
<%= render "form", social_profile: @social_profile, url: social_profile_path(@social_profile) %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/social_profiles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag @social_profile do %>
<%= render "form", social_profile: @social_profile, url: speaker_social_profiles_path(@social_profile.sociable, @social_profile) %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/social_profiles/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= turbo_stream.update "flashes" do %>
<%= render "shared/flashes" %>
<% end %>

<%= turbo_stream.update "socials" do %>
<%= render "speakers/socials", speaker: @social_profile.sociable %>
<% end %>

<%= turbo_stream.replace @social_profile do %>
<%= render "social_profiles/form", social_profile: @social_profile, url: social_profile_path(@social_profile) %>
<% end %>
30 changes: 0 additions & 30 deletions app/views/speakers/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,6 @@
<%= form.text_field :github, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :twitter, "Twitter/X (URL or handle)" %>
<%= form.text_field :twitter, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :mastodon, "Mastodon (Full URL/handle)" %>
<%= form.text_field :mastodon, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :linkedin, "LinekdIn (URL or slug)" %>
<%= form.text_field :linkedin, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :bsky, "Bluesky (URL or handle)" %>
<%= form.text_field :bsky, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :speakerdeck, "Speakerdeck" %>
<%= form.text_field :speakerdeck, class: "input input-bordered w-full" %>
</div>

<div>
<%= form.label :bio %>
<%= form.text_area :bio, rows: 4, class: "textarea textarea-bordered w-full" %>
Expand All @@ -47,11 +22,6 @@
</div>
</div>

<div>
<%= form.label :website %>
<%= form.text_field :website, class: "input input-bordered w-full" %>
</div>

<div class="flex items-center gap-4">
<%= ui_button "Suggest modifications", type: :submit %>
<%= ui_button "Cancel", data: {action: "click->modal#close"}, role: :button, kind: :ghost %>
Expand Down
Loading

0 comments on commit 3011932

Please sign in to comment.