Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request Growstuff#740 from yez/feature/robots-dissalow-on-…
Browse files Browse the repository at this point in the history
…staging

Feature/robots dissalow on staging
  • Loading branch information
Skud committed May 19, 2015
2 parents e04f78d + c1ab161 commit c47a1bc
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ submit the change with your pull request.
- Rocky Jaiswal / [rocky-jaiswal](https://github.com/rocky-jaiswal)
- Robert Landreaux / [robertlandreaux](https://github.com/robertlandreaux)
- Savant Krishna / [sksavant](https://github.com/sksavant)
- Jake Yesbeck / [yez](https://github.com/yez)

20 changes: 20 additions & 0 deletions app/controllers/robots_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class RobotsController < ApplicationController

DEFAULT_FILENAME = 'config/robots.txt'.freeze

def robots
filename = if subdomain && subdomain != 'www'
"config/robots.#{ subdomain }.txt"
end

file_to_render = File.exists?(filename.to_s) ? filename : DEFAULT_FILENAME

render file: file_to_render, layout: false, content_type: 'text/plain'
end

private

def subdomain
request.subdomain.present? ? request.subdomain : nil
end
end
2 changes: 2 additions & 0 deletions config/robots.staging.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-Agent: *
Disallow: /
Empty file added config/robots.txt
Empty file.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Growstuff::Application.routes.draw do

get '/robots.txt' => 'robots#robots'

resources :plant_parts

Expand Down
5 changes: 0 additions & 5 deletions public/robots.txt

This file was deleted.

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

describe RobotsController do
describe '#robots' do
let(:production_filename) { 'config/robots.txt' }
let(:staging_filename) { 'config/robots.staging.txt' }

before do
@request.host = "#{ subdomain }.localhost.com"
end

context 'subdomain is staging' do
let(:subdomain) { 'staging' }

it 'loads the staging robots.txt file' do
get :robots

expect(response).to be_success
expect(response.body).to eq(File.read(staging_filename))
end
end

context 'subdomain is www' do
let(:subdomain) { 'www' }

it 'loads the production robots.txt file' do
get :robots

expect(response).to be_success
expect(response.body).to eq(File.read(production_filename))
end
end

context 'subdomain is not present' do
let(:subdomain) { '' }

it 'loads the production robots.txt file' do
get :robots

expect(response).to be_success
expect(response.body).to eq(File.read(production_filename))
end
end

context 'subdomain is nonsense' do
let(:subdomain) { '1874ajnfien' }

it 'loads the production robots.txt file' do
get :robots

expect(response).to be_success
expect(response.body).to eq(File.read(production_filename))
end
end
end
end

0 comments on commit c47a1bc

Please sign in to comment.