Skip to content

Commit

Permalink
Create a robot to manage Robots.txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Aug 12, 2013
1 parent bd6d0d8 commit 513d482
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 52 deletions.
77 changes: 25 additions & 52 deletions app/controllers/admin/seo_controller.rb
@@ -1,76 +1,49 @@
class Admin::SeoController < Admin::BaseController
cache_sweeper :blog_sweeper
before_filter :set_setting, only: [:index, :titles]

def index
load_settings
if File.exists? "#{::Rails.root.to_s}/public/robots.txt"
@setting.robots = ""
file = File.readlines("#{::Rails.root.to_s}/public/robots.txt")
file.each do |line|
@setting.robots << line
end
else
build_robots
end
@setting.robots = Robot.new.rules
end

def permalinks
if request.post?
if params[:setting]['permalink_format'] and params[:setting]['permalink_format'] == 'custom'
params[:setting]['permalink_format'] = params[:setting]['custom_permalink']
update_settings
else
set_setting
if @setting.permalink_format != '/%year%/%month%/%day%/%title%' and
@setting.permalink_format != '/%year%/%month%/%title%' and
@setting.permalink_format != '/%title%'
@setting.custom_permalink = @setting.permalink_format
@setting.permalink_format = 'custom'
end
update
return
end

load_settings
if @setting.permalink_format != '/%year%/%month%/%day%/%title%' and
@setting.permalink_format != '/%year%/%month%/%title%' and
@setting.permalink_format != '/%title%'
@setting.custom_permalink = @setting.permalink_format
@setting.permalink_format = 'custom'
end
end

def titles
load_settings
end

def update
if request.post?
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end

save_robots unless params[:setting][:robots].blank?

redirect_to :action => params[:from]
end
update_settings if request.post?
rescue ActiveRecord::RecordInvalid
render params[:from]
end

private
def load_settings
@setting = this_blog
end

def save_robots
if File.writable? "#{::Rails.root.to_s}/public/robots.txt"
robots = File.new("#{::Rails.root.to_s}/public/robots.txt", "r+")
robots.write(params[:setting][:robots])
robots.close
def update_settings
if params[:setting]['permalink_format'] and params[:setting]['permalink_format'] == 'custom'
params[:setting]['permalink_format'] = params[:setting]['custom_permalink']
end
Blog.transaction do
params[:setting].each { |k,v| this_blog.send("#{k.to_s}=", v) }
this_blog.save
flash[:notice] = _('config updated.')
end
if params[:setting][:robots].present?
Robot.new.add(params[:setting][:robots])
end
redirect_to action: params[:from]
end

def build_robots
robots = File.new("#{::Rails.root.to_s}/public/robots.txt", "w+")
line = "User-agent: *\nAllow: /\nDisallow: /admin\n"
robots.write(line)
robots.close
@setting.robots = line
def set_setting
@setting = this_blog
end

end
27 changes: 27 additions & 0 deletions app/models/robot.rb
@@ -0,0 +1,27 @@
class Robot

FILE = "#{::Rails.root.to_s}/public/robots.txt"
DEFAULT_LINE = "User-agent: *\nAllow: /\nDisallow: /admin\n"

attr_reader :robots

def initialize
unless File.exists?(FILE)
robots = File.new(FILE, "w+")
robots.write(DEFAULT_LINE)
robots.close
end
@robots = File.new(FILE, "r+")
end

def add(rules)
if File.writable?(FILE)
robots.write(rules)
robots.close
end
end

def rules
robots.readlines.join('')
end
end
30 changes: 30 additions & 0 deletions spec/models/robot_spec.rb
@@ -0,0 +1,30 @@
require 'spec_helper'

describe Robot do
describe :rules do
it "read lines from robots.txt" do
fake_lines = ['something', 'other']
File.should_receive(:exists?).with(Robot::FILE).and_return(true)
fake_file = double('fake_file')
fake_file.should_receive(:readlines).and_return(fake_lines)
File.should_receive(:new).with(Robot::FILE, 'r+').and_return(fake_file)

expect(Robot.new.rules).to eq('somethingother')
end
end

describe :add do
it "if file is writable? then, add given lines" do
new_lines = 'something: to add'
File.should_receive(:writable?).with(Robot::FILE).and_return(true)
File.should_receive(:exists?).with(Robot::FILE).and_return(true)
fake_file = double('fake_file')
File.should_receive(:new).with(Robot::FILE, 'r+').and_return(fake_file)
fake_file.should_receive(:write).with(new_lines)
fake_file.should_receive(:close)

Robot.new.add(new_lines)
end
end
end

0 comments on commit 513d482

Please sign in to comment.