Skip to content

Commit 2db6988

Browse files
author
Lewis Youl
committed
feature/add popular snippets
1 parent 5199135 commit 2db6988

File tree

7 files changed

+45
-13
lines changed

7 files changed

+45
-13
lines changed

app/controllers/application_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
33
serialization_scope :view_context
44
before_action :configure_permitted_parameters, if: :devise_controller?
55
before_action :set_toast_message, if: -> { params[:notice].present? || params[:alert].present? }
6-
before_action :assign_users_for_connect
6+
before_action :assign_users_for_connect, :assign_popular_snippets
77

88
UNAUTHORIZED = 'You do not have access to this page'.freeze
99

@@ -38,4 +38,8 @@ def assign_users_for_connect
3838

3939
@users_for_connect = user_base.order(Arel.sql('RANDOM()')).limit(3)
4040
end
41+
42+
def assign_popular_snippets
43+
@popular_snippets = Snippet.popular.limit(3)
44+
end
4145
end

app/models/snippet.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Snippet < ApplicationRecord
1414

1515
scope :public_snippets, -> { where(public: true) }
1616
scope :private_snippets, -> { where(public: false) }
17+
scope :popular, -> { order(likes_count: :desc) }
1718

1819
def folder_for_user(user)
1920
folders.find_by(user_id: user&.id)

app/views/shared/_sidebar.html.erb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,33 @@
1616

1717
<%= render partial: 'shared/connect' %>
1818

19-
19+
<%= render 'shared/cards/card_no_padding' do %>
20+
<h4 class="text-center pt-2">Popular</h4>
21+
22+
<ul class="divide-y divide-gray-200" data-infinite-scroll-window-target="entries" data-action="scroll@window->infinite-scroll-window#scroll">
23+
<% @popular_snippets.each do |snippet| %>
24+
<li
25+
class="cursor-pointer hover:bg-gray-50 group transition pt-4 pb-4 px-4"
26+
data-controller="navigation"
27+
data-navigation-url-value="<%= snippet_path(snippet) %>"
28+
data-action="click->navigation#navigate"
29+
>
30+
<div class="flex justify-between">
31+
<div class="flex">
32+
<%= render partial: 'shared/icons/heart', locals: { width: 5, height: 5, color: 'text-red-500' } %>
33+
<% if snippet.likes? %>
34+
<span class="inline-block text-sm text-gray-500 ml-1 -mb-0.5"><%= snippet.likes_count %></span>
35+
<% end %>
36+
</div>
37+
<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-yellow-100 text-yellow-800">
38+
<%= snippet.language.name %>
39+
</span>
40+
</div>
41+
<p class="mt-2 font-normal group-hover:underline"><%= snippet.description %></p>
42+
</li>
43+
<% end %>
44+
</ul>
45+
<% end %>
2046

2147
</div>
2248
</aside>

app/views/snippets/_snippet.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<% if snippet.private? %>
1111
<%= render partial: 'shared/icons/lock_closed', locals: { height: 4, width: 4, color: 'text-yellow-500 mr-1' } %>
1212
<% end %>
13-
<div class="rounded-sm bg-gray-100 px-2 py-1 text-gray-500">
14-
<span class="block text-xs"><%= snippet.language.name %></span>
15-
</div>
13+
<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-yellow-100 text-yellow-800">
14+
<%= snippet.language.name %>
15+
</span>
1616
<% if @display_popover %>
1717
<%= render partial: 'shared/popover_trigger', locals: { popover_path: popover_snippet_path(snippet) } %>
1818
<% end %>

app/views/snippets/_snippet_preview.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<% if snippet.private? %>
1010
<%= render partial: 'shared/icons/lock_closed', locals: { height: 4, width: 4, color: 'text-yellow-500 mr-1' } %>
1111
<% end %>
12-
<div class="rounded-sm bg-gray-100 px-2 py-1 text-gray-500">
13-
<span class="block text-xs"><%= snippet.language.name %></span>
14-
</div>
12+
<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-yellow-100 text-yellow-800">
13+
<%= snippet.language.name %>
14+
</span>
1515
<% if @display_popover %>
1616
<%= render partial: 'shared/popover_trigger', locals: { popover_path: popover_snippet_path(snippet) } %>
1717
<% end %>

app/views/users/_connect.html.erb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<li class="cursor-pointer hover:bg-gray-50 transition pt-4 pb-4 px-4"
2-
data-controller="navigation"
3-
data-navigation-url-value="<%= user_path(user) %>"
4-
data-action="click->navigation#navigate"
5-
>
1+
<li
2+
class="group cursor-pointer hover:bg-gray-50 transition pt-4 pb-4 px-4"
3+
data-controller="navigation"
4+
data-navigation-url-value="<%= user_path(user) %>"
5+
data-action="click->navigation#navigate"
6+
>
67
<div class="relative">
78
<div class="relative flex items-start space-x-3">
89
<%= render partial: 'notifications/user_avatar', locals: { user: user } %>

public/example.png

-103 KB
Loading

0 commit comments

Comments
 (0)