From 80fea7c03293b1d4c04f0661d54c23e300452268 Mon Sep 17 00:00:00 2001 From: Graham Ashton Date: Fri, 12 Aug 2011 19:37:03 +0100 Subject: [PATCH] Moved Nesta::App.root to Nesta::Env.root. If you want to load Nesta inside another Ruby app you'll probably want to store it in a directory other than the process's current directory. To do so you'll need to set the root directory, and you have to do that before nesta/app is loaded. Given that nesta/app was what defined Nesta::App, this seemed like a daft place to store the root directory setting. --- CHANGES | 5 +++++ lib/nesta/app.rb | 4 ++-- lib/nesta/config.rb | 2 +- lib/nesta/env.rb | 7 +++++++ lib/nesta/path.rb | 4 ++-- spec/commands_spec.rb | 4 ++-- spec/overrides_spec.rb | 6 +++--- spec/spec_helper.rb | 1 + templates/config.ru | 5 +++-- 9 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 lib/nesta/env.rb diff --git a/CHANGES b/CHANGES index b1e10eb7..f7fe02d0 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,11 @@ * Bug fix: Removed trailing whitespace inside tags generated by the display_breadcrumbs() helper. + * Bug fix: Nesta::App.root couldn't be set until after nesta/app was + required. Odd that, as the only reason to want to change + Nesta::App.root would be before requiring nesta/app. Fixed by + creating Nesta::Env and moving root to there instead. + = 0.9.5 / (1 May 2011) * Added --version option to nesta command (Christopher Lindblom). diff --git a/lib/nesta/app.rb b/lib/nesta/app.rb index 95688518..38965f67 100644 --- a/lib/nesta/app.rb +++ b/lib/nesta/app.rb @@ -83,11 +83,11 @@ def format_date(date) def local_stylesheet? Nesta.deprecated('local_stylesheet?', 'use local_stylesheet_link_tag') - File.exist?(File.expand_path('views/local.sass', Nesta::App.root)) + File.exist?(File.expand_path('views/local.sass', Nesta::Env.root)) end def local_stylesheet_link_tag(name) - pattern = File.expand_path("views/#{name}.s{a,c}ss", Nesta::App.root) + pattern = File.expand_path("views/#{name}.s{a,c}ss", Nesta::Env.root) if Dir.glob(pattern).size > 0 haml_tag :link, :href => "/css/#{name}.css", :rel => "stylesheet" end diff --git a/lib/nesta/config.rb b/lib/nesta/config.rb index 5a1d89b5..1a86af52 100644 --- a/lib/nesta/config.rb +++ b/lib/nesta/config.rb @@ -46,7 +46,7 @@ def self.attachment_path(basename = nil) end def self.yaml_path - File.expand_path('config/config.yml', Nesta::App.root) + File.expand_path('config/config.yml', Nesta::Env.root) end def self.from_environment(setting) diff --git a/lib/nesta/env.rb b/lib/nesta/env.rb new file mode 100644 index 00000000..94606862 --- /dev/null +++ b/lib/nesta/env.rb @@ -0,0 +1,7 @@ +module Nesta + class Env + class << self + attr_accessor :root + end + end +end diff --git a/lib/nesta/path.rb b/lib/nesta/path.rb index cbe7670f..21c326b4 100644 --- a/lib/nesta/path.rb +++ b/lib/nesta/path.rb @@ -1,11 +1,11 @@ module Nesta class Path def self.local(*args) - File.expand_path(File.join(args), Nesta::App.root) + File.expand_path(File.join(args), Nesta::Env.root) end def self.themes(*args) - File.expand_path(File.join('themes', *args), Nesta::App.root) + File.expand_path(File.join('themes', *args), Nesta::Env.root) end end end diff --git a/spec/commands_spec.rb b/spec/commands_spec.rb index 93bc51f4..8d7c7206 100644 --- a/spec/commands_spec.rb +++ b/spec/commands_spec.rb @@ -115,7 +115,7 @@ def rakefile_source FileUtils.mkdir_p(File.dirname(@config_path)) Nesta::Config.stub!(:yaml_path).and_return(@config_path) create_config_yaml('content: path/to/content') - Nesta::App.stub!(:root).and_return(@project_path) + Nesta::Env.stub!(:root).and_return(@project_path) @repo_url = 'git://github.com/gma/nesta-demo-content.git' @demo_path = project_path('content-demo') @command = Nesta::Commands::Demo::Content.new @@ -265,7 +265,7 @@ def should_exist(file) end before(:each) do - Nesta::App.stub!(:root).and_return(FixtureHelper::FIXTURE_DIR) + Nesta::Env.stub!(:root).and_return(FixtureHelper::FIXTURE_DIR) @name = 'my-new-theme' Nesta::Commands::Theme::Create.new(@name).execute end diff --git a/spec/overrides_spec.rb b/spec/overrides_spec.rb index cbc1f22c..957d42c8 100644 --- a/spec/overrides_spec.rb +++ b/spec/overrides_spec.rb @@ -27,8 +27,8 @@ def create_app_file(type) end before(:each) do - @app_root = Nesta::App.root - Nesta::App.root = File.expand_path('fixtures/tmp', File.dirname(__FILE__)) + @app_root = Nesta::Env.root + Nesta::Env.root = File.expand_path('fixtures/tmp', File.dirname(__FILE__)) @theme = 'my-theme' @fixtures = [] stub_configuration @@ -36,7 +36,7 @@ def create_app_file(type) after(:each) do @fixtures.each { |path| FileUtils.rm(path) if File.exist?(path) } - Nesta::App.root = @app_root + Nesta::Env.root = @app_root end describe "when local files exist" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8c87ae73..5227c337 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ class App < Sinatra::Base end end +require File.expand_path('../lib/nesta/env', File.dirname(__FILE__)) require File.expand_path('../lib/nesta/app', File.dirname(__FILE__)) module FixtureHelper diff --git a/templates/config.ru b/templates/config.ru index e965cfac..93e23870 100644 --- a/templates/config.ru +++ b/templates/config.ru @@ -3,7 +3,8 @@ require 'bundler/setup' Bundler.require(:default) -require 'nesta/app' +require 'nesta/env' +Nesta::Env.root = ::File.expand_path('.', ::File.dirname(__FILE__)) -Nesta::App.root = ::File.expand_path('.', ::File.dirname(__FILE__)) +require 'nesta/app' run Nesta::App