Skip to content

Commit

Permalink
feat: filter users by followed/not followed
Browse files Browse the repository at this point in the history
fix: reduce min-width

horribly ugly on mobile
  • Loading branch information
sean-garwood committed Nov 29, 2024
1 parent ae7ab71 commit 6140f9c
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 58 deletions.
4 changes: 2 additions & 2 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@import "profile";

body {
/* HACK */
/* OPTIMIZE */
overflow-x: hidden;
margin: 0;
}
Expand Down Expand Up @@ -56,7 +56,7 @@ div a {
/* stretch to fill available vh: */
flex: 1;
margin: 10px;
min-width: 500px;
min-width: 250px;
}

.debug {
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
class UsersController < ApplicationController
def index
@users = User.all.includes(:profile).where.not(id: current_user.id)
@users = User.where.not(id: current_user.id).includes(:profile)
@followed_users = current_user.followed_users.unscope(:includes)
@not_followed_users = @users - @followed_users

case params[:filter]
when "followed"
@users = @followed_users
when "not followed"
@users = @not_followed_users
else
@users = @users
end
end
end
11 changes: 5 additions & 6 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module ApplicationHelper
# TODO: load faker::simpsons stuff

LOGGERS = %i[debug info warn error fatal unknown]

def log_error(data, method)
Rails.logger.error { "Failed to #{method}: #{data}" }
Rails.logger.error { "Failed to #{method}: #{data.inspect}" }
end

def my_debugger(**data)
wrapper = "\e[1;31mDEBUG: #{data.expand}\e[0m"
my_logger(wrapper, :debug)
def my_debugger(data)
wrapper = "\e[1;31mDEBUG: #{data.inspect}\e[0m"
my_logger(wrapper)
end

def my_logger(data, method = :debug)
Expand Down
8 changes: 8 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module UsersHelper
AVATAR_SRC_TEMPLATE = "https://www.gravatar.com/avatar/%s?s=%d&d=identicon".freeze

def simpsons_name
Faker::TvShows::Simpsons.character
end

def simpsons_quote
Faker::TvShows::Simpsons.quote
end

def avatar_src_url(user = current_user, size = 80)
email_hash = Digest::SHA256.hexdigest(user.email)
AVATAR_SRC_TEMPLATE % [ email_hash, size ]
Expand Down
9 changes: 9 additions & 0 deletions app/models/concerns/simpsons_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SimpsonsHelper
def simpsons_name
Faker::TvShows::Simpsons.character
end

def simpsons_quote
Faker::TvShows::Simpsons.quote
end
end
21 changes: 5 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class User < ApplicationRecord
include SimpsonsHelper

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

after_create :build_default_profile
after_create -> { create_profile(name: simpsons_name, bio: simpsons_quote) }

delegate :name, to: :profile, allow_nil: true
has_many :comments, dependent: :destroy, inverse_of: :author
Expand Down Expand Up @@ -41,8 +43,6 @@ class User < ApplicationRecord
dependent: :destroy,
inverse_of: :user

scope :not_followed_users, ->(user) { includes(:name).where.not(id: user.followed_users) }

validates_presence_of :email, unique: true

def feed
Expand All @@ -58,22 +58,11 @@ def following?(user)
end


def following_or_sent_pending_request_to?(user) # HACK
def following_or_sent_pending_request_to?(user) # OPTIMIZE
following?(user) || sent_pending_request_to?(user)
end

def has_received_follow_requests? # HACK
FollowRequest.received(self).exists?
end

def sent_pending_request_to?(user) # HACK
def sent_pending_request_to?(user) # OPTIMIZE
pending_sent_follow_requests.where(recipient: user).exists?
end

private
def build_default_profile
self.build_profile(
name: Faker::TvShows::Simpsons.character,
bio: Faker::TvShows::Simpsons.quote)
end
end
10 changes: 10 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="new-comment-container">
<%= form_with model: [@post, Comment.new] do |comment_form| %>
<fieldset>
<legend>Comment on this post
</legend>
<%= comment_form.text_area :body, cols: 40, rows: 12 %>
</fieldset>
<%= comment_form.submit "Comment" %>
<% end %>
</div>
1 change: 0 additions & 1 deletion app/views/follow_requests/_follow.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%# FIXIT: not posting changes %>
<%= form_with(model: FollowRequest.new) do |form| %>
<%= form.hidden_field :recipient_id, value: recipient.id %>
<%= form.hidden_field :sender_id, value: current_user.id %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/follow_requests/_sent_received.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%# HACK: just filter the discover page for followed/unfollowed %>
<%# OPTIMIZE: just filter the discover page for followed/unfollowed %>
<div class="page-header">
<h1><%= action_name.capitalize %> Follow Requests </h1>
<p>All follow requests that you have <%= action_name %> that are pending.</p>
Expand Down
1 change: 0 additions & 1 deletion app/views/follow_requests/_unfollow.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%# FIXIT: not posting changes %>
<%= form_with(model: request, method: :delete) do |form| %>
<%= form.submit "Unfollow" %>
<% end %>
15 changes: 2 additions & 13 deletions app/views/posts/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
<div class="actions-container">
<%= link_to "Edit Post", edit_post_path(@post) %>
<%= link_to "Delete Post", post_path(@post), data: { "turbo-method": :delete }
<%= link_to "Edit", edit_post_path(@post) %>
<%= link_to "Delete", post_path(@post), data: { "turbo-method": :delete }
%>
<%# check if post is in liked_by scope. yes: like, no: unlike %>
<% if current_user.liked_posts.include?(@post) %>
<%= link_to "Unlike", post_like_path(@post), data: { "turbo-method": :delete }
%>
<% else %>
<%= link_to "Like", post_likes_path(@post), data: { "turbo-method": :post } %>
<% end %>
</div>
<div class="new-comment-container">
<%= form_with model: [@post, Comment.new] do |comment_form| %>
<fieldset>
<legend>Comment on this post
</legend>
<%= comment_form.text_area :body, cols: 40, rows: 12 %>
<%= comment_form.submit "Comment" %>
<% end %>
</fieldset>
</div>
21 changes: 9 additions & 12 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,29 @@
</div>
<div class="posts-container">
<% @posts.each do |post| %>
<% Rails.logger.debug { "\e[1;31mPost: #{post.inspect}
#{}\e[0m" }
%>
<% Rails.logger.debug { "\e[1;31mPost author: #{post.author.inspect}
#{}\e[0m" }
%>
<% Rails.logger.debug { "\e[1;31mPost author name: #{post.author.name}
#{}\e[0m" }
%>
<div class="post-container">
<h2 class="post-title"><%= post.title %></h2>
<div class="author-info-container">
<p>By <%= post.author.name %> at <%= post.created_at %></p>
<p>
By <%= post.author.name %>,
<%= time_ago_in_words(post.created_at) %> ago
</p>
<p>
<% if post.updated_at != post.created_at %>
(Edited <%= time_ago_in_words(post.updated_at) %> ago)
<% end %>
</p>
</div>
<div class="post-body-container">
<p><%= post.body %></p>
</div>
<div class="post-links-container">
<%= link_to "View post", post_path(post) %>
<% if post.author.eql?(current_user) %>
<%# TODO: edit post button %>
<%= link_to "Edit post", edit_post_path(post) %>
<%= link_to "Delete post", post_path(post), data: { turbo_method:
:delete, turbo_confirm: "Are you sure?" } %>
<% end %>
<%# TODO: like, comment post button %>
</div>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

<div class="page-header">
<h2><%= @post.title %></h2>
</div>
<div class="post-container">
<%= render partial: 'post' %>
<%= render partial: 'actions' %>
<%= render partial: 'comments/form' %>
<%= render partial: 'comments' %>
</div>
10 changes: 10 additions & 0 deletions app/views/users/_search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%# _search.html.erb %>
<div class="search-container">
<%= form_with method: :get do |search_form| %>
<legend>Filter by:</legend>
<%= search_form.select :filter,
%w[all followed not\ followed],
{ selected: params[:filter] || "not followed" } %>
<%= search_form.submit "Filter" %>
<% end %>
</div>
8 changes: 4 additions & 4 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%# TODO: search form to filter by followed/unfollowed %>
<%# probably change route name from 'discover' %>
<% my_debugger(params) %>
<div class="page-header">
<h1>Discover</h1>
<p>Find new users to follow.</p>
</div>
<%= render partial: 'search' %>
<div class="requests-container">
<% @users.each do |user| %>
<div class="request-container">
Expand All @@ -20,8 +20,8 @@
request: request,
recipient: user } %>
<% else %>
<%= render partial: 'follow_requests/follow', locals: {
recipient: user } %>
<%= render partial: 'follow_requests/follow',
locals: { recipient: user } %>
<% end %>
</div>
</div>
Expand Down

0 comments on commit 6140f9c

Please sign in to comment.