Skip to content

Commit

Permalink
Allow users to configure the news feed order (#1172)
Browse files Browse the repository at this point in the history
I prefer to catch up with the older stories first and it always felt awkward going through the feed in reverse. So I always had a local patch to flip the order, decided to to push a proper solution upstream since I changed my deployment just now. Thanks for picking up the project btw!
  • Loading branch information
jhass authored Mar 18, 2024
1 parent 43f5dd9 commit f983dbb
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def update
private

def user_params
params.require(:user).permit(:username, :password_challenge)
params.require(:user).permit(:username, :password_challenge, :stories_order)
end
end
3 changes: 2 additions & 1 deletion app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class StoriesController < ApplicationController
def index
@unread_stories = authorization.scope(StoryRepository.unread)
order = current_user.stories_order
@unread_stories = authorization.scope(StoryRepository.unread(order:))
end

def update
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class User < ApplicationRecord

before_save :update_api_key

enum :stories_order, { desc: "desc", asc: "asc" }, prefix: true

attr_accessor :password_challenge

# `password_challenge` logic should be able to be removed in Rails 7.1
Expand Down
4 changes: 2 additions & 2 deletions app/repositories/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def self.exists?(id, feed_id)
Story.exists?(entry_id: id, feed_id:)
end

def self.unread
Story.where(is_read: false).order("published desc").includes(:feed)
def self.unread(order: "desc")
Story.where(is_read: false).order("published #{order}").includes(:feed)
end

def self.unread_since_id(since_id)
Expand Down
14 changes: 11 additions & 3 deletions app/views/profiles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<h1><%= t('.title') %></h1>

<header class="warning">
<p><%= t('.warning_html') %></p>
</header>
<%= form_with(model: user, url: profile_path) do |form| %>
<fieldset>
<legend><%= t(".stories_feed_settings") %></legend>
<%= form.label :stories_order %>
<%= form.select :stories_order, User.stories_orders.transform_keys {|k| User.human_attribute_name("stories_order.#{k}") } %>
</fieldset>
<%= form.submit("Update") %>
<% end %>

<%= form_with(model: user, url: profile_path) do |form| %>
<fieldset>
<legend><%= t('.change_username') %></legend>
<div class="warning">
<p><%= t('.warning_html') %></p>
</div>
<%= form.label :username %>
<%= form.text_field :username, required: true %>
<%= form.label :password_challenge, "Existing password" %>
Expand Down
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,10 @@ en:
title: 'Stringer is '
your_feeds: your feeds
your_stories: your stories
activerecord:
attributes:
user:
stories_order: "Stories feed order"
user/stories_order:
asc: "Oldest first"
desc: "Newest first"
7 changes: 7 additions & 0 deletions db/migrate/20240316211109_add_stories_order_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddStoriesOrderToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :stories_order, :string, default: "desc"
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spec/repositories/story_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def create_feed(url: "http://blog.golang.org/feed.atom")

expect(described_class.unread).to be_empty
end

it "allows to override the order" do
story1 = create(:story, :unread, published: 5.minutes.ago)
story2 = create(:story, :unread, published: 4.minutes.ago)

expect(described_class.unread(order: :asc)).to eq([story1, story2])
end
end

describe ".unread_since_id" do
Expand Down
14 changes: 10 additions & 4 deletions spec/system/profile_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

RSpec.describe "profile page" do
it "allows the user to edit their username" do
before do
login_as(default_user)
visit(edit_profile_path)
end

it "allows the user to edit their username" do
fill_in_username_fields(default_user.password)
click_on("Update username")

Expand All @@ -19,9 +21,6 @@ def fill_in_username_fields(existing_password)
end

it "allows the user to edit their password" do
login_as(default_user)
visit(edit_profile_path)

fill_in_password_fields(default_user.password, "new_password")
click_on("Update password")

Expand All @@ -35,4 +34,11 @@ def fill_in_password_fields(existing_password, new_password)
fill_in("Password confirmation", with: new_password)
end
end

it "allows the user to edit their feed order" do
select("Oldest first", from: "Stories feed order")
click_on("Update")

expect(default_user.reload).to be_stories_order_asc
end
end

0 comments on commit f983dbb

Please sign in to comment.