forked from publify/publify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a robot to manage Robots.txt file
- Loading branch information
Yannick Francois
committed
Aug 12, 2013
1 parent
bd6d0d8
commit 513d482
Showing
3 changed files
with
82 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|