Permalink
Browse files

Merge pull request #4 from forest/support-erb-preprocessing

Added support for erb pre-processing of effective robots.txt file.
  • Loading branch information...
2 parents dfec298 + 7e801d1 commit 4e64f55c21ad81b5bfca700f47b518a59073e85d @dpickett dpickett committed Jul 3, 2012
@@ -8,7 +8,8 @@ def show
protected
def robot_contents
- Roboto::ContentProvider.new.contents
+ # process erb template in the context of this controller request
+ Roboto::ContentProvider.new.contents(binding)
end
def robots_path
@@ -1,10 +1,18 @@
+require 'erb'
+
module Roboto
#provides the content of effective robots.txt file
class ContentProvider
# Reads the contents of the effective robots.txt file
# @return [String] the contents of the effective robots.txt file
- def contents
- @contents ||= File.read(path)
+ def contents(custom_binding = nil)
+ return @contents unless @contents.nil?
+
+ @contents = File.read(path)
+ if path.extname == '.erb'
+ @contents = ERB.new(@contents).result(custom_binding ? custom_binding : binding)
+ end
+ @contents
end
# Determines the most relevant robots.txt file.
@@ -30,7 +38,9 @@ def path
def lookup_paths
[
Rails.root.join("config/robots/#{Rails.env}.txt"),
+ Rails.root.join("config/robots/#{Rails.env}.txt.erb"),
Rails.root.join(relative_path_to_default),
+ Rails.root.join("#{relative_path_to_default}.erb"),
Roboto::Engine.root.join(relative_path_to_default)
]
end
@@ -17,8 +17,16 @@
it 'uses the environment specific file if found' do
path = Rails.root.join("config/robots/test.txt")
- FileUtils.touch(path)
+ File.open(path, 'wb') { |f| f.write(Rails.env) }
+ content_provider.path.should eql(path)
+ content_provider.contents.should eql(Rails.env)
+ end
+
+ it 'supports erb pre-processing' do
+ path = Rails.root.join("config/robots/test.txt.erb")
+ File.open(path, 'wb') { |f| f.write('<%= Rails.env %>') }
content_provider.path.should eql(path)
+ content_provider.contents.should eql(Rails.env)
end
it 'uses the default robots file if found in the rails root' do

0 comments on commit 4e64f55

Please sign in to comment.