Move featured story to separate service (#14810)

* Move featured story to separate service

* Travis, wake up
This commit is contained in:
Michael Kohl 2021-09-29 12:26:14 +07:00 committed by GitHub
parent 647573dcad
commit 43a1c576a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 53 deletions

View file

@ -148,7 +148,7 @@ class StoriesController < ApplicationController
end
def featured_story
@featured_story ||= Articles::Feeds::LargeForemExperimental.find_featured_story(@stories)
@featured_story ||= Articles::Feeds::FindFeaturedStory.call(@stories)
end
def handle_podcast_index

View file

@ -0,0 +1,16 @@
module Articles
module Feeds
module FindFeaturedStory
def self.call(stories)
featured_story =
if stories.is_a?(ActiveRecord::Relation)
stories.where.not(main_image: nil).first
else
stories.detect { |story| story.main_image.present? }
end
featured_story || Article.new
end
end
end
end

View file

@ -11,19 +11,6 @@ module Articles
@experience_level_weight = 1 # default weight for user experience level
end
def self.find_featured_story(stories)
featured_story = if stories.is_a?(ActiveRecord::Relation)
stories.where.not(main_image: nil).first
else
stories.detect { |story| story.main_image.present? }
end
featured_story || Article.new
end
def find_featured_story(stories)
self.class.find_featured_story(stories)
end
def default_home_feed(user_signed_in: false)
_featured_story, stories = default_home_feed_and_featured_story(user_signed_in: user_signed_in, ranking: true)
stories

View file

@ -0,0 +1,25 @@
require "rails_helper"
RSpec.describe Articles::Feeds::FindFeaturedStory, type: :service do
before { create(:article) }
context "when passed an ActiveRecord collection" do
it "returns first article with a main image" do
featured_story = described_class.call(Article.all)
expect(featured_story.main_image).not_to be_nil
end
end
context "when passed an array" do
it "returns first article with a main image" do
featured_story = described_class.call(Article.all.to_a)
expect(featured_story.main_image).to be_present
end
end
context "when passed collection without any articles" do
it "returns an new, empty Article object" do
expect(described_class.call([])).not_to be_persisted
end
end
end

View file

@ -99,7 +99,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
context "when user logged in" do
let(:stories) { feed.default_home_feed(user_signed_in: true) }
it "includes stories " do
it "includes stories" do
expect(stories).to include(old_story)
expect(stories).to include(new_story)
end
@ -252,7 +252,7 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
expect(feed.score_experience_level(article)).to eq(-3)
end
it "returns proper negative when fractional" do
it "returns proper negative when fractional" do
article.experience_level_rating = 8
expect(feed.score_experience_level(article)).to eq(-3.5)
end
@ -389,41 +389,4 @@ RSpec.describe Articles::Feeds::LargeForemExperimental, type: :service do
end
end
end
describe ".find_featured_story" do
let(:featured_story) { described_class.find_featured_story(stories) }
context "when passed an ActiveRecord collection" do
let(:stories) { Article.all }
it "returns first article with a main image" do
expect(featured_story.main_image).not_to be_nil
end
end
context "when passed an array" do
let(:stories) { Article.all.to_a }
it "returns first article with a main image" do
expect(featured_story.main_image).not_to be_nil
end
end
context "when passed collection without any articles" do
let(:stories) { [] }
it "returns an new, empty Article object" do
expect(featured_story.main_image).to be_nil
expect(featured_story.id).to be_nil
end
end
end
describe "#find_featured_story" do
it "calls the class method" do
allow(described_class).to receive(:find_featured_story)
feed.find_featured_story([])
expect(described_class).to have_received(:find_featured_story)
end
end
end