Skip to content

Commit

Permalink
Social media profiles to conferences
Browse files Browse the repository at this point in the history
  • Loading branch information
gorangorun committed Jan 24, 2025
1 parent 2125bb3 commit 794c759
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 28 deletions.
3 changes: 2 additions & 1 deletion app/avo/resources/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Avo::Resources::Event < Avo::BaseResource
if id.is_a?(Array)
query.where(slug: id)
else
query.find_by(slug: id)
query.find_by(slug: id) || query.find_by(id:)
end
}
self.external_link = -> {
Expand All @@ -27,6 +27,7 @@ def fields
field :talks, as: :has_many
field :speakers, as: :has_many, through: :talks
field :topics, as: :has_many
field :social_profiles, as: :has_many, use_resource: "Avo::Resources::SocialProfile"
end

def actions
Expand Down
8 changes: 8 additions & 0 deletions app/avo/resources/social_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Avo::Resources::SocialProfile < Avo::BaseResource
def fields
field :id, as: :id
field :provider, enum: ::SocialProfile.providers, as: :select, required: true
field :value, as: :text
field :sociable, as: :belongs_to, polymorphic_as: :sociable, types: [::Speaker, ::Event], foreign_key: :slug
end
end
10 changes: 2 additions & 8 deletions app/avo/resources/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Avo::Resources::Speaker < Avo::BaseResource
if id.is_a?(Array)
query.where(slug: id)
else
query.find_by(slug: id)
query.find_by(slug: id) || query.find_by(id:)
end
}
self.search = {
Expand All @@ -17,19 +17,13 @@ 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 :twitter, as: :text
field :github, as: :text
field :speakerdeck, as: :text
field :mastodon, as: :text, hide_on: :index
field :linkedin, as: :text, hide_on: :index
field :bsky, as: :text, hide_on: :index
field :bio, as: :textarea, hide_on: :index
field :website, as: :text, hide_on: :index
field :slug, as: :text, hide_on: :index
field :talks_count, as: :number, sortable: true
field :canonical, as: :belongs_to, hide_on: :index
# field :suggestions, as: :has_many
# field :speaker_talks, as: :has_many
field :social_profiles, as: :has_many, use_resource: "Avo::Resources::SocialProfile"
field :talks, as: :has_many, use_resource: "Avo::Resources::Talk", attach_scope: -> { query.order(title: :asc) }, searchable: true
end

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/avo/social_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/3.0/controllers.html
class Avo::SocialProfilesController < Avo::ResourcesController
end
7 changes: 7 additions & 0 deletions app/models/concerns/sociable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Sociable
extend ActiveSupport::Concern

included do
has_many :social_profiles, as: :sociable, dependent: :destroy
end
end
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class Event < ApplicationRecord
include Suggestable
include Sluggable
include Sociable
slug_from :name

# associations
Expand Down
47 changes: 47 additions & 0 deletions app/models/social_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# == Schema Information
#
# Table name: social_profiles
#
# id :integer not null, primary key
# provider :integer
# sociable_type :string indexed => [sociable_id]
# value :string
# created_at :datetime not null
# updated_at :datetime not null
# sociable_id :integer indexed => [sociable_type]
#
# Indexes
#
# index_social_profiles_on_sociable (sociable_type,sociable_id)
#
class SocialProfile < ApplicationRecord
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)
end

# 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 :mastodon, with: ->(value) {
return value if value&.match?(URI::DEFAULT_PARSER.make_regexp)
return "" unless value.count("@") == 2

_, handle, instance = value.split("@")

"https://#{instance}/@#{handle}"
}
end
19 changes: 1 addition & 18 deletions app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Speaker < ApplicationRecord
include ActionView::RecordIdentifier
include Sluggable
include Suggestable
include Sociable
include Speaker::Searchable
slug_from :name

Expand Down Expand Up @@ -72,24 +73,6 @@ class Speaker < ApplicationRecord
scope :canonical, -> { where(canonical_id: nil) }
scope :not_canonical, -> { where.not(canonical_id: nil) }

# 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 :bsky, with: ->(value) {
value.gsub(%r{https?://(?:www\.)?(?:x\.com|bsky\.app/profile)/}, "").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 :mastodon, with: ->(value) {
return value if value&.match?(URI::DEFAULT_PARSER.make_regexp)
return "" unless value.count("@") == 2

_, handle, instance = value.split("@")

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

def title
name
end
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20250123120534_create_social_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateSocialProfiles < ActiveRecord::Migration[8.0]
def change
create_table :social_profiles do |t|
t.string :value
t.integer :provider
t.belongs_to :sociable, polymorphic: true
t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

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

27 changes: 27 additions & 0 deletions test/fixtures/social_profiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
# == Schema Information
#
# Table name: social_profiles
#
# id :integer not null, primary key
# provider :integer
# sociable_type :string indexed => [sociable_id]
# value :string
# created_at :datetime not null
# updated_at :datetime not null
# sociable_id :integer indexed => [sociable_type]
#
# Indexes
#
# index_social_profiles_on_sociable (sociable_type,sociable_id)
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/social_profile_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class SocialProfileTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 794c759

Please sign in to comment.