Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter user sees channel specific twitter card #191

Merged
merged 5 commits into from
Nov 5, 2017
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
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 assets/static/assets/elixir_twitter_card.png
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 assets/static/assets/git_twitter_card.png
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 assets/static/assets/javascript_twitter_card.png
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 assets/static/assets/react_twitter_card.png
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 assets/static/assets/ruby_twitter_card.png
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 assets/static/assets/sql_twitter_card.png
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 assets/static/assets/vim_twitter_card.png
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 assets/static/assets/workflow_twitter_card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions lib/tilex_web/models/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ defmodule Tilex.Post do
|> String.downcase
end

def twitter_description(post) do
post.body
|> String.split("\n")
|> hd
end

defp add_slug(changeset) do
case get_field(changeset, :slug) do
nil ->
Expand Down
4 changes: 2 additions & 2 deletions lib/tilex_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<meta name="twitter:site" content="@hashrockettil">
<meta name="twitter:creator" content="@hashrockettil">
<meta name="twitter:title" content="Today I Learned: a Hashrocket Project">
<meta name="twitter:description" content="TIL is an open-source project by Hashrocket that exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as select friends of the team. We hope you enjoy learning along with us.">
<meta name="twitter:image" content=<%= TilexWeb.Endpoint.static_url() <> "/assets/til_twittercard.png" %>>
<meta name="twitter:description" content="<%= twitter_description(assigns[:post]) %>">
<meta name="twitter:image" content=<%= twitter_image_url(assigns[:post]) %>>

<link href='//fonts.googleapis.com/css?family=Raleway:700,900' rel='stylesheet' type='text/css'>
<link href='//fonts.googleapis.com/css?family=Lora:400,700italic,700,400italic' rel='stylesheet' type='text/css'>
Expand Down
32 changes: 32 additions & 0 deletions lib/tilex_web/views/layout_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,36 @@ defmodule TilexWeb.LayoutView do
def page_title(%{developer: developer}), do: developer.username
def page_title(%{page_title: page_title}), do: page_title
def page_title(_), do: Application.get_env(:tilex, :organization_name)

def twitter_image_url(%Tilex.Post{} = post) do
channel_name = channel_name(post)

case File.exists?("assets/static/assets/#{channel_name}_twitter_card.png") do
true -> twitter_image_url(channel_name)
false -> twitter_image_url(nil)
end
end
def twitter_image_url(name) when is_binary(name) do
TilexWeb.Endpoint.static_url() <> "/assets/#{name}_twitter_card.png}"
end
def twitter_image_url(name) when is_nil(name) do
TilexWeb.Endpoint.static_url() <> "/assets/til_twittercard.png"
end

defp channel_name(post) do
case Ecto.assoc_loaded?(post.channel) do
true -> String.replace(post.channel.name, "-", "_")
false -> nil
end
end

def twitter_description(%Tilex.Post{} = post) do
Tilex.Post.twitter_description(post)
end

def twitter_description(_post) do
"""
TIL is an open-source project by Hashrocket that exists to catalogue the sharing & accumulation of knowledge as it happens day-to-day. Posts have a 200-word limit, and posting is open to any Rocketeer as well as select friends of the team. We hope you enjoy learning along with us.
"""
end
end
23 changes: 21 additions & 2 deletions test/features/visitor_views_post_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule VisitorViewsPostTest do

test "the page shows a post", %{session: session} do
developer = Factory.insert!(:developer)
channel = Factory.insert!(:channel, name: "awesomeness")
channel = Factory.insert!(:channel, name: "command-line")
post = Factory.insert!(:post,
title: "A special post",
body: "This is how to be super awesome!",
Expand All @@ -22,7 +22,7 @@ defmodule VisitorViewsPostTest do
|> PostShowPage.expect_post_attributes(%{
title: "A special post",
body: "This is how to be super awesome!",
channel: "#awesomeness",
channel: "#command-line",
likes_count: 1
})

Expand Down Expand Up @@ -60,6 +60,25 @@ defmodule VisitorViewsPostTest do
assert url =~ "#{post.slug}-alternate-also-cool-title"
end

test "and sees a channel specific twitter card and a post specific twitter description", %{session: session} do
popular_channel = Factory.insert!(:channel, name: "command-line")
post = Factory.insert!(:post, channel: popular_channel, body: "One sentence that sets up the post.\nAnother sentence that is more informative")

image_url = session
|> visit(post_path(Endpoint, :show, post))
|> find(Query.css("meta[name='twitter:image']", visible: false))
|> Element.attr("content")

assert image_url =~ "command_line_twitter_card.png"

twitter_description = session
|> find(Query.css("meta[name='twitter:description']", visible: false))
|> Element.attr("content")

assert twitter_description =~ "One sentence that sets up the post."
refute twitter_description =~ "Another sentence"
end

test "and clicks 'like' for that post", %{session: session} do
developer = Factory.insert!(:developer)
post = Factory.insert!(:post, title: "A special post", developer: developer, likes: 1)
Expand Down
7 changes: 5 additions & 2 deletions test/support/pages/navigation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ defmodule Tilex.Integration.Pages.Navigation do
use Wallaby.DSL

def ensure_heading(session, text) do
session
|> Browser.find(Query.css("header.site_head div h1", text: text))
heading = session
|> Browser.find(Query.css("header.site_head div h1"))
|> Element.text

ExUnit.Assertions.assert(heading =~ ~r/#{text}/i, "Expected: #{text}, Found: #{heading}")

session
end
Expand Down
13 changes: 9 additions & 4 deletions test/support/pages/post_show_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ defmodule Tilex.Integration.Pages.PostShowPage do
session
|> Browser.find(Query.css(".post .copy", text: expected_body))

session
|> Browser.find(
Query.css(".post aside .post__tag-link", text: String.upcase(expected_channel))
)
channel_name =
session
|> Browser.find(Query.css(".post aside .post__tag-link"))
|> Element.text()

ExUnit.Assertions.assert(
channel_name =~ ~r/#{expected_channel}/i,
"Unable to find text channel #{expected_channel}, instead found #{channel_name}"
)

session
|> Browser.find(Query.css(".js-like-action", text: expected_likes_count))
Expand Down