Skip to content
This repository
Browse code

Remove Rails support features that automatically set directory and en…

…vironment

This feature was complicated to implement and test. It can be
replicated with a simple 2-line initializer within a Rails app. In
hindsight, this feature shouldn't have been added in the first place.
  • Loading branch information...
commit 50925437a5b60fb0de1522e4ab7a741d2c9105aa 1 parent 4b822f2
Mani Tadayon authored January 05, 2012
25  README.rdoc
Source Rendered
@@ -93,30 +93,27 @@ You can also add in extra values from outside of Yacht using the :merge option,
93 93
     # => ";var Yacht = {\"cookie_domain\":\"example.com\",\"current_time\":\"06/29/2011\"};"
94 94
 
95 95
 
96  
-=== Rails support
  96
+=== Rails
97 97
 
98  
-When used inside of a Rails application, Yacht offers some convenience features:
  98
+To use Yacht inside of Rails, just add an initializer to <tt>config/initializers</tt>:
99 99
 
100  
-1. <tt>Yacht.environment</tt> will be set by default to the currently loaded environment. For example, if you start your application in <tt>test</tt> mode, Yacht's values will be determined by the <tt>test</tt> entry in <tt>base.yml</tt> .
  100
+    # config/initializers/00_yacht_init.rb
101 101
 
102  
-2. <tt>Yacht.dir</tt> will be set to <tt>/RAILS_ROOT/config/yacht</tt> (where <tt>RAILS_ROOT</tt> is the location of your Rails app).
  102
+    # Yacht needs to know where to find its YAML files...
  103
+    # ...and the current Rails environment
  104
+    Yacht::Loader.dir          = Rails.root.join('config', 'yacht')
  105
+    Yacht::Loader.environment  = Rails.env
103 106
 
104  
-3. The <tt>yacht_js_snippet</tt> Rails helper wraps the string from <tt>Yacht::Loader#to_js_snippet</tt> in a script tag using Rails' +javascript_tag+ helper.
105  
-
106  
-    Yacht.rails_env
107  
-    # => "development"  # Yacht automatically adds the current Rails environment in ruby
108  
-    # If you want rails_env included in javascript, just add it to js_keys.yml
  107
+When used inside of a Rails application, the <tt>yacht_js_snippet</tt> Rails helper is included in the global <tt>ApplicationHelper</tt> so you can use it in views. <tt>yacht_js_snippet</tt> wraps the string from <tt>Yacht::Loader#to_js_snippet</tt> in a script tag using Rails' +javascript_tag+ helper.
109 108
 
110 109
     # inside a view or helper:
111 110
     yacht_js_snippet
112  
-    # => "<script type=\"text/javascript\">\n//<![CDATA[\n;var Yacht = {\"cookie_domain\":\"localhost\",\"rails_env\":\"development\"};\n//]]>\n</script>"
  111
+    # => "<script type=\"text/javascript\">\n//<![CDATA[\n;var Yacht = {\"cookie_domain\":\"localhost\"\n//]]>\n</script>"
113 112
 
114  
-    # you can also pass options to yacht_js_snippet:
115  
-    yacht_js_snippet(:merge => {:current_time => Time.now.to_s})
  113
+    # you can also pass options to yacht_js_snippet, like the the current Rails environment:
  114
+    yacht_js_snippet(:merge => {:current_time => Time.now.to_s, :rails_env => Rails.env})
116 115
     # => "<script type=\"text/javascript\">\n//<![CDATA[\n;var Yacht = {\"cookie_domain\":\"localhost\",\"rails_env\":\"development\",\"current_time\":\"06/29/2011\"};\n//]]>\n</script>"
117 116
 
118  
-4. <tt>Yacht.rails_env</tt> will be defined and match the currently loaded Rails environment.
119  
-
120 117
 == Ruby compatibility
121 118
 
122 119
 Yacht works with ruby 1.8.7 and 1.9.2.
2  lib/monkeypatches/rails_helper.rb
... ...
@@ -1,8 +1,6 @@
1 1
 module Yacht::RailsHelper
2 2
   # Create a string with a javascript version of Yacht values intended for inclusion in an HTML page
3 3
   #
4  
-  # @note environment will be set to Rails.env by default
5  
-  #
6 4
   # @example Set custom environment
7 5
   #   yacht_js_snippet(:env => 'local_development')
8 6
   #   # => "<script type=\"text/javascript\">;var Yacht = {\"foo\":\"bar\"};</script>"
8  lib/yacht.rb
@@ -4,14 +4,6 @@
4 4
 require "yacht/loader"
5 5
 
6 6
 if Object.const_defined?(:Rails)
7  
-  require "yacht/rails"
8  
-  class Yacht::Loader
9  
-    include Yacht::Rails
10  
-  end
11  
-
12  
-  Yacht::Loader.dir          = Yacht::Loader.rails_default_yacht_dir
13  
-  Yacht::Loader.environment  = Yacht::Loader.rails_env
14  
-
15 7
   require "monkeypatches/rails_helper"
16 8
 end
17 9
 
39  lib/yacht/rails.rb
... ...
@@ -1,39 +0,0 @@
1  
-module Yacht::Rails
2  
-  def self.included(base)
3  
-    base.extend ClassMethods
4  
-
5  
-    base.class_eval do
6  
-      class << self
7  
-        # alias_method_chain is wonky in rspec with ruby 1.8.7
8  
-        alias_method :all_without_rails_env, :all
9  
-        alias_method :all, :all_with_rails_env
10  
-      end
11  
-    end
12  
-
13  
-    base.dir          = base.rails_default_yacht_dir
14  
-    base.environment  = base.rails_env
15  
-  end
16  
-
17  
-  module ClassMethods
18  
-    def rails
19  
-      if Object.const_defined?(:Rails)
20  
-        Rails
21  
-      else
22  
-        raise "Rails is not defined!"
23  
-      end
24  
-    end
25  
-
26  
-    def rails_env
27  
-      rails.env
28  
-    end
29  
-
30  
-    def rails_default_yacht_dir
31  
-      rails.root.join('config', 'yacht')
32  
-    end
33  
-
34  
-    # Add current Rails environment to defined keys
35  
-    def all_with_rails_env
36  
-      all_without_rails_env.merge('rails_env' => rails_env)
37  
-    end
38  
-  end
39  
-end
65  spec/yacht/rails_spec.rb
... ...
@@ -1,65 +0,0 @@
1  
-require 'spec_helper'
2  
-require "yacht/rails"
3  
-
4  
-describe Yacht::Rails do
5  
-  class SomeClass
6  
-    class << self
7  
-      attr_accessor :environment, :dir
8  
-      def all
9  
-      end
10  
-    end
11  
-  end
12  
-
13  
-  class FakeRails; end
14  
-
15  
-  subject { SomeClass }
16  
-
17  
-  describe "when Rails does not exist" do
18  
-    it "including the module should raise an error" do
19  
-      expect{
20  
-        class SomeClass
21  
-          include Yacht::Rails
22  
-        end
23  
-      }.to raise_error ('Rails is not defined!')
24  
-    end
25  
-  end
26  
-
27  
-  describe "when Rails exists" do
28  
-    before :all do
29  
-      @yacht_dir = "/path/to/rails/config/yacht"
30  
-    end
31  
-
32  
-    around :each do |example|
33  
-      with_constants :Rails => FakeRails do
34  
-        Rails.stub(:env).and_return(:awesome)
35  
-        Rails.stub_chain(:root, :join).and_return(@yacht_dir)
36  
-
37  
-        class SomeClass
38  
-          include Yacht::Rails
39  
-        end
40  
-
41  
-        example.run
42  
-      end
43  
-    end
44  
-
45  
-    describe :environment do
46  
-      it "uses the current rails environment by default" do
47  
-        subject.environment.should == :awesome
48  
-      end
49  
-    end
50  
-
51  
-    describe :dir do
52  
-      it "uses config/yacht by default" do
53  
-        subject.dir.should == @yacht_dir
54  
-      end
55  
-    end
56  
-
57  
-    describe :all_with_rails_env do
58  
-      it "adds the current Rails environment to super" do
59  
-        subject.stub(:all_without_rails_env).and_return(:foo => :bar)
60  
-
61  
-        subject.all_with_rails_env.should == {:foo => :bar, 'rails_env' => :awesome}
62  
-      end
63  
-    end
64  
-  end
65  
-end

0 notes on commit 5092543

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