Skip to content

Commit 9916b1a

Browse files
Feature to show recently active badge for developers (joemasilotti#649)
* Implemented code to show recently active badge for developers * Cleaned badge component to only pass title and color attributes * Tests for recently acitve badge for developers * Updated Changelog file * Feedback requested changes for badge color and recently_active method * Make sure badges flow on mobile Co-authored-by: Joe Masilotti <[email protected]>
1 parent 266faac commit 9916b1a

File tree

10 files changed

+88
-7
lines changed

10 files changed

+88
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## September
66

7+
* September 16 - Show "Recently Active" badge for developers active in last 7 days #640 @sarvaiyanidhi
78
* September 15 - Allow rich text for developer bios #638 @rayhanw
89
* September 15 - Show "Featured" badge in search results #642
910
* September 14 - Only ever send one Celebration Promotion email #617 @sarvaiyanidhi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Developers
2+
class BadgeComponent < ApplicationComponent
3+
private attr_reader :title, :color
4+
5+
def initialize(title, color:)
6+
@title = title
7+
@color = color
8+
end
9+
10+
def call
11+
tag.span title, class: class_names("inline-flex items-center rounded-md px-2.5 py-0.5 text-sm font-medium", color_classes)
12+
end
13+
14+
def color_classes
15+
case color.to_sym
16+
when :blue
17+
"bg-blue-100 text-blue-800"
18+
when :green
19+
"bg-green-100 text-green-800"
20+
else
21+
raise "Unknown color: #{color}"
22+
end
23+
end
24+
end
25+
end

app/components/developers/card_component.html.erb

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
</div>
1616
</div>
1717

18-
<% if featured? %>
19-
<div class="mt-1 pb-1">
20-
<span class="inline-flex items-center px-2.5 py-0.5 rounded-md text-sm font-medium bg-blue-100 text-blue-800"> <%= t(".featured") %> </span>
21-
</div>
22-
<% end %>
18+
<div class="mt-3 flex flex-col space-y-4 sm:flex-row sm:space-y-0 sm:space-x-4 items-start">
19+
<% if featured? %>
20+
<%= render Developers::BadgeComponent.new(t(".featured"), color: "blue") %>
21+
<% end %>
22+
<% if recently_active? %>
23+
<%= render Developers::BadgeComponent.new(t(".recently_active"), color: "green") %>
24+
<% end %>
25+
</div>
2326

24-
<div class="mt-2 text-sm sm:text-base text-gray-700 space-y-4">
27+
<div class="mt-3 text-sm sm:text-base text-gray-700 space-y-4">
2528
<p class="line-clamp-3 break-words"><%= developer.plain_text_bio %></p>
2629
</div>
2730
</div>

app/components/developers/card_component.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class CardComponent < ApplicationComponent
33
with_collection_parameter :developer
44

55
delegate :featured?, to: :developer
6+
delegate :recently_active?, to: :developer
67

78
private attr_reader :developer, :highlight_featured
89

app/models/developer.rb

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Developer < ApplicationRecord
88
include PgSearch::Model
99

1010
FEATURE_LENGTH = 1.week
11+
RECENTLY_ACTIVE_LENGTH = 1.week
1112

1213
enum search_status: {
1314
actively_looking: 1,
@@ -98,4 +99,8 @@ def feature!
9899
def featured?
99100
featured_at? && featured_at >= FEATURE_LENGTH.ago
100101
end
102+
103+
def recently_active?
104+
updated_at >= RECENTLY_ACTIVE_LENGTH.ago
105+
end
101106
end

app/models/message.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Message < ApplicationRecord
88
)
99

1010
belongs_to :conversation, touch: true
11-
belongs_to :sender, polymorphic: true
11+
belongs_to :sender, polymorphic: true, touch: true
1212
has_one :developer, through: :conversation
1313
has_one :business, through: :conversation
1414

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ en:
264264
unspecified_html: Currently unavailable
265265
card_component:
266266
featured: Featured developer
267+
recently_active: Recently Active
267268
count_component:
268269
cta: Reset filters
269270
title_html: Showing <span class='font-bold'>%{count}</span> of <span class='font-bold'>%{total}</span>+ developers.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "test_helper"
2+
3+
module Developers
4+
class BadgeComponentTest < ViewComponent::TestCase
5+
setup do
6+
@developer = developers(:one)
7+
end
8+
9+
test "renders recently active badge if the developer profile is active in last 7 days" do
10+
@developer.updated_at = Date.current
11+
render_inline BadgeComponent.new("Recently Active Developer", color: "green")
12+
assert_selector("span[class~='bg-green-100']")
13+
end
14+
15+
test "renders feature badge if the developer profile is features" do
16+
@developer.feature!
17+
render_inline BadgeComponent.new("Featured Developer", color: "blue")
18+
assert_selector("span[class~='bg-blue-100']")
19+
end
20+
end
21+
end

test/components/developers/card_component_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,19 @@ class CardComponentTest < ViewComponent::TestCase
4848
render_inline(CardComponent.new(developer: @developer, highlight_featured: true))
4949
assert_selector "a.border-l-4.border-blue-400"
5050
end
51+
52+
test "renders recently active badge if developer is active in last 7 days" do
53+
@developer.update!(bio: "I am the first developer")
54+
@developer.recently_active?
55+
render_inline(CardComponent.new(developer: @developer))
56+
assert_text I18n.t("developers.card_component.recently_active")
57+
end
58+
59+
test "doesn't render recently active badge if developer is active more than 7 days ago" do
60+
@developer.update!(updated_at: 2.weeks.ago)
61+
@developer.recently_active?
62+
render_inline(CardComponent.new(developer: @developer))
63+
assert_no_text I18n.t("developers.card_component.recently_active")
64+
end
5165
end
5266
end

test/models/developer_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,14 @@ class DeveloperTest < ActiveSupport::TestCase
215215

216216
travel_back
217217
end
218+
219+
test "recently active developers within last one week" do
220+
@developer = developers(:one)
221+
222+
@developer.updated_at = 2.weeks.ago
223+
refute @developer.recently_active?
224+
225+
@developer.updated_at = Date.current
226+
assert @developer.recently_active?
227+
end
218228
end

0 commit comments

Comments
 (0)