Skip to content

Commit

Permalink
/active
Browse files Browse the repository at this point in the history
  • Loading branch information
pushcx committed Feb 20, 2022
1 parent f8ad5f7 commit 6a84f47
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def index
end
end

def active
@stories, show_more = get_from_cache(order: 'max(comments.id) desc') {
paginate stories.active
}

@title = 'Active Discussions'
@cur_url = '/active'

respond_to do |format|
format.html { render action: :index }
format.json { render json: @stories }
end
end

def newest
@stories, @show_more = get_from_cache(newest: true) {
paginate stories.newest
Expand Down
2 changes: 1 addition & 1 deletion app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Story < ApplicationRecord
# Dingbats, emoji, and other graphics https://www.unicode.org/charts/
GRAPHICS_RE = /[\u{0000}-\u{001F}\u{2190}-\u{27BF}\u{1F000}-\u{1F9FF}]/.freeze

attr_accessor :editing_from_suggestions, :editor, :fetching_ip, :is_hidden_by_cur_user,
attr_accessor :editing_from_suggestions, :editor, :fetching_ip, :is_hidden_by_cur_user, :latest_comment_id,
:is_saved_by_cur_user, :moderation_reason, :previewing, :seen_previous, :vote
attr_writer :fetched_response

Expand Down
8 changes: 8 additions & 0 deletions app/models/story_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def newest
Story.base(@user).filter_tags(@params[:exclude_tags] || []).order(id: :desc)
end

def active
Story.base(@user)
.filter_tags(@params[:exclude_tags] || [])
.select('stories.*, (select max(comments.id) from comments where comments.story_id = stories.id) as latest_comment_id')
.where('created_at >= ?', 3.days.ago)
.order('latest_comment_id desc')
end

def newest_by_user(user)
if @user == user
stories = Story.includes(:tags).not_deleted.left_joins(:merged_stories)
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

get "/page/:page" => "home#index"

get "/active" => "home#active"
get "/active/page/:page" => "home#active"
get "/newest" => "home#newest"
get "/newest/page/:page" => "home#newest"
get "/newest/:user" => "home#newest_by_user"
Expand Down
11 changes: 11 additions & 0 deletions spec/models/story_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
let(:submitter) { create(:user) }
let(:repo) { StoryRepository.new(submitter) }

describe '.active' do
it "is ordered by most-recent comment" do
older_story = create(:story)
newer_story = create(:story)
older_comment = create(:comment, story: newer_story)
newer_comment = create(:comment, story: older_story)

expect(repo.active).to eq([newer_comment.story, older_comment.story])
end
end

describe ".newest_by_user" do
context "when user is viewing their own stories" do
before do
Expand Down

0 comments on commit 6a84f47

Please sign in to comment.