Skip to content

Commit

Permalink
Add dynamic robots.txt support
Browse files Browse the repository at this point in the history
  • Loading branch information
dlpierce committed Dec 13, 2023
1 parent be17c2d commit 1efdc11
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
43 changes: 43 additions & 0 deletions app/controllers/robots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class RobotsController < ApplicationController
before_action :authenticate_user!, except: :show
before_action :find_robots_txt
before_action :throw_breadcrumbs, except: :show
layout 'hyrax/dashboard'

def show
render body: @robots_txt.value
end

def edit
authorize! :edit, @robots_txt
end

def update
authorize! :update, @robots_txt
respond_to do |format|
if @robots_txt.update(permitted_params)
format.html { redirect_to edit_robots_path, notice: 'robots.txt updated.' }
else
flash.now[:alert] = "robots.txt could not be updated. #{@robots_txt.errors.full_messages}"
format.html { render :edit }
end
end
end

private

def find_robots_txt
@robots_txt = ContentBlock.find_or_create_by(name: 'robots_txt')
end

def throw_breadcrumbs
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
add_breadcrumb t(:'hyrax.admin.sidebar.configuration'), '#'
add_breadcrumb 'robots.txt', edit_robots_path
end

def permitted_params
params.require(:content_block).permit(:value)
end
end
3 changes: 3 additions & 0 deletions app/views/hyrax/dashboard/sidebar/_configuration.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<%= menu.nav_link(hyrax.admin_features_path) do %>
<span class="fa fa-wrench" aria-hidden="true"></span> <span class="sidebar-action-text"><%= t('hyrax.admin.sidebar.technical') %></span>
<% end %>
<%= menu.nav_link(main_app.edit_robots_path) do %>
<span class="fa fa-sitemap" aria-hidden="true"></span> <span class="sidebar-action-text">robots.txt</span>
<% end %>
<% end %>
<% end %>
</li>
Expand Down
20 changes: 20 additions & 0 deletions app/views/robots/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="panel panel-default tabs">
<div class="tab-content">
<div id="about" class="tab-pane active">
<div class="panel panel-default labels">
<%= simple_form_for @robots_txt, url: robots_path do |f| %>
<div class="panel-body">
<div class="field form-group">
<%= f.label :value, 'robots.txt' %><br />
<%= f.text_area :value, value: f.object.value, class: 'form-control', rows: 20, cols: 120 %>
</div>
</div>
<div class="panel-footer">
<%= link_to t(:'hyrax.pages.cancel'), hyrax.dashboard_path, class: 'btn btn-default pull-right' %>
<%= f.button :submit, class: 'btn btn-primary pull-right' %>
</div>
<% end %>
</div>
</div>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/robots/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% content_for :page_header do %>
<h1><span class="fa fa-sitemap" aria-hidden="true"></span> Editing robots.txt</h1>
<% end %>

<div class="row">
<div class="col-md-12">
<%= render 'form' %>
</div>
</div>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
get '/purl/formats/:id', to: 'purl#formats', as: 'formats_purl'
get '/purl/*id', to: 'purl#default', as: 'default_purl'

# robots.txt
resource :robots, only: [:show, :edit, :update]

# Send ActionController::RoutingError to 404 page
# Must be the last route defined
match '*unmatched', to: 'application#rescue_404', via: :all
Expand Down
1 change: 0 additions & 1 deletion public/robots.txt

This file was deleted.

79 changes: 79 additions & 0 deletions spec/controllers/robots_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'rails_helper'

describe RobotsController do
let(:user) { create(:user) }
let(:admin) { create(:admin) }
let(:robots_txt) { ContentBlock.create(name: 'robots_txt', value: content) }
let(:content) { "User-Agent: *\nDisallow: /concern" }

after do
ContentBlock.delete('robots_txt')
end

describe '#show' do
it 'is blank by default' do
get :show
expect(response).to be_successful
expect(response.body).to eq ''
end

it 'renders the value' do
robots_txt
get :show
expect(response).to be_successful
expect(response.body).to eq content
end

it 'is route for /robots.txt', type: :routing do
expect(get: '/robots.txt').to route_to(controller: 'robots', action: 'show', format: 'txt')
end
end

describe '#edit' do
it 'is unavailable to the public' do
get :edit
expect(response).to redirect_to(new_user_session_path(locale: nil))
end

it 'is unavailable to regular users' do
sign_in user
get :edit
expect(response).to be_unauthorized
end

context 'with rendering' do
render_views
it 'is rendered for admins' do
robots_txt
sign_in admin
get :edit
expect(response).to be_successful
expect(response.body).to include(content)
end
end
end

describe '#update' do
let(:new_content) { 'Disallow: *' }

it 'is unavailable to the public' do
patch :update, params: { content_block: { value: new_content } }
expect(response).to redirect_to(new_user_session_path(locale: nil))
end

it 'is unavailable to regular users' do
sign_in user
patch :update, params: { content_block: { value: new_content } }
expect(response).to be_unauthorized
end

it 'is updated for admins' do
robots_txt
sign_in admin
patch :update, params: { content_block: { value: new_content } }
expect(response).to redirect_to(edit_robots_path)
get :show
expect(response.body).to eq new_content
end
end
end

0 comments on commit 1efdc11

Please sign in to comment.