Add internal tool to preview feed configs (#18544)

* Add internal tool to preview feed configs

* Update app/services/articles/feeds/variant_query.rb
This commit is contained in:
Ben Halpern 2022-10-06 07:48:44 -04:00 committed by GitHub
parent 9d755a9d2f
commit f70bc2b0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 3 deletions

View file

@ -22,6 +22,27 @@ module Admin
redirect_to admin_tools_path
end
def feed_playground
return if params[:config].blank?
begin
config_json = JSON.parse(params[:config])
config = Articles::Feeds::VariantAssembler
.build_with(catalog: Articles::Feeds.lever_catalog, config: config_json, variant: "test")
@user = User.find_by(username: params[:username]) || current_user
@feed = Articles::Feeds::VariantQuery.new(
config: config,
user: @user,
number_of_articles: params[:number_of_articles] || 25,
page: 1,
tag: nil,
)
@articles = @feed.more_comments_minimal_weight_randomized.to_a
rescue KeyError, JSON::ParserError, ActiveRecord::StatementInvalid => e
flash[:danger] = e.message
end
end
private
def handle_dead_path

View file

@ -88,7 +88,6 @@ module Articles
reseed_randomizer_on_each_request: config.fetch("reseed_randomizer_on_each_request"),
)
end
private_class_method :build_with
end
end
end

View file

@ -25,8 +25,6 @@ module Articles
new(config: config, **kwargs)
end
# Let's make sure that folks initialize this with a variant configuration.
private_class_method :new
Config = Struct.new(
:variant,

View file

@ -0,0 +1,33 @@
<h1>Feed Playground</h1>
<div class="crayons-card p-6 my-6">
<%= form_tag feed_playground_admin_tools_path do %>
<div class="form-group">
<%= label_tag :config %>
<%= text_area_tag :config, params[:config], placeholder: '{"description": ..., "order_by": ..., etc.}', class: "block w-100 mb-4 rounded", style: "height: 300px" %>
<%= label_tag :username, "User" %>
<%= text_field_tag :username, params[:username] || current_user.username, placeholder: "sloan" %>
<%= label_tag :number_of_articles %>
<%= text_field_tag :number_of_articles, params[:number_of_articles] || 25, placeholder: "25" %>
<%= submit_tag("Preview feed") %>
</div>
<% end %>
</div>
<% if @user && @articles %>
<h1>Feed Preview for <a href="/<%= @user.username %>">@<%= @user.username %></a></h1>
<div>
<% Follow.follower_tag(@user.id).includes(:followable).order("points DESC").each do |follow| %>
<a href="/t/<%= follow.followable.name %>" class="crayons-tag">
<span class="crayons-tag__prefix">#</span><%= follow.followable.name %>: <%= follow.points.round(3) %>
</a>
<% end %>
</div>
<div class="articles-list mt-4" id="articles-list">
<div class="substories" id="substories">
<% @articles.each do |article| %>
<%= render "articles/single_story", story: article.decorate, featured: article.main_image.present? %>
<% end %>
</div>
</div>
<% end %>

View file

@ -132,6 +132,8 @@ namespace :admin do
resources :tools, only: %i[index create] do
collection do
post "bust_cache"
get "feed_playground"
post "feed_playground"
end
end

View file

@ -0,0 +1,78 @@
require "rails_helper"
RSpec.describe "/admin/advanced/tools/feed_playground", type: :request do
describe "GET /admin/advanced/tools/feed_playground" do
context "when the user is not an admin" do
let(:user) { create(:user) }
before do
sign_in user
end
it "blocks the request" do
expect do
get feed_playground_admin_tools_path
end.to raise_error(Pundit::NotAuthorizedError)
end
end
context "when the user is a super admin" do
let(:super_admin) { create(:user, :super_admin) }
before do
sign_in super_admin
get feed_playground_admin_tools_path
end
it "allows the request" do
expect(response).to have_http_status(:ok)
end
end
context "when the user is a single resource admin" do
let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Tool) }
before do
sign_in single_resource_admin
get feed_playground_admin_tools_path
end
it "allows the request" do
expect(response).to have_http_status(:ok)
end
end
context "when the user is the wrong single resource admin" do
let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Article) }
before do
sign_in single_resource_admin
end
it "blocks the request" do
expect do
get feed_playground_admin_tools_path
end.to raise_error(Pundit::NotAuthorizedError)
end
end
end
describe "POST /admin/advanced/tools/feed_playground" do
context "when the user is a super admin" do
let(:super_admin) { create(:user, :super_admin) }
before do
sign_in super_admin
post feed_playground_admin_tools_path, params: { config: "sddsdsdsdsds" }
end
it "allows the request" do
expect(response).to have_http_status(:ok)
end
it "shows proper error" do
expect(response.body).to include("unexpected token at")
end
end
end
end