Permalink
Browse files

Refactor Rails extension into a separate module

Travis CI discovered a nasty dependency on load order in the specs due to the way Rails was mocked. This should fix the issue and simplify the code.
  • Loading branch information...
1 parent 5eb4e46 commit d55d5052b184705740aec1a1992d570682841d9d @bowsersenior bowsersenior committed Jan 5, 2012
Showing with 59 additions and 36 deletions.
  1. +7 −0 lib/yacht.rb
  2. +1 −1 lib/yacht/loader.rb
  3. +28 −14 lib/yacht/rails.rb
  4. +2 −0 spec/spec_helper.rb
  5. +21 −21 spec/yacht/rails_spec.rb
View
7 lib/yacht.rb
@@ -5,6 +5,13 @@
if Object.const_defined?(:Rails)
require "yacht/rails"
+ class Yacht::Loader
+ include Yacht::Rails
+ end
+
+ Yacht::Loader.dir = Yacht::Loader.rails_default_yacht_dir
+ Yacht::Loader.environment = Yacht::Loader.rails_env
+
require "monkeypatches/rails_helper"
end
View
2 lib/yacht/loader.rb
@@ -10,7 +10,7 @@ def environment
attr_accessor :dir
def full_file_path_for_config(config_type)
- raise Yacht::LoadError.new "No directory set" unless dir
+ raise Yacht::LoadError.new "No directory set" unless self.dir
File.join( self.dir, "#{config_type}.yml" )
end
View
42 lib/yacht/rails.rb
@@ -1,25 +1,39 @@
-class Yacht::Loader
- class << self
- # use the current rails environment by default
- def environment
- @environment ||= Rails.env
+module Yacht::Rails
+ def self.included(base)
+ base.extend ClassMethods
+
+ base.class_eval do
+ class << self
+ # alias_method_chain is wonky in rspec with ruby 1.8.7
+ alias_method :all_without_rails_env, :all
+ alias_method :all, :all_with_rails_env
+ end
+ end
+
+ base.dir = base.rails_default_yacht_dir
+ base.environment = base.rails_env
+ end
+
+ module ClassMethods
+ def rails
+ if Object.const_defined?(:Rails)
+ ::Rails
+ else
+ raise "Rails is not defined!"
+ end
end
- def dir
- @dir ||= Rails.root.join('config', 'yacht')
+ def rails_env
+ rails.env
end
- def full_file_path_for_config(config_type)
- dir.join("#{config_type}.yml")
+ def rails_default_yacht_dir
+ rails.root.join('config', 'yacht')
end
# Add current Rails environment to defined keys
def all_with_rails_env
- all_without_rails_env.merge('rails_env' => Rails.env)
+ all_without_rails_env.merge('rails_env' => rails_env)
end
- # alias_method_chain is wonky in rspec with ruby 1.8.7
- alias_method :all_without_rails_env, :all
- alias_method :all, :all_with_rails_env
-
end
end
View
2 spec/spec_helper.rb
@@ -7,6 +7,8 @@
require 'yacht'
+Dir['./spec/support/**/*.rb'].map {|f| require f}
+
RSpec.configure do |config|
config.before :all do
# ==============
View
42 spec/yacht/rails_spec.rb
@@ -1,46 +1,46 @@
require 'spec_helper'
+require "yacht/rails"
-describe "Rails support" do
- subject { Yacht::Loader }
+describe Yacht::Rails do
+ subject {
+ @yacht_dir = "/path/to/rails/config/yacht"
+
+ class SomeClass
+ class << self
+ attr_accessor :environment, :dir
+ def all
+ end
+ end
+ end
+
+ SomeClass.stub(:rails_env).and_return(:awesome)
+ SomeClass.stub(:rails_default_yacht_dir).and_return(@yacht_dir)
+
+ class SomeClass
+ include Yacht::Rails
+ end
+ }
before do
- Rails = stub("Rails")
- Object.stub(:alias_method_chain).as_null_object
- require "yacht/rails"
@yacht_dir = "/path/to/rails/config/yacht"
end
describe :environment do
it "uses the current rails environment by default" do
- Rails.should_receive(:env).and_return('awesome')
-
- subject.environment.should == 'awesome'
+ subject.environment.should == :awesome
end
end
describe :dir do
it "uses config/yacht by default" do
- Rails.stub_chain(:root, :join).and_return(@yacht_dir)
-
subject.dir.should == @yacht_dir
end
end
- describe :full_file_path_for_config do
- it "calls dir.join" do
- fake_dir = stub("dir stub")
- subject.stub(:dir).and_return(fake_dir)
- fake_dir.should_receive(:join).with("some.yml")
-
- subject.full_file_path_for_config("some")
- end
- end
-
describe :all_with_rails_env do
it "adds the current Rails environment to super" do
subject.stub(:all_without_rails_env).and_return(:foo => :bar)
- Rails.stub(:env).and_return(:awesome)
subject.all_with_rails_env.should == {:foo => :bar, 'rails_env' => :awesome}
end
end

0 comments on commit d55d505

Please sign in to comment.