tweak new domain controller - copy, var names, spec

This commit is contained in:
Peter Bhat Harkins 2022-09-01 07:36:59 -05:00
parent 9a7ad7db6f
commit b731305b94
12 changed files with 132 additions and 125 deletions

View File

@ -1,38 +1,32 @@
class DomainsController < ApplicationController
before_action :require_logged_in_admin
before_action :set_domain, only: [:update, :edit, :unban]
before_action :set_reason, only: [:update, :unban]
before_action :find_domain, only: [:edit, :update]
def edit; end
def update
if @banned_reason.present?
@domain.ban_by_user_for_reason!(@user, @banned_reason)
if domain_params[:banned_reason].present?
if @domain.banned?
@domain.unban_by_user_for_reason!(@user, domain_params[:banned_reason])
else
@domain.ban_by_user_for_reason!(@user, domain_params[:banned_reason])
end
flash[:success] = "Domain updated."
redirect_to domain_path(@domain)
else
flash[:error] = "You must give a reason for the ban"
flash.now[:error] = "Reason required for the modlog."
render :edit
end
redirect_to edit_domain_path(name: @domain.domain)
end
def unban
if @banned_reason.present?
@domain.unban_by_user_for_reason!(@user, @banned_reason)
else
flash[:error] = "You must give a reason for the unban"
end
redirect_to edit_domain_path(name: @domain.domain)
end
private
def set_domain
@domain = Domain.find_by(domain: params[:name])
def domain_params
params.require(:domain).permit(:banned_reason)
end
def set_reason
@banned_reason = params.dig('domain', 'banned_reason')
def find_domain
@domain = Domain.find_by(domain: params[:id])
end
def path_of_form(domain)

View File

@ -236,7 +236,7 @@ class HomeController < ApplicationController
end
def for_domain
@domain = Domain.find_by!(domain: params[:name])
@domain = Domain.find_by!(domain: params[:id])
@stories, @show_more = get_from_cache(domain: @domain.domain) do
paginate @domain.stories.base(@user).order('id desc')

View File

@ -26,10 +26,10 @@
</span>
<% end %>
<%= form_with model: @domain, method: :post do |f| %>
<%= form_with model: @domain do |f| %>
<div class="boxline">
<%= f.label :banned_reason, "#{caption_of_button(@domain)} Reason:", class: 'required' %>
<%= f.text_field :banned_reason %>
<%= f.text_field :banned_reason, value: '' %>
</div>
<div class="boxline">

View File

@ -2,8 +2,11 @@
<%= @domain.domain %>:
<%= "#{@domain.stories.count} story".pluralize(@domain.stories.count) %> from
<%= "#{@domain.n_submitters} submitter".pluralize(@domain.n_submitters) %>
<%= link_to 'Edit', edit_domain_path(@domain) if @user.try(:is_admin?) %>
</p>
<% if @domain.banned? %>
<p>
Banned

View File

@ -69,15 +69,13 @@ Rails.application.routes.draw do
get "/t/:tag" => "home#multi_tag", :as => "multi_tag"
get "/t/:tag/page/:page" => "home#tagged"
dm_con = { :name => /([^\/]+?)(?=\.json|\.rss|$|\/)/ }
get "/domain/:name(.:format)" => "home#for_domain", :as => "domain",
:constraints => dm_con
get "/domain/:name/page/:page" => "home#for_domain", :constraints => { name: /[^\/]+/ }
get "/domains/:name" => "domains#edit", :as => "edit_domain", :constraints => dm_con
post "/domains/:name/unban" => "domains#unban", :as => "unban_domain", :constraints => dm_con
post "/domains/:name" => "domains#update", :as => "update_domain", :constraints => dm_con
constraints :id => /([^\/]+?)(?=\.json|\.rss|$|\/)/ do
get "/domain/:id(.:format)", to: redirect('/domains/%{id}')
get "/domain/:id/page/:page", to: redirect('/domains/%{id}/page/%{page}')
get "/domains/:id(.:format)" => "home#for_domain", :as => "domain"
get "/domains/:id/page/:page" => "home#for_domain"
resources :domains, only: [:edit, :update]
end
get "/search" => "search#index"
get "/search/:q" => "search#index"

View File

@ -10,17 +10,17 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
ActiveRecord::Schema[7.0].define(version: 2022_08_06_200248) do
create_table "categories", charset: "utf8mb4", force: :cascade do |t|
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["category"], name: "index_categories_on_category", unique: true
end
create_table "comments", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil
t.string "short_id", limit: 10, default: "", null: false
t.bigint "story_id", null: false, unsigned: true
t.bigint "user_id", null: false, unsigned: true
@ -48,16 +48,16 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
create_table "domains", charset: "utf8mb4", force: :cascade do |t|
t.string "domain"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "banned_at"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.datetime "banned_at", precision: nil
t.integer "banned_by_user_id"
t.string "banned_reason", limit: 200
end
create_table "hat_requests", id: { type: :bigint, unsigned: true }, charset: "utf8mb3", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.bigint "user_id", null: false, unsigned: true
t.string "hat", null: false
t.string "link", null: false
@ -66,14 +66,14 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
end
create_table "hats", id: { type: :bigint, unsigned: true }, charset: "utf8mb3", force: :cascade do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.bigint "user_id", null: false, unsigned: true
t.bigint "granted_by_user_id", null: false, unsigned: true
t.string "hat", null: false
t.string "link", collation: "utf8mb4_general_ci"
t.boolean "modlog_use", default: false
t.datetime "doffed_at"
t.datetime "doffed_at", precision: nil
t.index ["granted_by_user_id"], name: "hats_granted_by_user_id_fk"
t.index ["user_id"], name: "hats_user_id_fk"
end
@ -92,18 +92,18 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.string "name", null: false
t.text "memo"
t.string "ip_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
end
create_table "invitations", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.bigint "user_id", null: false, unsigned: true
t.string "email"
t.string "code"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.text "memo", size: :medium
t.datetime "used_at"
t.datetime "used_at", precision: nil
t.bigint "new_user_id", unsigned: true
t.index ["new_user_id"], name: "invitations_new_user_id_fk"
t.index ["user_id"], name: "invitations_user_id_fk"
@ -116,7 +116,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
end
create_table "messages", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.datetime "created_at"
t.datetime "created_at", precision: nil
t.bigint "author_user_id", unsigned: true
t.bigint "recipient_user_id", null: false, unsigned: true
t.boolean "has_been_read", default: false
@ -136,15 +136,15 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.bigint "user_id", null: false, unsigned: true
t.text "note", null: false
t.text "markeddown_note", null: false
t.datetime "created_at", null: false
t.datetime "created_at", precision: nil, null: false
t.index ["id", "user_id"], name: "index_mod_notes_on_id_and_user_id"
t.index ["moderator_user_id"], name: "mod_notes_moderator_user_id_fk"
t.index ["user_id"], name: "mod_notes_user_id_fk"
end
create_table "moderations", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "moderator_user_id", unsigned: true
t.bigint "story_id", unsigned: true
t.bigint "comment_id", unsigned: true
@ -166,8 +166,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
create_table "read_ribbons", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.boolean "is_following", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "user_id", null: false, unsigned: true
t.bigint "story_id", null: false, unsigned: true
t.index ["story_id"], name: "index_read_ribbons_on_story_id"
@ -175,8 +175,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
end
create_table "saved_stories", id: { type: :bigint, unsigned: true }, charset: "utf8mb3", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "user_id", null: false, unsigned: true
t.bigint "story_id", null: false, unsigned: true
t.index ["story_id"], name: "saved_stories_story_id_fk"
@ -184,7 +184,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
end
create_table "stories", id: { type: :bigint, unsigned: true }, charset: "utf8mb4", force: :cascade do |t|
t.datetime "created_at"
t.datetime "created_at", precision: nil
t.bigint "user_id", null: false, unsigned: true
t.string "url", limit: 250, default: ""
t.string "title", limit: 150, default: "", null: false
@ -196,10 +196,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.boolean "is_moderated", default: false, null: false
t.decimal "hotness", precision: 20, scale: 10, default: "0.0", null: false
t.text "markeddown_description", size: :medium
t.text "story_cache", size: :medium
t.integer "comments_count", default: 0, null: false
t.bigint "merged_story_id", unsigned: true
t.datetime "unavailable_at"
t.datetime "unavailable_at", precision: nil
t.string "twitter_id", limit: 20
t.boolean "user_is_author", default: false
t.boolean "user_is_following", default: false, null: false
@ -220,7 +219,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
create_table "story_texts", charset: "utf8mb4", force: :cascade do |t|
t.text "body", size: :medium, null: false
t.timestamp "created_at", default: -> { "current_timestamp()" }, null: false
t.timestamp "created_at", default: -> { "current_timestamp() ON UPDATE current_timestamp()" }, null: false
t.index ["body"], name: "index_story_texts_on_body", type: :fulltext
end
@ -242,8 +241,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
end
create_table "tag_filters", id: { type: :bigint, unsigned: true }, charset: "utf8mb3", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.bigint "user_id", null: false, unsigned: true
t.bigint "tag_id", null: false, unsigned: true
t.index ["tag_id"], name: "tag_filters_tag_id_fk"
@ -274,7 +273,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.string "username", limit: 50, collation: "utf8mb4_general_ci"
t.string "email", limit: 100, collation: "utf8mb4_general_ci"
t.string "password_digest", limit: 75, collation: "utf8mb4_general_ci"
t.datetime "created_at"
t.datetime "created_at", precision: nil
t.boolean "is_admin", default: false
t.string "password_reset_token", limit: 75, collation: "utf8mb4_general_ci"
t.string "session_token", limit: 75, default: "", null: false, collation: "utf8mb4_general_ci"
@ -286,11 +285,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.string "mailing_list_token", limit: 75, collation: "utf8mb4_general_ci"
t.integer "mailing_list_mode", default: 0
t.integer "karma", default: 0, null: false
t.datetime "banned_at"
t.datetime "banned_at", precision: nil
t.bigint "banned_by_user_id", unsigned: true
t.string "banned_reason", limit: 200, collation: "utf8mb4_general_ci"
t.datetime "deleted_at"
t.datetime "disabled_invite_at"
t.datetime "deleted_at", precision: nil
t.datetime "disabled_invite_at", precision: nil
t.bigint "disabled_invite_by_user_id", unsigned: true
t.string "disabled_invite_reason", limit: 200
t.text "settings"
@ -312,7 +311,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_08_06_202153) do
t.bigint "comment_id", unsigned: true
t.integer "vote", limit: 1, null: false
t.string "reason", limit: 1
t.datetime "updated_at", null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["comment_id"], name: "index_votes_on_comment_id"
t.index ["story_id"], name: "votes_story_id_fk"
t.index ["user_id", "comment_id"], name: "user_id_comment_id"

View File

@ -7,7 +7,7 @@ describe HomeController do
describe "#for_domain" do
it 'returns stories for a domain' do
get :for_domain, params: { name: story.domain.domain }
get :for_domain, params: { id: story.domain.domain }
expect(response).to be_successful
expect(@controller.view_assigns['title']).to include(story.domain.domain)

View File

@ -1,5 +1,11 @@
FactoryBot.define do
factory :domain do
sequence(:domain) {|n| "example-#{n}.local" }
trait(:banned) do
banned_by_user { association(:user) }
banned_at { Time.current }
banned_reason { "some reason" }
end
end
end

View File

@ -0,0 +1,47 @@
require 'rails_helper'
RSpec.feature "Domains" do
let(:admin) { create(:user, :admin) }
before(:each) { stub_login_as admin }
context 'update' do
let(:domain) { create(:domain) }
it 'bans domain with valid params' do
visit "/domains/#{domain.domain}/edit"
fill_in 'Reason', with: 'just because'
click_on 'Ban'
expect(page).to have_text('Domain updated')
expect(domain.reload).to be_banned
end
it 'does not ban domain when the reason is blank' do
visit "/domains/#{domain.domain}/edit"
fill_in 'Reason', with: ''
click_on 'Ban'
expect(page).to have_text('Reason required')
expect(domain.reload).to_not be_banned
end
end
context 'unban' do
let(:domain) { create(:domain, :banned) }
it 'unbans domain with valid params' do
visit "/domains/#{domain.domain}/edit"
fill_in 'Reason', with: 'am i not merciful'
click_on 'Unban'
expect(page).to have_text('Domain updated')
expect(domain.reload).to_not be_banned
end
it 'unbans domain when the reason is blank' do
visit "/domains/#{domain.domain}/edit"
fill_in 'Reason', with: ''
click_on 'Unban'
expect(page).to have_text('Reason required')
expect(domain.reload).to be_banned
end
end
end

View File

@ -1,48 +0,0 @@
require 'rails_helper'
RSpec.describe "Domains", type: :request do
let(:user) { create(:user) }
before do
sign_in user
allow_any_instance_of(DomainsController).to receive(:require_logged_in_admin)
end
context 'update' do
let(:domain) { create(:domain) }
it 'bans domain with valid params' do
messg = 'Banned with reason'
expect_any_instance_of(Domain).to receive(:ban_by_user_for_reason!).once.with(user, messg)
post "/domains/#{domain.domain}", params: { domain: { banned_reason: messg } }
expect(response).to redirect_to edit_domain_path
end
it 'bans domain when the reason is blank' do
expect_any_instance_of(Domain).not_to receive(:ban_by_user_for_reason!)
post "/domains/#{domain.domain}", params: { domain: { banned_reason: '' } }
expect(response).to redirect_to edit_domain_path
end
end
context 'unban' do
let(:domain) { create(:domain) }
it 'unbans domain with valid params' do
messg = 'Unbanned with reason'
expect_any_instance_of(Domain).to receive(:unban_by_user_for_reason!).once.with(user, messg)
post "/domains/#{domain.domain}/unban", params: { domain: { banned_reason: messg } }
expect(response).to redirect_to edit_domain_path
end
it 'unbans domain when the reason is blank' do
expect_any_instance_of(Domain).not_to receive(:unban_by_user_for_reason!)
post "/domains/#{domain.domain}/unban", params: { domain: { banned_reason: '' } }
expect(response).to redirect_to edit_domain_path
end
end
end

View File

@ -13,15 +13,23 @@ describe 'home', type: :request do
describe "#for_domain" do
it 'returns 404 for non-existent domain' do
expect { get '/domain/unseen.domain' }.to raise_error(ActiveRecord::RecordNotFound)
expect { get '/domains/unseen.domain' }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'returns 200 for existing domains' do
story = create(:story)
get "/domain/#{story.domain.domain}"
get "/domains/#{story.domain.domain}"
expect(response).to be_successful
expect(response.body).to include(story.title)
end
it 'redirects old singular routes' do
get "/domain/example.com"
expect(response).to redirect_to('/domains/example.com')
get "/domain/example.com/page/2"
expect(response).to redirect_to('/domains/example.com/page/2')
end
end
end

View File

@ -3,15 +3,15 @@ require 'rails_helper'
describe 'home routing' do
it 'routes a domain' do
assert_recognizes(
{ controller: 'home', action: 'for_domain', name: 'example.com' },
'/domain/example.com'
{ controller: 'home', action: 'for_domain', id: 'example.com' },
'/domains/example.com'
)
end
it 'routes a domain rss feed' do
assert_recognizes(
{ controller: 'home', action: 'for_domain', name: 'example.com', format: 'rss' },
'/domain/example.com.rss'
{ controller: 'home', action: 'for_domain', id: 'example.com', format: 'rss' },
'/domains/example.com.rss'
)
end
end