Implement Forem Organization Unified Embed (#16110)
* Complete implementation and add specs * nudge Travis * Clarify forem_domain use * Clarify forem_domain use * More specific check for forem_domain
This commit is contained in:
parent
fef3001bfe
commit
dba225b1be
2 changed files with 20 additions and 2 deletions
|
|
@ -2,10 +2,11 @@ class OrganizationTag < LiquidTagBase
|
|||
include ApplicationHelper
|
||||
include ActionView::Helpers::TagHelper
|
||||
PARTIAL = "organizations/liquid".freeze
|
||||
REGISTRY_REGEXP = %r{#{URL.url}/(?<org_slug>[\w-]+)(?:/)?(?:[\w-]+)?}
|
||||
|
||||
def initialize(_tag_name, organization, _parse_context)
|
||||
super
|
||||
@organization = parse_slug_to_organization(organization.delete(" "))
|
||||
@organization = parse_slug_to_organization(strip_tags(organization))
|
||||
@follow_button = follow_button(@organization)
|
||||
@organization_colors = user_colors(@organization)
|
||||
end
|
||||
|
|
@ -22,7 +23,16 @@ class OrganizationTag < LiquidTagBase
|
|||
end
|
||||
|
||||
def parse_slug_to_organization(organization)
|
||||
organization = Organization.find_by(slug: organization)
|
||||
forem_domain = URL.url
|
||||
if organization.starts_with?(forem_domain)
|
||||
match = pattern_match_for(organization, [REGISTRY_REGEXP])
|
||||
raise StandardError, "Invalid Organization URL" unless match
|
||||
|
||||
organization = Organization.find_by(slug: match[:org_slug])
|
||||
else
|
||||
organization = Organization.find_by(slug: organization)
|
||||
end
|
||||
|
||||
raise StandardError, "Invalid organization slug" if organization.nil?
|
||||
|
||||
organization
|
||||
|
|
@ -31,3 +41,5 @@ end
|
|||
|
||||
Liquid::Template.register_tag("organization", OrganizationTag)
|
||||
Liquid::Template.register_tag("org", OrganizationTag)
|
||||
|
||||
UnifiedEmbed.register(OrganizationTag, regexp: OrganizationTag::REGISTRY_REGEXP)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ RSpec.describe UnifiedEmbed::Registry do
|
|||
let(:comment) do
|
||||
create(:comment, commentable: article, user: user, body_markdown: "TheComment")
|
||||
end
|
||||
let(:organization) { create(:organization) }
|
||||
|
||||
describe ".find_liquid_tag_for" do
|
||||
valid_blogcast_url_formats = [
|
||||
|
|
@ -125,6 +126,11 @@ RSpec.describe UnifiedEmbed::Registry do
|
|||
.to eq(NextTechTag)
|
||||
end
|
||||
|
||||
it "returns OrganizationTag for a Forem organization url" do
|
||||
expect(described_class.find_liquid_tag_for(link: "#{URL.url}/#{organization.slug}"))
|
||||
.to eq(OrganizationTag)
|
||||
end
|
||||
|
||||
it "returns RedditTag for a reddit url" do
|
||||
expect(described_class.find_liquid_tag_for(link: "https://www.reddit.com/r/Cricket/comments/qrkwol/match_thread_2nd_semifinal_australia_vs_pakistan/"))
|
||||
.to eq(RedditTag)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue