Skip to content
This repository
Browse code

Clean up references to Rails.root

* only load Rails-specific stuff if in Rails 
* add a Application spec
  • Loading branch information...
commit 23ea310cba84a1e442b9f63ad4a3d08ade9852da 1 parent 50c670d
Karl Varga authored
6 lib/sitemap_generator/interpreter.rb
@@ -6,9 +6,9 @@ module SitemapGenerator
6 6 # Rails URL helpers.
7 7 class Interpreter
8 8
9   - if SitemapGenerator::Utilities.rails3?
  9 + if SitemapGenerator.app.rails3?
10 10 include ::Rails.application.routes.url_helpers
11   - else
  11 + elsif SitemapGenerator.app.rails?
12 12 require 'action_controller'
13 13 include ActionController::UrlWriter
14 14 end
@@ -23,7 +23,7 @@ def initialize(sitemap, sitemap_config_file=nil, &block)
23 23 if block_given?
24 24 instance_eval(&block)
25 25 else
26   - sitemap_config_file ||= File.join(::Rails.root, 'config/sitemap.rb')
  26 + sitemap_config_file ||= SitemapGenerator.app.root + 'config/sitemap.rb'
27 27 eval(File.read(sitemap_config_file), nil, sitemap_config_file.to_s)
28 28 end
29 29 end
10 lib/sitemap_generator/utilities.rb
@@ -4,26 +4,26 @@ module Utilities
4 4
5 5 # Copy templates/sitemap.rb to config if not there yet.
6 6 def install_sitemap_rb(verbose=false)
7   - if File.exist?(File.join(Rails.root, 'config/sitemap.rb'))
  7 + if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
8 8 puts "already exists: config/sitemap.rb, file not copied" if verbose
9 9 else
10 10 FileUtils.cp(
11 11 SitemapGenerator.templates.template_path(:sitemap_sample),
12   - File.join(Rails.root, 'config/sitemap.rb'))
  12 + SitemapGenerator.app.root + 'config/sitemap.rb')
13 13 puts "created: config/sitemap.rb" if verbose
14 14 end
15 15 end
16 16
17 17 # Remove config/sitemap.rb if exists.
18 18 def uninstall_sitemap_rb
19   - if File.exist?(File.join(Rails.root, 'config/sitemap.rb'))
20   - File.rm(File.join(Rails.root, 'config/sitemap.rb'))
  19 + if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
  20 + File.rm(SitemapGenerator.app.root + 'config/sitemap.rb')
21 21 end
22 22 end
23 23
24 24 # Clean sitemap files in output directory.
25 25 def clean_files
26   - FileUtils.rm(Dir[File.join(Rails.root, 'public/sitemap*.xml.gz')])
  26 + FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz'])
27 27 end
28 28
29 29 # Validate all keys in a hash match *valid keys, raising ArgumentError on a
23 spec/sitemap_generator/application_spec.rb
... ... @@ -0,0 +1,23 @@
  1 +require 'spec_helper'
  2 +
  3 +describe SitemapGenerator::Application do
  4 + before :each do
  5 + @app = SitemapGenerator::Application.new
  6 + end
  7 +
  8 + context "rails3?" do
  9 + tests = {
  10 + :nil => false,
  11 + '2.3.11' => false,
  12 + '3.0.1' => true,
  13 + '3.0.11' => true
  14 + }
  15 +
  16 + it "should identify the rails version correctly" do
  17 + tests.each do |version, result|
  18 + Rails.expects(:version).returns(version)
  19 + @app.rails3?.should == result
  20 + end
  21 + end
  22 + end
  23 +end
8 spec/sitemap_generator/sitemap_generator_spec.rb
@@ -162,14 +162,16 @@
162 162 #
163 163
164 164 def rails_path(file)
165   - File.join(::Rails.root, file)
  165 + SitemapGenerator.app.root + file
166 166 end
167 167
168 168 def copy_sitemap_file_to_rails_app
169   - FileUtils.cp(File.join(SitemapGenerator.root, 'spec/sitemap.file'), File.join(::Rails.root, '/config/sitemap.rb'))
  169 + FileUtils.cp(File.join(SitemapGenerator.root, 'spec/sitemap.file'), SitemapGenerator.app.root + '/config/sitemap.rb')
170 170 end
171 171
172 172 def delete_sitemap_file_from_rails_app
173   - FileUtils.remove(File.join(::Rails.root, '/config/sitemap.rb')) rescue nil
  173 + FileUtils.remove(SitemapGenerator.app.root + '/config/sitemap.rb')
  174 + rescue
  175 + nil
174 176 end
175 177 end
15 spec/sitemap_generator/utilities_spec.rb
@@ -2,21 +2,6 @@
2 2
3 3 describe SitemapGenerator::Utilities do
4 4
5   - describe "rails3?" do
6   - tests = {
7   - :nil => false,
8   - '2.3.11' => false,
9   - '3.0.1' => true,
10   - '3.0.11' => true
11   - }
12   - it "should identify the rails version correctly" do
13   - tests.each do |version, result|
14   - Rails.expects(:version).returns(version)
15   - SitemapGenerator::Utilities.rails3?.should == result
16   - end
17   - end
18   - end
19   -
20 5 describe "assert_valid_keys" do
21 6 it "should raise error on invalid keys" do
22 7 lambda {

0 comments on commit 23ea310

Please sign in to comment.
Something went wrong with that request. Please try again.